Skip to content

Commit

Permalink
Merge branch 'main' into feature/optional-get-session-on-sign-in
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfactory-frank-spee authored Nov 29, 2024
2 parents d5c28ef + 218a846 commit 32f3e80
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/guide/advanced/deployment/vercel.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This variable is avalible at both build and run-time. Therefore you can referenc
export default defineNuxtConfig({
modules: ['@sidebase/nuxt-auth'],
auth: {
baseURL: process.env.VERCEL_URL ? `https://${VERCEL_URL}/api/auth` : undefined
baseURL: process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}/api/auth` : undefined
}
})
```
Expand Down
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 @@ -202,11 +202,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

0 comments on commit 32f3e80

Please sign in to comment.