mirror of
https://github.com/ramvignesh-b/pi-ku.git
synced 2026-05-04 15:56:56 +00:00
feat: implement authentication state management with Zustand and add request interceptor for JWT injection
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { create } from "zustand";
|
||||
import authApiClient from "../api/apiClient";
|
||||
|
||||
interface AuthState {
|
||||
accessToken: string | null;
|
||||
refreshToken: string | null;
|
||||
isAuthenticated: boolean;
|
||||
user: any | null;
|
||||
login: (credentials: any) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
}
|
||||
|
||||
export const useAuth = create<AuthState>((set) => ({
|
||||
accessToken: null,
|
||||
refreshToken: null,
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
login: async (credentials: any) => {
|
||||
const response = await authApiClient.post("login/", credentials);
|
||||
set({
|
||||
accessToken: response.data.access,
|
||||
refreshToken: response.data.refresh,
|
||||
isAuthenticated: true,
|
||||
user: response.data.user,
|
||||
});
|
||||
},
|
||||
logout: async () => {
|
||||
await authApiClient.post("logout/");
|
||||
set({
|
||||
accessToken: null,
|
||||
refreshToken: null,
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
});
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user