Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(#895): Custom refresh response token pointer #910

14 changes: 14 additions & 0 deletions docs/guide/local/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export default defineNuxtConfig({
refreshOnlyToken: true,
token: {
signInResponseRefreshTokenPointer: '/refresh-token',
refreshResponseTokenPointer: '',
refreshRequestTokenPointer: '/refresh-token',
cookieName: 'auth.token',
maxAgeInSeconds: 1800,
Expand Down Expand Up @@ -291,6 +292,19 @@ E.g., setting this to `/token/refreshToken` and returning an object like `{ toke

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

#### `refreshResponseTokenPointer`

- **Type:** `string`
- **Default:** `''`

How to extract the authentication-token from the refresh response.

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`.

If not set, `token.signInResponseTokenPointer` will be used instead.

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

#### `refreshRequestTokenPointer`

- **Type:** `string`
Expand Down
1 change: 1 addition & 0 deletions playground-local/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineNuxtConfig({
endpoint: { path: '/refresh', method: 'post' },
token: {
signInResponseRefreshTokenPointer: '/token/refreshToken',
refreshResponseTokenPointer: '',
refreshRequestTokenPointer: '/refreshToken'
},
}
Expand Down
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const defaultsByBackend: {
refreshOnlyToken: true,
token: {
signInResponseRefreshTokenPointer: '/refreshToken',
refreshResponseTokenPointer: '',
refreshRequestTokenPointer: '/refreshToken',
cookieName: 'auth.refresh-token',
maxAgeInSeconds: 60 * 60 * 24 * 7, // 7 days
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/composables/local/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ async function refresh(getSessionOptions?: GetSessionOptions) {
})

// Extract the new token from the refresh response
const extractedToken = jsonPointerGet(response, config.token.signInResponseTokenPointer)
const tokenPointer = config.refresh.token.refreshResponseTokenPointer || config.token.signInResponseTokenPointer
const extractedToken = jsonPointerGet(response, tokenPointer)
if (typeof extractedToken !== 'string') {
console.error(
`Auth: string token expected, received instead: ${JSON.stringify(extractedToken)}. `
+ `Tried to find token at ${config.token.signInResponseTokenPointer} in ${JSON.stringify(response)}`
+ `Tried to find token at ${tokenPointer} in ${JSON.stringify(response)}`
)
return
}
Expand Down
9 changes: 4 additions & 5 deletions src/runtime/plugins/refresh-token.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export default defineNuxtPlugin({
headers
})

const tokenPointer = provider.refresh.token.refreshResponseTokenPointer || provider.token.signInResponseTokenPointer
const extractedToken = jsonPointerGet(
response,
provider.token.signInResponseTokenPointer
tokenPointer
)
if (typeof extractedToken !== 'string') {
console.error(
`Auth: string token expected, received instead: ${JSON.stringify(
extractedToken
)}. Tried to find token at ${
provider.token.signInResponseTokenPointer
)}. Tried to find token at ${tokenPointer
} in ${JSON.stringify(response)}`
)
return
Expand All @@ -57,8 +57,7 @@ export default defineNuxtPlugin({
console.error(
`Auth: string token expected, received instead: ${JSON.stringify(
extractedRefreshToken
)}. Tried to find token at ${
provider.refresh.token.signInResponseRefreshTokenPointer
)}. Tried to find token at ${provider.refresh.token.signInResponseRefreshTokenPointer
} in ${JSON.stringify(response)}`
)
return
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ export interface ProviderLocal {
* @example / Access the root of the sign-in response object, useful when your endpoint returns a plain, non-object string as the token
*/
signInResponseRefreshTokenPointer?: string
/**
* How to extract the authentication-token from the refresh response.
*
*
* 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`.
*
* If not set, `token.signInResponseTokenPointer` will be used instead.
*
* This follows the JSON Pointer standard, see it's RFC6901 here: https://www.rfc-editor.org/rfc/rfc6901
*
* @default ''
* @example / Access the root of the refresh response object, useful when your endpoint returns a plain, non-object string as the token
*/
refreshResponseTokenPointer?: string
/**
* How to do a fetch for the refresh token.
*
Expand Down