Skip to content

Commit

Permalink
Merge pull request #3 from kurorido/allow-config-response-token-key
Browse files Browse the repository at this point in the history
add option for token key in login response
  • Loading branch information
hkp22 authored Oct 28, 2024
2 parents 9f889b4 + 660b1f3 commit 31740a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineNuxtModule<DeepPartial<ModuleOptions>>({
token: {
storageKey: 'AUTH_TOKEN',
provider: 'cookie',
responseKey: 'token',
},
fetchClientOptions: {
retryAttempts: false,
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/composables/useSanctum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export const useSanctum = <T>() => {

// Handle token or cookie auth
if (authMode === 'token') {
await useTokenStorage(nuxtApp).set(response.token);
const { token } = options;
const tokenValue = token.responseKey
.split('.')
.reduce((acc, key) => acc && acc[key], response);
await useTokenStorage(nuxtApp).set(tokenValue);
}

await refreshUser();
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/types/ModuleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export interface ModuleOptions {
*/
storageKey: string;

/**
* The key present the token in the login response.
* @default 'token'
*/
responseKey: string;

/**
* The storage provider to use for the token.
* @default 'cookie'
Expand Down

0 comments on commit 31740a5

Please sign in to comment.