From ebad02a1e14cce1484428dfe20314be478cb73ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Preu=C3=9F?= Date: Sun, 19 Feb 2023 12:47:08 +0100 Subject: [PATCH] chore(release): v1.0.4 --- CHANGELOG.md | 2 ++ package.json | 2 +- src/runtime/composables/useAuth.ts | 11 +++++++++-- src/runtime/plugin.ts | 19 ++++++++++++------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 026c6fb..446e31c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog +## v1.0.4 + ## v1.0.3 ## v1.0.2 diff --git a/package.json b/package.json index 0df8d82..05a569c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitinflow/nuxt-oauth", - "version": "1.0.3", + "version": "1.0.4", "description": "Nuxt 3 OAuth Module", "license": "MIT", "type": "module", diff --git a/src/runtime/composables/useAuth.ts b/src/runtime/composables/useAuth.ts index e8b9418..a4a58ac 100644 --- a/src/runtime/composables/useAuth.ts +++ b/src/runtime/composables/useAuth.ts @@ -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,18 @@ 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, authConfig } } diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 6e9bf9e..fe4d23c 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -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) }