tymon/jwt-auth
Laravel JWT authentication package providing token issuing, parsing, refresh, and invalidation using JSON Web Tokens. Integrates with Laravel guards/middleware, supports custom claims and multiple auth providers, and includes docs and testing support.
The following methods are available on the Auth guard instance.
If the newly created 'api' guard is not set as a default guard or you have defined multiple guards to handle authentication, you should specify the guard when calling auth().
$token = auth('api')->attempt($credentials);
Attempt to authenticate a user via some credentials.
// Generate a token for the user if the credentials are valid
$token = auth()->attempt($credentials);
This will return either a jwt or null
Log a user in and return a jwt for them.
// Get some user from somewhere
$user = User::first();
// Get the token
$token = auth()->login($user);
Get the currently authenticated user.
// Get the currently authenticated user
$user = auth()->user();
If the user is not then authenticated, then null will be returned.
Get the currently authenticated user or throw an exception.
try {
$user = auth()->userOrFail();
} catch (\Tymon\JWTAuth\Exceptions\UserNotDefinedException $e) {
// do something
}
If the user is not set, then a Tymon\JWTAuth\Exceptions\UserNotDefinedException will be thrown
Log the user out - which will invalidate the current token and unset the authenticated user.
auth()->logout();
// Pass true to force the token to be blacklisted "forever"
auth()->logout(true);
Refresh a token, which invalidates the current one
$newToken = auth()->refresh();
// Pass true as the first param to force the token to be blacklisted "forever".
// The second parameter will reset the claims for the new token
$newToken = auth()->refresh(true, true);
Invalidate the token (add it to the blacklist)
auth()->invalidate();
// Pass true as the first param to force the token to be blacklisted "forever".
auth()->invalidate(true);
Get a token based on a given user's id.
$token = auth()->tokenById(123);
Get the raw JWT payload
$payload = auth()->payload();
// then you can access the claims directly e.g.
$payload->get('sub'); // = 123
$payload['jti']; // = 'asfe4fq434asdf'
$payload('exp') // = 123456
$payload->toArray(); // = ['sub' => 123, 'exp' => 123456, 'jti' => 'asfe4fq434asdf'] etc
Validate a user's credentials
if (auth()->validate($credentials)) {
// credentials are valid
}
$token = auth()->claims(['foo' => 'bar'])->attempt($credentials);
$user = auth()->setToken('eyJhb...')->user();
$user = auth()->setRequest($request)->user();
$token = auth()->setTTL(7200)->attempt($credentials);
How can I help you explore Laravel packages today?