Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jimmylet's avatar

Laravel API Rest and user login

Hello all,

I have to do a job for a course. I have to try to make a Rest API for Laravel. For that, I installed Passport.

I followed several tutorials on the internet and realized a login function in my Auth / LoginController

public function login(Request $request)
    {
        $this->validateLogin($request);
        
        if ($this->attemptLogin($request)) {
            $user = Auth::user();
            $success['token'] = $user->createToken('auth')->accessToken;
            $success['user'] = $user;
            return response()->json($success, 201);
        }
        
        return $this->sendFailedLoginResponse($request);
    }

I test with Postman and everything seems to work. As well as my logout function

public function logout()
    {
        $user = Auth::user();
        $user->token()->revoke();
        $user->token()->delete();
        
        return response()->json(null, 204);
    }

Now, I try to integrate that into my front end.

I can connect with Laravel's basic login form. Only he redirects me to the json of my login () function.

When I refresh the page, it redirects me to the dashboard (it is that I am well connected). In my database, the token is well created.

How to use the basic Laravel auth to use Passport?

Or did not I understand something?

Thank you !

0 likes
0 replies

Please or to participate in this conversation.