generated from muhtadeetaron/nextjs-template
fix(nav): fix exam flow navigation
chore(zustand): refactor auth code for zustand store
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { create } from "zustand";
|
||||
import { UserData } from "@/types/auth";
|
||||
import { LoginForm, RegisterForm, UserData } from "@/types/auth";
|
||||
import { API_URL } from "@/lib/auth";
|
||||
|
||||
// Cookie utilities
|
||||
@ -32,12 +32,19 @@ const setCookie = (
|
||||
}
|
||||
};
|
||||
|
||||
interface APIError extends Error {
|
||||
response?: any;
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
token: string | null;
|
||||
isLoading: boolean;
|
||||
hydrated: boolean;
|
||||
user: UserData | null;
|
||||
error: string | null;
|
||||
|
||||
login: (form: LoginForm) => Promise<void>;
|
||||
register: (form: RegisterForm) => Promise<void>;
|
||||
setToken: (token: string | null) => void;
|
||||
fetchUser: () => Promise<void>;
|
||||
logout: () => void;
|
||||
@ -48,6 +55,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
token: null,
|
||||
isLoading: true,
|
||||
hydrated: false,
|
||||
error: null,
|
||||
user: null,
|
||||
|
||||
setToken: (newToken) => {
|
||||
@ -55,6 +63,61 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
setCookie("authToken", newToken);
|
||||
},
|
||||
|
||||
login: async (form: LoginForm) => {
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/auth/login/`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || "Login failed");
|
||||
}
|
||||
|
||||
setCookie("authToken", data.token);
|
||||
set({ token: data.token, isLoading: false });
|
||||
} catch (err: any) {
|
||||
set({
|
||||
error: err?.message || "Login failed",
|
||||
isLoading: false,
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
register: async (form: RegisterForm) => {
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/auth/register/`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
const error: APIError = new Error(
|
||||
data?.detail || "Registration failed"
|
||||
);
|
||||
error.response = data;
|
||||
throw error;
|
||||
}
|
||||
|
||||
setCookie("authToken", data.token);
|
||||
set({ token: data.token, isLoading: false });
|
||||
} catch (err: any) {
|
||||
set({
|
||||
error: err?.message || "Registration failed",
|
||||
isLoading: false,
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
|
||||
fetchUser: async () => {
|
||||
const token = get().token;
|
||||
if (!token) return;
|
||||
@ -67,6 +130,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
if (!res.ok) throw new Error("Failed to fetch user info");
|
||||
|
||||
const data: UserData = await res.json();
|
||||
console.log(data);
|
||||
set({ user: data });
|
||||
} catch (err) {
|
||||
console.error("Error fetching user:", err);
|
||||
|
||||
Reference in New Issue
Block a user