Authentication
[[toc]]
Introdution
Authentication is an indispensable feature in Web Applications, the facades.Auth()
module of Goravel provides support for JWT.
Configration
You can configure default guard and multiple guards in the config/auth.go
file to switch different user identities in the application.
You can configure parameters of JWT in the config/jwt.go
file, such as secret
, ttl
, refresh_ttl
.
Generate JWT Token
Generate Token Using User
You can generate Token by Model, there is no extra configuration if the model use orm.Model
, otherwise, you need to configure Tag on the model permary key filed, for example:
Generate Token Using ID
Parse Token
Through payload
you can get:
Guard
: Current Guard;Key
: User flag;ExpireAt
: Expire time;IssuedAt
: Issued time;
When
err
isn't nil other thanErrorTokenExpired
, payload == nil
You can judge whether the Token is expired by err:
Token can be parsed normally with or without Bearer prefix.
Get User
You need to generate Token by Parse
before getting user, the process can be handled in HTTP middleware.
Refresh Token
You need to generate Token by Parse
before refreshing user.
Logout
Multiple Guards
When don't use default guard, the
Guard
method needs to be called beforehand when calling the above methods.
Last updated