mirror of
https://github.com/bitinflow/nuxt-oauth.git
synced 2026-03-13 21:56:00 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebad02a1e1 | ||
|
|
0981a12d08 | ||
|
|
36ccf819bd |
@@ -1,6 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
|
||||
## v1.0.4
|
||||
|
||||
## v1.0.3
|
||||
|
||||
## v1.0.2
|
||||
|
||||
## v1.0.0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@bitinflow/nuxt-oauth",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.4",
|
||||
"description": "Nuxt 3 OAuth Module",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
|
||||
@@ -12,7 +12,7 @@ export interface ModuleOptions {
|
||||
endpoints: {
|
||||
authorization: string,
|
||||
userInfo: string,
|
||||
logout: string
|
||||
logout: string | null
|
||||
},
|
||||
clientId: string,
|
||||
scope: string[]
|
||||
@@ -28,7 +28,7 @@ const defaults: ModuleOptions = {
|
||||
endpoints: {
|
||||
authorization: 'https://accounts.bitinflow.com/oauth/authorize',
|
||||
userInfo: `https://accounts.bitinflow.com/api/v3/user`,
|
||||
logout: 'https://accounts.bitinflow.com/logout'
|
||||
logout: null,
|
||||
},
|
||||
clientId: 'please-set-client-id',
|
||||
scope: ['user:read']
|
||||
|
||||
@@ -45,10 +45,20 @@ export default async (options: ComposableOptions = {
|
||||
accessToken.value = null;
|
||||
user.value = null;
|
||||
|
||||
return navigateTo(authConfig.endpoints.logout || authConfig.redirect.logout)
|
||||
if (authConfig.endpoints.logout) {
|
||||
// create oauth logout url
|
||||
const params = new URLSearchParams({
|
||||
client_id: authConfig.clientId,
|
||||
redirect_uri: window.location.origin + authConfig.redirect.logout
|
||||
})
|
||||
|
||||
window.location.href = `${authConfig.endpoints.logout}?${params.toString()}`
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -58,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user