-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from manchenkoff/33-token-based-auth
feat: added token storage support
- Loading branch information
Showing
18 changed files
with
280 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { FetchContext } from 'ofetch'; | ||
import type { ConsolaInstance } from 'consola'; | ||
import { type NuxtApp } from '#app'; | ||
import { useSanctumAppConfig } from '../../composables/useSanctumAppConfig'; | ||
|
||
/** | ||
* Use token in authentication header for the request | ||
* @param app Nuxt application instance | ||
* @param ctx Fetch context | ||
* @param logger Module logger instance | ||
*/ | ||
export default async function handleRequestTokenHeader( | ||
app: NuxtApp, | ||
ctx: FetchContext, | ||
logger: ConsolaInstance | ||
): Promise<void> { | ||
const appConfig = useSanctumAppConfig(); | ||
|
||
const token = await appConfig.tokenStorage!.get(app); | ||
|
||
if (!token) { | ||
logger.debug('Authentication token is not set in the storage'); | ||
return; | ||
} | ||
|
||
ctx.options.headers = { | ||
...ctx.options.headers, | ||
Authorization: `Bearer ${token}`, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useCookie, type NuxtApp } from '#app'; | ||
import { unref } from 'vue'; | ||
import type { TokenStorage } from '../types/config'; | ||
|
||
const cookieTokenKey = 'sanctum.token.cookie'; | ||
|
||
/** | ||
* Token storage using a secure cookie. | ||
* Works with both CSR/SSR modes. | ||
*/ | ||
export const cookieTokenStorage: TokenStorage = { | ||
async get(app: NuxtApp) { | ||
return await app.runWithContext(() => { | ||
const cookie = useCookie(cookieTokenKey, { readonly: true }); | ||
|
||
return unref(cookie.value) ?? undefined; | ||
}); | ||
}, | ||
async set(app: NuxtApp, token?: string) { | ||
await app.runWithContext(() => { | ||
const cookie = useCookie(cookieTokenKey, { secure: true }); | ||
|
||
cookie.value = token; | ||
}); | ||
}, | ||
}; |
Oops, something went wrong.