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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions docs/guide/local/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export default defineNuxtConfig({
endpoint: { path: '/refresh', method: 'POST' },
refreshOnlyToken: true,
token: {
refreshResponseTokenPointer: '/token',
Rizzato95 marked this conversation as resolved.
Show resolved Hide resolved
signInResponseRefreshTokenPointer: '/refresh-token',
refreshRequestTokenPointer: '/refresh-token',
cookieName: 'auth.token',
Expand Down Expand Up @@ -280,6 +281,17 @@ When refreshOnlyToken is set, only the `token` will be refreshed and the `refres

### `token`

#### `refreshResponseTokenPointer`
Rizzato95 marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

#### `signInResponseRefreshTokenPointer`

- **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 @@ -26,6 +26,7 @@ export default defineNuxtConfig({
isEnabled: process.env.NUXT_AUTH_REFRESH_ENABLED !== 'false',
endpoint: { path: '/refresh', method: 'post' },
token: {
refreshResponseTokenPointer: '/token/accessToken',
signInResponseRefreshTokenPointer: '/token/refreshToken',
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 @@ -76,6 +76,7 @@ const defaultsByBackend: {
endpoint: { path: '/refresh', method: 'post' },
refreshOnlyToken: true,
token: {
refreshResponseTokenPointer: '/token',
Rizzato95 marked this conversation as resolved.
Show resolved Hide resolved
signInResponseRefreshTokenPointer: '/refreshToken',
refreshRequestTokenPointer: '/refreshToken',
cookieName: 'auth.refresh-token',
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables/local/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ async function refresh(getSessionOptions?: GetSessionOptions) {
})

// Extract the new token from the refresh response
const extractedToken = jsonPointerGet(response, config.token.signInResponseTokenPointer)
const extractedToken = jsonPointerGet(response, config.refresh.token.refreshResponseTokenPointer)
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 ${config.refresh.token.refreshResponseTokenPointer} in ${JSON.stringify(response)}`
)
return
}
Expand Down
8 changes: 3 additions & 5 deletions src/runtime/plugins/refresh-token.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ export default defineNuxtPlugin({

const extractedToken = jsonPointerGet(
response,
provider.token.signInResponseTokenPointer
provider.refresh.token.refreshResponseTokenPointer
)
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 ${provider.refresh.token.refreshResponseTokenPointer
} in ${JSON.stringify(response)}`
)
return
Expand All @@ -57,8 +56,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
12 changes: 12 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ export interface ProviderLocal {
* Settings for the refresh-token that `nuxt-auth` receives from the `signIn` endpoint that is used for the `refresh` endpoint.
*/
token?: {
/**
* 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`.
*
* This follows the JSON Pointer standard, see it's RFC6901 here: https://www.rfc-editor.org/rfc/rfc6901
*
* @default /token Access the `token` property of the refresh response object
* @example / Access the root of the refresh response object, useful when your endpoint returns a plain, non-object string as the token
*/
refreshResponseTokenPointer?: string
Rizzato95 marked this conversation as resolved.
Show resolved Hide resolved
/**
* How to extract the authentication-token from the sign-in response.
*
Expand Down
Loading