Comments (3)
There is no example on refresh tokens in this repository because there are different strategies for that. If you see the README's References section's link you can follow some articles about it. Instead, we have a simple example at: https://github.com/kataras/iris/tree/jwt-new-features/_examples/auth/jwt/refresh-token.
In-short:
Sign access, refresh tokens and generate a pair which sent to the client
func generateTokenPair() jwt.TokenPair {
// Simulate a user...
userID := "53afcf05-38a3-43c3-82af-8bbbe0e4a149"
refreshClaims := jwt.Claims{Subject: userID}
accessClaims := UserClaims{
ID: userID,
Username: "kataras",
}
accessToken, err := jwt.Sign(alg, secret, accessClaims, 5 * time.Minute)
refreshToken, err := jwt.Sign(alg, secert, refreshClaims, 1 * time.Hour)
tokenPair := jwt.NewTokenPair(accessToken, refreshToken)
return tokenPair
}
Create a handler on /login
and send the result of that token pair.
The refresh operation (there are other strategies though)
currentUserID := "53afcf05-38a3-43c3-82af-8bbbe0e4a149"
refreshToken := take from header...
verifiedToken, err := jwt.Verify(alg, secret, refreshToken, jwt.Expected{Subject: currentUserID})
if err != nil { /* send 401 */ }
tokenPair := generateTokenPair()
// ^ send this to the client
Create a handler on /refresh
and send the result of that token pair.
Your client can fire 'silent' calls to the /refresh
to renew its access token automatically.
from jwt.
@daheige If you still need a native net/http
example, just comment below and i will prepare you an http.Server, http.Client
and a javascript client examples :) Keep note that the refresh strategy depends on your application requirements and it's better if you just google and get ideas from there instead, so you have the complete picture in your mind before decide what is better for you.
from jwt.
Thank you very much. After reading what you said, there are indeed different refresh strategies. This depends on the business scenario. I will try these strategies you mentioned, and if there are other questions, I will consult you again.
from jwt.
Related Issues (14)
- jwt: unexpected token algorithm "Error" HOT 2
- "Unexpected token algorithm" error when validating CloudFlare's JWT HOT 1
- ignore expired tokens on certain routes HOT 1
- jwt: invalid token form
- Generating the wrong token when customising claims
- Header decode compatibility issue HOT 9
- Fix or remove Audience Claim from standard claims HOT 2
- [BUG] jwt Claims - Invalid for individual custom struct fields HOT 2
- Unnecessary custom base64 implementation HOT 2
- Audience parsing doesn't reject unexpected items HOT 1
- JWT not rejected when audience claim present but not specified by verifying party HOT 1
- payload is not a type of JSON error
- CompareHeader function fails when key id (kid) included HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwt.