Nitish Patra
Nitish Patra

Reputation: 329

JWT Auth for Laravel

I am trying to authenticate a user from App, and I have written the API in laravel. I want to know what is the difference between JWTAuth::fromUser($user),JWTAuth::toUser($user) and JWTAuth::attempt($user) and any advantages over using it?

Upvotes: 1

Views: 6909

Answers (1)

rkj
rkj

Reputation: 8287

JWTAuth::fromUser($user)

If you have user instance already and want to generate token for that user then you use fromUser

$token = JWTAuth::fromUser($user);

JWTAuth::attempt($user)

This function is used to authenticate user from credentials and if authenticate success then it generate token for authenticated user

if (! $token = JWTAuth::attempt($credentials)) {
    return Response::json(['error' => 'invalid_credentials'], 401);
}

JWTAuth::toUser($user)

When you want to get user from token then you use toUser method. like this

$user = JWTAuth::toUser($token);

For details you can check it here https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens

Upvotes: 8

Related Questions