mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-30 04:08:12 +00:00
fix: change the me endpoint to useQuery api
This commit is contained in:
@@ -4,6 +4,6 @@ import { baseQueryWithReauth } from './base-query-with-reauth';
|
||||
export const baseApi = createApi({
|
||||
reducerPath: 'baseApi',
|
||||
baseQuery: baseQueryWithReauth,
|
||||
tagTypes: ['Api'],
|
||||
tagTypes: ['Api', 'Me'],
|
||||
endpoints: () => ({}),
|
||||
});
|
||||
|
||||
@@ -22,11 +22,15 @@ const queryApi = baseApi.injectEndpoints({
|
||||
headers,
|
||||
}),
|
||||
}),
|
||||
getMe: builder.query<unknown, void>({
|
||||
query: () => '/auth/me',
|
||||
providesTags: ['Me'],
|
||||
}),
|
||||
}),
|
||||
overrideExisting: false,
|
||||
});
|
||||
|
||||
export const { useApiQueryQuery, useApiMutationMutation } = queryApi;
|
||||
export const { useApiQueryQuery, useApiMutationMutation, useGetMeQuery } = queryApi;
|
||||
|
||||
export function useApiQuery<TData = unknown>(
|
||||
args: ApiQueryArgs,
|
||||
|
||||
@@ -5,11 +5,7 @@ export const SESSION_HEADER_KEYS = {
|
||||
currentProjectId: 'x-current-project-id',
|
||||
} as const;
|
||||
|
||||
const TOKEN_STORAGE_KEYS = [
|
||||
'ema-backoffice-auth-token',
|
||||
'ema-portal-auth-token',
|
||||
'auth-token',
|
||||
] as const;
|
||||
const TOKEN_STORAGE_KEYS = ['auth-token'] as const;
|
||||
|
||||
export function resolveTokenFromStorage(): string | undefined {
|
||||
for (const key of TOKEN_STORAGE_KEYS) {
|
||||
|
||||
@@ -8,6 +8,6 @@ export { ForgotPasswordPage } from './lib/pages/ForgotPasswordPage';
|
||||
export { OTPVerificationPage } from './lib/pages/OTPVerificationPage';
|
||||
export { authReducer, loginSuccess, setUser, setCurrentProfile, clearCurrentProfile, logout, hydrateAuth } from './lib/store/auth.slice';
|
||||
export { signupReducer, setSignupData, setSignupStep, resetSignup } from './lib/store/signup.slice';
|
||||
export { configureAuthStorage, authStorage } from './lib/utils/auth-storage';
|
||||
export { authStorage } from './lib/utils/auth-storage';
|
||||
export { refreshAccessToken } from './lib/utils/refresh-token';
|
||||
export type { AuthUser, AuthState, LoginPayload, CurrentProfile, CurrentProfileAddress, CurrentProfileProfession } from './lib/types/auth.types';
|
||||
|
||||
@@ -1,39 +1,29 @@
|
||||
let _prefix = 'ema-auth';
|
||||
|
||||
export function configureAuthStorage(prefix: string) {
|
||||
_prefix = prefix;
|
||||
}
|
||||
|
||||
function key(k: string) {
|
||||
return `${_prefix}-${k}`;
|
||||
}
|
||||
|
||||
export const authStorage = {
|
||||
getToken: () => localStorage.getItem(key('auth-token')) ?? undefined,
|
||||
setToken: (token: string) => localStorage.setItem(key('auth-token'), token),
|
||||
getRefreshToken: () => localStorage.getItem(key('refresh-token')) ?? undefined,
|
||||
setRefreshToken: (t: string) => localStorage.setItem(key('refresh-token'), t),
|
||||
getToken: () => localStorage.getItem('auth-token') ?? undefined,
|
||||
setToken: (token: string) => localStorage.setItem('auth-token', token),
|
||||
getRefreshToken: () => localStorage.getItem('refresh-token') ?? undefined,
|
||||
setRefreshToken: (t: string) => localStorage.setItem('refresh-token', t),
|
||||
getUser: <T = unknown>(): T | null => {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(key('auth-user')) ?? 'null') as T | null;
|
||||
return JSON.parse(localStorage.getItem('auth-user') ?? 'null') as T | null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
setUser: <T>(u: T) => localStorage.setItem(key('auth-user'), JSON.stringify(u)),
|
||||
getProfileId: () => localStorage.getItem(key('profile-id')) ?? undefined,
|
||||
setProfileId: (id: string) => localStorage.setItem(key('profile-id'), id),
|
||||
setUser: <T>(u: T) => localStorage.setItem('auth-user', JSON.stringify(u)),
|
||||
getProfileId: () => localStorage.getItem('profile-id') ?? undefined,
|
||||
setProfileId: (id: string) => localStorage.setItem('profile-id', id),
|
||||
getProfile: <T = unknown>(): T | null => {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(key('current-profile')) ?? 'null') as T | null;
|
||||
return JSON.parse(localStorage.getItem('current-profile') ?? 'null') as T | null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
setProfile: <T>(p: T) => localStorage.setItem(key('current-profile'), JSON.stringify(p)),
|
||||
removeProfile: () => localStorage.removeItem(key('current-profile')),
|
||||
setProfile: <T>(p: T) => localStorage.setItem('current-profile', JSON.stringify(p)),
|
||||
removeProfile: () => localStorage.removeItem('current-profile'),
|
||||
clear: () => {
|
||||
[key('auth-token'), key('refresh-token'), key('auth-user'), key('profile-id'), key('current-profile')].forEach((k) =>
|
||||
['auth-token', 'refresh-token', 'auth-user', 'profile-id', 'current-profile'].forEach((k) =>
|
||||
localStorage.removeItem(k),
|
||||
);
|
||||
document.cookie =
|
||||
|
||||
Reference in New Issue
Block a user