mirror of
https://github.com/Kizuren/QuizConnect.git
synced 2025-12-21 13:06:15 +01:00
Updated frontend to use the logout endpoints
This commit is contained in:
parent
eb36080bc1
commit
7208845369
2 changed files with 32 additions and 4 deletions
|
|
@ -29,11 +29,25 @@ export const login = async (pin: string) => {
|
|||
return response.data;
|
||||
};
|
||||
|
||||
export const logout = async (accessToken: string) => {
|
||||
const response = await api.delete('/user/logout', {
|
||||
data: { accessToken }
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export const adminLogin = async (loginId: string) => {
|
||||
const response = await api.post('/admin/login', { loginId });
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const adminLogout = async (accessToken: string) => {
|
||||
const response = await api.delete('/admin/logout', {
|
||||
data: { accessToken }
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// Admin endpoints
|
||||
export const getAdminQuestionSets = async () => {
|
||||
const response = await api.get('/admin/questionsets');
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
import { logout, adminLogout } from '../lib/api';
|
||||
|
||||
interface AuthState {
|
||||
token: string | null;
|
||||
|
|
@ -10,16 +11,29 @@ interface AuthState {
|
|||
|
||||
export const useAuthStore = create<AuthState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
(set, get) => ({
|
||||
token: null,
|
||||
isAdmin: false,
|
||||
setToken: (token, isAdmin) => {
|
||||
localStorage.setItem('token', token);
|
||||
set({ token, isAdmin });
|
||||
},
|
||||
logout: () => {
|
||||
logout: async () => {
|
||||
try {
|
||||
const token = get().token;
|
||||
if (token) {
|
||||
if (get().isAdmin) {
|
||||
await adminLogout(token);
|
||||
} else {
|
||||
await logout(token);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error during logout:', error);
|
||||
} finally {
|
||||
localStorage.removeItem('token');
|
||||
set({ token: null, isAdmin: false });
|
||||
}
|
||||
},
|
||||
}),
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue