38 lines
931 B
PHP
38 lines
931 B
PHP
<?php
|
|
|
|
namespace App\Actions\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class LoginAction
|
|
{
|
|
/**
|
|
* Execute the login action.
|
|
*
|
|
* @param array $credentials
|
|
* @return array
|
|
* @throws ValidationException
|
|
*/
|
|
public function execute(array $credentials): array
|
|
{
|
|
$user = User::where('email', $credentials['email'])->first();
|
|
|
|
if (!$user || !Hash::check($credentials['password'], $user->password)) {
|
|
throw ValidationException::withMessages([
|
|
'email' => [__('auth.failed')],
|
|
]);
|
|
}
|
|
|
|
$token = $user->createToken('auth_token')->plainTextToken;
|
|
|
|
return [
|
|
'user' => $user,
|
|
'token' => $token,
|
|
'roles' => $user->getRoleNames(),
|
|
'permissions' => $user->getAllPermissions()->pluck('name'),
|
|
];
|
|
}
|
|
}
|