fix: login url fragment and refresh token
This commit is contained in:
parent
7d16320dea
commit
62a4c3b1ca
8 changed files with 72 additions and 172 deletions
|
|
@ -1,36 +1,11 @@
|
|||
import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
|
||||
import axios from 'axios'
|
||||
import KeyCloakService from '@/services/KeyCloakService'
|
||||
import { getToken } from './KeyCloakService'
|
||||
|
||||
const HttpMethods = {
|
||||
GET: 'GET',
|
||||
POST: 'POST',
|
||||
DELETE: 'DELETE',
|
||||
}
|
||||
const instance = axios.create()
|
||||
|
||||
const _axios = axios.create()
|
||||
const cb = (config: InternalAxiosRequestConfig) => {
|
||||
config.headers.Authorization = `Bearer ${KeyCloakService.GetAccesToken()}`
|
||||
instance.interceptors.request.use(async (config) => {
|
||||
config.headers.Authorization = `Bearer ${await getToken()}`
|
||||
return config
|
||||
}
|
||||
})
|
||||
|
||||
const configureAxiosKeycloak = (): void => {
|
||||
_axios.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
|
||||
if (KeyCloakService.IsLoggedIn()) {
|
||||
KeyCloakService.UpdateToken(cb(config))
|
||||
}
|
||||
return config
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const getAxiosClient = (): AxiosInstance => _axios
|
||||
|
||||
const HttpService = {
|
||||
HttpMethods,
|
||||
configureAxiosKeycloak,
|
||||
getAxiosClient,
|
||||
}
|
||||
|
||||
export default HttpService
|
||||
export default instance
|
||||
|
|
|
|||
|
|
@ -1,77 +1,42 @@
|
|||
import Keycloak from 'keycloak-js'
|
||||
|
||||
const keycloakInstance = new Keycloak()
|
||||
const keycloak = new Keycloak()
|
||||
|
||||
interface CallbackOneParam<T1 = void, T2 = void> {
|
||||
(param1: T1): T2
|
||||
}
|
||||
/**
|
||||
* Initializes Keycloak instance and calls the provided callback function if successfully authenticated.
|
||||
*
|
||||
* @param onAuthenticatedCallback
|
||||
*/
|
||||
const Login = (onAuthenticatedCallback: CallbackOneParam): void => {
|
||||
keycloakInstance
|
||||
.init({ onLoad: 'login-required' })
|
||||
.then(function (authenticated) {
|
||||
authenticated ? onAuthenticatedCallback() : alert('non authenticated')
|
||||
})
|
||||
.catch((e) => {
|
||||
console.dir(e)
|
||||
console.log(`keycloak init exception: ${e}`)
|
||||
export async function login(cb?: (...args: any[]) => void) {
|
||||
const auth = await keycloak
|
||||
.init({
|
||||
onLoad: 'login-required',
|
||||
responseMode: 'query',
|
||||
checkLoginIframe: false,
|
||||
})
|
||||
.catch((e) => console.dir(e))
|
||||
|
||||
if (auth && cb) cb()
|
||||
}
|
||||
|
||||
const UserName = (): string | undefined =>
|
||||
keycloakInstance?.tokenParsed?.preferred_username
|
||||
export async function logout() {
|
||||
await keycloak.logout()
|
||||
}
|
||||
|
||||
const Token = (): string | undefined => keycloakInstance?.token
|
||||
const IdToken = (): string | undefined => keycloakInstance?.idToken
|
||||
export async function getToken() {
|
||||
await keycloak.updateToken(60).catch(() => login())
|
||||
return keycloak.token
|
||||
}
|
||||
|
||||
const LogOut = () => keycloakInstance.logout()
|
||||
export function getUsername(): string {
|
||||
return keycloak.tokenParsed?.preferred_username
|
||||
}
|
||||
|
||||
/*
|
||||
const UserRoles = (): string[] | undefined => {
|
||||
if (keycloakInstance.resourceAccess === undefined) return undefined;
|
||||
if (keycloakInstance.resourceAccess["express-client"] === undefined) return undefined;
|
||||
export function getRole(): string[] {
|
||||
const decoded = keycloak.tokenParsed
|
||||
|
||||
return keycloakInstance.resourceAccess["express-client"].roles;
|
||||
};
|
||||
*/
|
||||
const UserRoles = () => {
|
||||
const decoded = DecodeToken()
|
||||
|
||||
if (decoded && decoded.resource_access) {
|
||||
return decoded.resource_access[decoded.azp ?? ''].roles
|
||||
if (decoded && decoded.resource_access && decoded.azp) {
|
||||
return decoded.resource_access[decoded.azp].roles
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
const updateToken = (successCallback: any) =>
|
||||
keycloakInstance.updateToken(5).then(successCallback).catch(doLogin)
|
||||
|
||||
const doLogin = keycloakInstance.login
|
||||
|
||||
const isLoggedIn = () => !!keycloakInstance.token
|
||||
|
||||
const DecodeToken = () => {
|
||||
return keycloakInstance.tokenParsed
|
||||
export function isLoggedIn() {
|
||||
return !!keycloak.token
|
||||
}
|
||||
const DecodeIdToken = () => {
|
||||
return keycloakInstance.idTokenParsed
|
||||
}
|
||||
|
||||
const KeycloakService = {
|
||||
CallLogin: Login,
|
||||
GetUserName: UserName,
|
||||
GetAccesToken: Token,
|
||||
GetIdToken: IdToken,
|
||||
CallLogOut: LogOut,
|
||||
GetUserRoles: UserRoles,
|
||||
UpdateToken: updateToken,
|
||||
IsLoggedIn: isLoggedIn,
|
||||
GetDecodeToken: DecodeToken,
|
||||
GetDecodeIdToken: DecodeIdToken,
|
||||
}
|
||||
|
||||
export default KeycloakService
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue