feat: implement authentication layout and pages, including login and registration forms, with validation and API integration

This commit is contained in:
2025-09-06 19:19:20 +02:00
parent e7cf76a0d8
commit c7cb3f537d
11 changed files with 572 additions and 157 deletions

View File

@@ -0,0 +1,40 @@
"use client";
import { createContext, useContext, useState, ReactNode } from "react";
import Cookies from "js-cookie";
interface AuthContextType {
token: string | null;
setToken: (token: string | null) => void;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: ReactNode }) {
const [token, setTokenState] = useState<string | null>(() => {
return Cookies.get("auth_token") || null;
});
const setToken = (newToken: string | null) => {
setTokenState(newToken);
if (newToken) {
Cookies.set("auth_token", newToken, { expires: 7, secure: true });
} else {
Cookies.remove("auth_token");
}
};
return (
<AuthContext.Provider value={{ token, setToken }}>
{children}
</AuthContext.Provider>
);
}
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error("useAuth must be used within an AuthProvider");
}
return context;
};