7 changed files with 108 additions and 60 deletions
@ -1,53 +0,0 @@ |
|||||
import { useAppConfig } from '@vben/hooks'; |
|
||||
|
|
||||
import { UserManager, WebStorageStateStore } from 'oidc-client-ts'; |
|
||||
|
|
||||
const { authority, audience, clientId, clientSecret, disablePKCE } = |
|
||||
useAppConfig(import.meta.env, import.meta.env.PROD); |
|
||||
|
|
||||
const userManager = new UserManager({ |
|
||||
authority, |
|
||||
client_id: clientId, |
|
||||
client_secret: clientSecret, |
|
||||
redirect_uri: `${window.location.origin}/signin-callback`, |
|
||||
response_type: 'code', |
|
||||
scope: audience, |
|
||||
post_logout_redirect_uri: `${window.location.origin}/`, |
|
||||
silent_redirect_uri: `${window.location.origin}/silent-renew.html`, |
|
||||
automaticSilentRenew: true, |
|
||||
loadUserInfo: true, |
|
||||
userStore: new WebStorageStateStore({ store: window.localStorage }), |
|
||||
disablePKCE, |
|
||||
}); |
|
||||
|
|
||||
export default { |
|
||||
async login() { |
|
||||
return userManager.signinRedirect(); |
|
||||
}, |
|
||||
|
|
||||
async logout() { |
|
||||
return userManager.signoutRedirect(); |
|
||||
}, |
|
||||
|
|
||||
async refreshToken() { |
|
||||
return userManager.signinSilent(); |
|
||||
}, |
|
||||
|
|
||||
async getAccessToken() { |
|
||||
const user = await userManager.getUser(); |
|
||||
return user?.access_token; |
|
||||
}, |
|
||||
|
|
||||
async isAuthenticated() { |
|
||||
const user = await userManager.getUser(); |
|
||||
return !!user && !user.expired; |
|
||||
}, |
|
||||
|
|
||||
async handleCallback() { |
|
||||
return userManager.signinRedirectCallback(); |
|
||||
}, |
|
||||
|
|
||||
async getUser() { |
|
||||
return userManager.getUser(); |
|
||||
}, |
|
||||
}; |
|
||||
@ -1 +1,2 @@ |
|||||
export * from './useOAuthError'; |
export * from './useOAuthError'; |
||||
|
export * from './useOidcClient'; |
||||
|
|||||
@ -0,0 +1,43 @@ |
|||||
|
import { userManager } from '../utils/auth'; |
||||
|
|
||||
|
export function useOidcClient() { |
||||
|
async function login() { |
||||
|
return userManager.signinRedirect(); |
||||
|
} |
||||
|
|
||||
|
async function logout() { |
||||
|
return userManager.signoutRedirect(); |
||||
|
} |
||||
|
|
||||
|
async function refreshToken() { |
||||
|
return userManager.signinSilent(); |
||||
|
} |
||||
|
|
||||
|
async function getAccessToken() { |
||||
|
const user = await userManager.getUser(); |
||||
|
return user?.access_token; |
||||
|
} |
||||
|
|
||||
|
async function isAuthenticated() { |
||||
|
const user = await userManager.getUser(); |
||||
|
return !!user && !user.expired; |
||||
|
} |
||||
|
|
||||
|
async function handleCallback() { |
||||
|
return userManager.signinRedirectCallback(); |
||||
|
} |
||||
|
|
||||
|
async function getUser() { |
||||
|
return userManager.getUser(); |
||||
|
} |
||||
|
|
||||
|
return { |
||||
|
login, |
||||
|
logout, |
||||
|
refreshToken, |
||||
|
getAccessToken, |
||||
|
isAuthenticated, |
||||
|
handleCallback, |
||||
|
getUser, |
||||
|
}; |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
import { useAppConfig } from '@vben/hooks'; |
||||
|
|
||||
|
import { UserManager, WebStorageStateStore } from 'oidc-client-ts'; |
||||
|
import SecureLS from 'secure-ls'; |
||||
|
|
||||
|
const { authority, audience, clientId, clientSecret, disablePKCE } = |
||||
|
useAppConfig(import.meta.env, import.meta.env.PROD); |
||||
|
|
||||
|
const env = import.meta.env.PROD ? 'prod' : 'dev'; |
||||
|
const appVersion = import.meta.env.VITE_APP_VERSION; |
||||
|
const namespace = `${import.meta.env.VITE_APP_NAMESPACE}-${appVersion}-${env}`; |
||||
|
|
||||
|
const ls = new SecureLS({ |
||||
|
encodingType: 'aes', |
||||
|
encryptionSecret: import.meta.env.VITE_APP_STORE_SECURE_KEY, |
||||
|
isCompression: true, |
||||
|
// @ts-ignore secure-ls does not have a type definition for this
|
||||
|
metaKey: `${namespace}-secure-oidc`, |
||||
|
}); |
||||
|
export const userManager = new UserManager({ |
||||
|
authority, |
||||
|
client_id: clientId, |
||||
|
client_secret: clientSecret, |
||||
|
redirect_uri: `${window.location.origin}/signin-callback`, |
||||
|
response_type: 'code', |
||||
|
scope: audience, |
||||
|
post_logout_redirect_uri: `${window.location.origin}/`, |
||||
|
silent_redirect_uri: `${window.location.origin}/silent-renew.html`, |
||||
|
automaticSilentRenew: true, |
||||
|
loadUserInfo: true, |
||||
|
userStore: new WebStorageStateStore({ |
||||
|
store: import.meta.env.DEV |
||||
|
? localStorage |
||||
|
: { |
||||
|
length: ls.storage.length, |
||||
|
clear: ls.clear, |
||||
|
setItem(key, value) { |
||||
|
ls.set(key, value); |
||||
|
}, |
||||
|
getItem(key) { |
||||
|
return ls.get(key); |
||||
|
}, |
||||
|
key(index) { |
||||
|
const keys = ls.getAllKeys(); |
||||
|
return keys[index] ?? null; |
||||
|
}, |
||||
|
removeItem(key) { |
||||
|
ls.remove(key); |
||||
|
}, |
||||
|
}, |
||||
|
}), |
||||
|
disablePKCE, |
||||
|
}); |
||||
@ -0,0 +1 @@ |
|||||
|
export * from './auth'; |
||||
Loading…
Reference in new issue