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;
|
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) => {
|
export const adminLogin = async (loginId: string) => {
|
||||||
const response = await api.post('/admin/login', { loginId });
|
const response = await api.post('/admin/login', { loginId });
|
||||||
return response.data;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const adminLogout = async (accessToken: string) => {
|
||||||
|
const response = await api.delete('/admin/logout', {
|
||||||
|
data: { accessToken }
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
};
|
||||||
|
|
||||||
// Admin endpoints
|
// Admin endpoints
|
||||||
export const getAdminQuestionSets = async () => {
|
export const getAdminQuestionSets = async () => {
|
||||||
const response = await api.get('/admin/questionsets');
|
const response = await api.get('/admin/questionsets');
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
import { persist } from 'zustand/middleware';
|
import { persist } from 'zustand/middleware';
|
||||||
|
import { logout, adminLogout } from '../lib/api';
|
||||||
|
|
||||||
interface AuthState {
|
interface AuthState {
|
||||||
token: string | null;
|
token: string | null;
|
||||||
|
|
@ -10,16 +11,29 @@ interface AuthState {
|
||||||
|
|
||||||
export const useAuthStore = create<AuthState>()(
|
export const useAuthStore = create<AuthState>()(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set, get) => ({
|
||||||
token: null,
|
token: null,
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
setToken: (token, isAdmin) => {
|
setToken: (token, isAdmin) => {
|
||||||
localStorage.setItem('token', token);
|
localStorage.setItem('token', token);
|
||||||
set({ token, isAdmin });
|
set({ token, isAdmin });
|
||||||
},
|
},
|
||||||
logout: () => {
|
logout: async () => {
|
||||||
localStorage.removeItem('token');
|
try {
|
||||||
set({ token: null, isAdmin: false });
|
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