Skip to content

Commit 218a846

Browse files
authored
enh(#895): Custom refresh response token pointer (#910)
1 parent 47e6192 commit 218a846

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

docs/guide/local/quick-start.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export default defineNuxtConfig({
224224
refreshOnlyToken: true,
225225
token: {
226226
signInResponseRefreshTokenPointer: '/refresh-token',
227+
refreshResponseTokenPointer: '',
227228
refreshRequestTokenPointer: '/refresh-token',
228229
cookieName: 'auth.token',
229230
maxAgeInSeconds: 1800,
@@ -291,6 +292,19 @@ E.g., setting this to `/token/refreshToken` and returning an object like `{ toke
291292

292293
This follows the JSON Pointer standard, see its RFC6901 here: https://www.rfc-editor.org/rfc/rfc6901
293294

295+
#### `refreshResponseTokenPointer`
296+
297+
- **Type:** `string`
298+
- **Default:** `''`
299+
300+
How to extract the authentication-token from the refresh response.
301+
302+
E.g., setting this to `/token/bearer` and returning an object like `{ token: { bearer: 'THE_AUTH_TOKEN' }, timestamp: '2023' }` from the `refresh` endpoint will result in `nuxt-auth` extracting and storing `THE_AUTH_TOKEN`.
303+
304+
If not set, `token.signInResponseTokenPointer` will be used instead.
305+
306+
This follows the JSON Pointer standard, see its RFC6901 here: https://www.rfc-editor.org/rfc/rfc6901
307+
294308
#### `refreshRequestTokenPointer`
295309

296310
- **Type:** `string`

playground-local/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineNuxtConfig({
2727
endpoint: { path: '/refresh', method: 'post' },
2828
token: {
2929
signInResponseRefreshTokenPointer: '/token/refreshToken',
30+
refreshResponseTokenPointer: '',
3031
refreshRequestTokenPointer: '/refreshToken'
3132
},
3233
}

src/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const defaultsByBackend: {
7777
refreshOnlyToken: true,
7878
token: {
7979
signInResponseRefreshTokenPointer: '/refreshToken',
80+
refreshResponseTokenPointer: '',
8081
refreshRequestTokenPointer: '/refreshToken',
8182
cookieName: 'auth.refresh-token',
8283
maxAgeInSeconds: 60 * 60 * 24 * 7, // 7 days

src/runtime/composables/local/useAuth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ async function refresh(getSessionOptions?: GetSessionOptions) {
199199
})
200200

201201
// Extract the new token from the refresh response
202-
const extractedToken = jsonPointerGet(response, config.token.signInResponseTokenPointer)
202+
const tokenPointer = config.refresh.token.refreshResponseTokenPointer || config.token.signInResponseTokenPointer
203+
const extractedToken = jsonPointerGet(response, tokenPointer)
203204
if (typeof extractedToken !== 'string') {
204205
console.error(
205206
`Auth: string token expected, received instead: ${JSON.stringify(extractedToken)}. `
206-
+ `Tried to find token at ${config.token.signInResponseTokenPointer} in ${JSON.stringify(response)}`
207+
+ `Tried to find token at ${tokenPointer} in ${JSON.stringify(response)}`
207208
)
208209
return
209210
}

src/runtime/plugins/refresh-token.server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export default defineNuxtPlugin({
3232
headers
3333
})
3434

35+
const tokenPointer = provider.refresh.token.refreshResponseTokenPointer || provider.token.signInResponseTokenPointer
3536
const extractedToken = jsonPointerGet(
3637
response,
37-
provider.token.signInResponseTokenPointer
38+
tokenPointer
3839
)
3940
if (typeof extractedToken !== 'string') {
4041
console.error(
4142
`Auth: string token expected, received instead: ${JSON.stringify(
4243
extractedToken
43-
)}. Tried to find token at ${
44-
provider.token.signInResponseTokenPointer
44+
)}. Tried to find token at ${tokenPointer
4545
} in ${JSON.stringify(response)}`
4646
)
4747
return
@@ -57,8 +57,7 @@ export default defineNuxtPlugin({
5757
console.error(
5858
`Auth: string token expected, received instead: ${JSON.stringify(
5959
extractedRefreshToken
60-
)}. Tried to find token at ${
61-
provider.refresh.token.signInResponseRefreshTokenPointer
60+
)}. Tried to find token at ${provider.refresh.token.signInResponseRefreshTokenPointer
6261
} in ${JSON.stringify(response)}`
6362
)
6463
return

src/runtime/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ export interface ProviderLocal {
255255
* @example / Access the root of the sign-in response object, useful when your endpoint returns a plain, non-object string as the token
256256
*/
257257
signInResponseRefreshTokenPointer?: string
258+
/**
259+
* How to extract the authentication-token from the refresh response.
260+
*
261+
*
262+
* E.g., setting this to `/token/bearer` and returning an object like `{ token: { bearer: 'THE_AUTH_TOKEN' }, timestamp: '2023' }` from the `refresh` endpoint will
263+
* result in `nuxt-auth` extracting and storing `THE_AUTH_TOKEN`.
264+
*
265+
* If not set, `token.signInResponseTokenPointer` will be used instead.
266+
*
267+
* This follows the JSON Pointer standard, see it's RFC6901 here: https://www.rfc-editor.org/rfc/rfc6901
268+
*
269+
* @default ''
270+
* @example / Access the root of the refresh response object, useful when your endpoint returns a plain, non-object string as the token
271+
*/
272+
refreshResponseTokenPointer?: string
258273
/**
259274
* How to do a fetch for the refresh token.
260275
*

0 commit comments

Comments
 (0)