3 Commits

Author SHA1 Message Date
René Preuß
5454c9677b chore(release): v1.0.5 2023-04-08 11:39:35 +02:00
René Preuß
434c335e3f Add access token ref to useAuth return 2023-04-08 11:38:41 +02:00
René Preuß
ebad02a1e1 chore(release): v1.0.4 2023-02-19 12:47:08 +01:00
4 changed files with 27 additions and 10 deletions

View File

@@ -1,6 +1,10 @@
# Changelog
## v1.0.5
## v1.0.4
## v1.0.3
## v1.0.2

View File

@@ -1,6 +1,6 @@
{
"name": "@bitinflow/nuxt-oauth",
"version": "1.0.3",
"version": "1.0.5",
"description": "Nuxt 3 OAuth Module",
"license": "MIT",
"type": "module",

View File

@@ -58,7 +58,7 @@ export default async (options: ComposableOptions = {
return navigateTo(authConfig.redirect.logout)
}
const setBearer = async (token: string, tokenType: string, expires: number) => {
const setBearerToken = async (token: string, tokenType: string, expires: number) => {
accessToken.value = {token, tokenType, expiresAt: Date.now() + expires * 1000};
await fetchUser()
}
@@ -68,11 +68,19 @@ export default async (options: ComposableOptions = {
await fetchUser()
}
const bearerToken = () => {
return accessToken.value
? `${accessToken.value.tokenType} ${accessToken.value.token}`
: null;
}
return {
user,
signIn,
signOut,
setBearer,
setBearerToken,
bearerToken,
accessToken,
authConfig
}
}

View File

@@ -3,17 +3,22 @@ import useAuth from "./composables/useAuth"
export default defineNuxtPlugin(() => {
addRouteMiddleware('auth', async (to) => {
const {user, authConfig, setBearer} = await useAuth()
const {user, authConfig, setBearerToken} = await useAuth()
if (to.path === authConfig.redirect.callback) {
const params = new URLSearchParams(to.hash.substring(1))
const queryParams = new URLSearchParams(to.query.toString());
if (queryParams.has('error')) {
return navigateTo(authConfig.redirect.login)
}
if (params.has('access_token')) {
const token = params.get('access_token') as string;
const tokenType = params.get('token_type') as string;
const expires = params.get('expires_in') as string;
const hashParams = new URLSearchParams(to.hash.substring(1))
await setBearer(token, tokenType, parseInt(expires));
if (hashParams.has('access_token')) {
const token = hashParams.get('access_token') as string;
const tokenType = hashParams.get('token_type') as string;
const expires = hashParams.get('expires_in') as string;
await setBearerToken(token, tokenType, parseInt(expires));
return navigateTo(authConfig.redirect.home)
}