feat: implement authentication flow with JWT refresh logic, Login page, and user session management

This commit is contained in:
Your Name
2026-04-10 19:24:15 +05:30
parent c4733249fa
commit 7748cd10c9
7 changed files with 205 additions and 30 deletions
+39 -11
View File
@@ -1,36 +1,64 @@
import { create } from "zustand";
import authApiClient from "../api/apiClient";
interface UserProfile {
public_id: string;
email: string;
full_name: string;
}
interface AuthState {
accessToken: string | null;
refreshToken: string | null;
isAuthenticated: boolean;
user: any | null;
user: UserProfile | null;
isInitializing: boolean; // refresh in transit
login: (credentials: any) => Promise<void>;
logout: () => Promise<void>;
checkAuth: () => Promise<void>;
}
export const useAuth = create<AuthState>((set) => ({
accessToken: null,
refreshToken: null,
isAuthenticated: false,
user: null,
isInitializing: true,
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,
});
try {
await authApiClient.post("logout/");
} finally {
set({
accessToken: null,
isAuthenticated: false,
user: null,
});
}
},
checkAuth: async () => {
try {
const response = await authApiClient.get("me/");
set({
user: response.data,
isAuthenticated: true,
});
} catch (err) {
console.error("Check auth error:", err);
set({
user: null,
isAuthenticated: false,
});
} finally {
set({ isInitializing: false });
}
},
}));