Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat (readme): added some examples on refresh token usage
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgalbu committed Nov 9, 2021
1 parent c4f9635 commit 9f41865
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,35 @@ Just make sure to prepend `.use("jwt")`:

```ts
//authenticate() example
Route.get('/dashboard', async ({ auth }) => {
Route.get('/dashboard', async ({ auth }:HttpContextContract) => {
await auth.use("jwt").authenticate();
const userModel = auth.use("jwt").user!;
const userPayloadFromJwt = auth.use("jwt").payload!;
});

//generate() example:
Route.get('/login', async ({ auth }) => {
Route.get('/login', async ({ auth }:HttpContextContract) => {
const user = await User.find(1);
const jwt = await auth.use("jwt").generate(user);
//or using .login():
//const jwt = await auth.use("jwt").login(user);
});

Route.post('/logout', async ({ auth, response }) => {
//refresh token usage example:
Route.post('/refresh', async ({ auth, request }:HttpContextContract) => {
const refreshToken = request.input("refresh_token");
const jwt = await auth.use("jwt").loginViaRefreshToken(refreshToken);
});

Route.post('/logout', async ({ auth, response }:HttpContextContract) => {
await auth.use('jwt').revoke()
return {
revoked: true
}
})
```

By default, `.generate()` uses a payload like the following:
By default, `.generate()` or `.login()` uses a payload like the following:

```ts
//user is a Lucid model
Expand All @@ -86,10 +94,21 @@ By default, `.generate()` uses a payload like the following:
}
```

If you want to generate a JWT with a different payload, simply specify `payload` when calling `.generate()`:
If you want to generate a JWT with a different payload, simply specify `payload` when calling `.generate()` or `.login()`:

```ts
await auth.use("jwt").login(user, {
payload: {
email: user.email,
},
});
```

With the refresh token, you can obtain a new JWT using `loginViaRefreshToken()`:

```ts
await auth.use("jwt").generate(user, {
const refreshToken = request.input("refresh_token");
await auth.use("jwt").loginViaRefreshToken(refreshToken, {
payload: {
email: user.email,
},
Expand Down

0 comments on commit 9f41865

Please sign in to comment.