mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 04:20:12 +02:00
Update login flow
This commit is contained in:
@@ -18,7 +18,7 @@ export const useLogin = (
|
||||
onSuccess: (res) => {
|
||||
const props = {
|
||||
accessToken: res.data.accessToken,
|
||||
permissions: { isAdmin: res.data.isAdmin },
|
||||
permissions: { isAdmin: res.data.isAdmin, username: res.data.username },
|
||||
refreshToken: res.data.refreshToken,
|
||||
serverKey: md5(serverUrl),
|
||||
serverUrl,
|
||||
|
||||
@@ -12,7 +12,7 @@ export const ax = Axios.create({
|
||||
ax.interceptors.request.use(
|
||||
(config) => {
|
||||
const { state } = JSON.parse(
|
||||
localStorage.getItem('authentication') || '{}'
|
||||
localStorage.getItem('store_authentication') || '{}'
|
||||
);
|
||||
|
||||
config.baseURL = `${state.serverUrl}/api`;
|
||||
@@ -34,7 +34,9 @@ ax.interceptors.response.use(
|
||||
async (err) => {
|
||||
if (err.response && err.response.status === 401) {
|
||||
const { config } = err;
|
||||
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
||||
const auth = JSON.parse(
|
||||
localStorage.getItem('store_authentication') || '{}'
|
||||
);
|
||||
|
||||
if (err.response.data.error.message === 'jwt expired' && !config.sent) {
|
||||
config.sent = true;
|
||||
@@ -46,7 +48,7 @@ ax.interceptors.response.use(
|
||||
).data;
|
||||
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
'store_authentication',
|
||||
JSON.stringify({ ...auth, state: { ...auth.state, accessToken } })
|
||||
);
|
||||
|
||||
@@ -59,7 +61,7 @@ ax.interceptors.response.use(
|
||||
}
|
||||
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
'store_authentication',
|
||||
JSON.stringify({
|
||||
...auth,
|
||||
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||
@@ -68,7 +70,7 @@ ax.interceptors.response.use(
|
||||
|
||||
if (err.response.data.error.message === 'No auth token') {
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
'store_authentication',
|
||||
JSON.stringify({
|
||||
...auth,
|
||||
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||
|
||||
@@ -8,6 +8,7 @@ export interface AuthState {
|
||||
currentServer?: Server;
|
||||
permissions: {
|
||||
isAdmin: boolean;
|
||||
username: string;
|
||||
};
|
||||
refreshToken: string;
|
||||
serverCredentials: {
|
||||
@@ -37,10 +38,6 @@ export interface AuthSlice extends AuthState {
|
||||
setCurrentServer: (server: Server) => void;
|
||||
}
|
||||
|
||||
const persistedState = JSON.parse(
|
||||
localStorage.getItem('authentication') || '{}'
|
||||
);
|
||||
|
||||
export const useAuthStore = create<AuthSlice>()(
|
||||
persist(
|
||||
devtools(
|
||||
@@ -92,12 +89,13 @@ export const useAuthStore = create<AuthSlice>()(
|
||||
logout: () => {
|
||||
return set({
|
||||
accessToken: undefined,
|
||||
permissions: { isAdmin: false },
|
||||
permissions: { isAdmin: false, username: '' },
|
||||
refreshToken: undefined,
|
||||
});
|
||||
},
|
||||
permissions: {
|
||||
isAdmin: false,
|
||||
username: '',
|
||||
},
|
||||
refreshToken: '',
|
||||
serverCredentials: [],
|
||||
@@ -105,20 +103,11 @@ export const useAuthStore = create<AuthSlice>()(
|
||||
serverPermissions: '',
|
||||
serverUrl: '',
|
||||
setCurrentServer: (server: Server) => {
|
||||
const prev = JSON.parse(
|
||||
localStorage.getItem('authentication') || '{}'
|
||||
);
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
JSON.stringify({
|
||||
...prev,
|
||||
state: { ...prev.state, currentServer: server },
|
||||
})
|
||||
);
|
||||
return set({ currentServer: server });
|
||||
},
|
||||
}))
|
||||
})),
|
||||
{ name: 'authentication' }
|
||||
),
|
||||
{ name: 'authentication' }
|
||||
{ name: 'store_authentication' }
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user