Skip to content

Commit d2cfdd7

Browse files
authored
Merge pull request #12 from cstaleror/main
feat: possibility of adding a name to the refreshToken and passing abilities to the new refresh token
2 parents 0c58e3b + e0f1992 commit d2cfdd7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ const refreshToken = await User.refreshTokens.create(user)
204204
// if you use the refresh token
205205
router.post('jwt/refresh', async ({ auth }) => {
206206
// this will authenticate the user using the refresh token
207-
// it will delete the old refresh token and generate a new one
208-
const user = await auth.use('jwt').authenticateWithRefreshToken()
207+
// it will delete the old refresh token and generate a new one with the same abilities
208+
// You could pass a name for the new token as well
209+
const user = await auth.use('jwt').authenticateWithRefreshToken('optional_name')
209210
const newRefreshToken = user.currentToken
210211
const newToken = await auth.use('jwt').generate(user)
211212

src/jwt.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ export class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>>
177177
return this.getUserOrFail()
178178
}
179179

180-
async authenticateWithRefreshToken(): Promise<
181-
UserProvider[typeof symbols.PROVIDER_REAL_USER] & { currentToken: string }
182-
> {
180+
async authenticateWithRefreshToken(
181+
name?: string
182+
): Promise<UserProvider[typeof symbols.PROVIDER_REAL_USER] & { currentToken: string }> {
183183
/**
184184
* Avoid re-authentication when it has been done already
185185
* for the given request
@@ -240,6 +240,11 @@ export class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>>
240240
currentToken: string
241241
}
242242

243+
/**
244+
* Get the same abilities for the new refresh token
245+
*/
246+
const abilities = accessToken.abilities
247+
243248
/**
244249
* Delete the refresh token from the database
245250
*/
@@ -250,7 +255,9 @@ export class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>>
250255
})
251256
}
252257

253-
const newRefreshToken = await this.#refreshTokenUserProvider.createToken(this.user)
258+
const newRefreshToken = await this.#refreshTokenUserProvider.createToken(this.user, abilities, {
259+
name,
260+
})
254261

255262
if (!newRefreshToken.value) {
256263
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {

0 commit comments

Comments
 (0)