feat(auth): implement authentication structure
- Add auth helper functions (getAuth, postLogout) - Add postLogin API request function - Add useLogin custom hook with React Query - Update validation schema from email to username
This commit is contained in:
parent
2ea8d19c87
commit
d45f588fa7
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { QUERY_KEYS } from "@/utils/query-key";
|
||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
import { postLogin } from "./_requests";
|
||||||
|
import { LoginRequest, LoginResponse } from "@/types/auth";
|
||||||
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
|
export const useLogin = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [QUERY_KEYS.ADMIN_LOGIN],
|
||||||
|
mutationFn: (credentials: LoginRequest) => postLogin(credentials),
|
||||||
|
onSuccess: (response: LoginResponse) => {
|
||||||
|
localStorage.setItem("admin_token", response.tokens.access_token);
|
||||||
|
localStorage.setItem(
|
||||||
|
"admin_refresh_token",
|
||||||
|
response.tokens.refresh_token
|
||||||
|
);
|
||||||
|
localStorage.setItem("admin_user", JSON.stringify(response.admin_user));
|
||||||
|
localStorage.setItem(
|
||||||
|
"admin_permissions",
|
||||||
|
JSON.stringify(response.admin_user.permissions)
|
||||||
|
);
|
||||||
|
localStorage.setItem(
|
||||||
|
"admin_all_permissions",
|
||||||
|
JSON.stringify(response.permissions)
|
||||||
|
);
|
||||||
|
|
||||||
|
toast.success("ورود موفقیتآمیز بود");
|
||||||
|
},
|
||||||
|
onError: (error: any) => {
|
||||||
|
console.error("Login error:", error);
|
||||||
|
toast.error(error?.message || "خطا در ورود");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { httpPostRequest, APIUrlGenerator } from "@/utils/baseHttpService";
|
||||||
|
import { API_ROUTES } from "@/constant/routes";
|
||||||
|
import { LoginRequest, LoginResponse } from "@/types/auth";
|
||||||
|
|
||||||
|
export const postLogin = async (credentials: LoginRequest) => {
|
||||||
|
const response = await httpPostRequest<LoginResponse>(
|
||||||
|
APIUrlGenerator(API_ROUTES.ADMIN_LOGIN),
|
||||||
|
credentials
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const postLogout = () => {
|
||||||
|
localStorage.removeItem("admin_token");
|
||||||
|
localStorage.removeItem("admin_refresh_token");
|
||||||
|
localStorage.removeItem("admin_user");
|
||||||
|
localStorage.removeItem("admin_permissions");
|
||||||
|
localStorage.removeItem("admin_all_permissions");
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
export const getAuth = async () => {
|
||||||
|
const token = localStorage.getItem("admin_token");
|
||||||
|
const userStr = localStorage.getItem("admin_user");
|
||||||
|
|
||||||
|
if (token && userStr) {
|
||||||
|
try {
|
||||||
|
const user = JSON.parse(userStr);
|
||||||
|
return { token, user };
|
||||||
|
} catch (error) {
|
||||||
|
localStorage.removeItem("admin_token");
|
||||||
|
localStorage.removeItem("admin_refresh_token");
|
||||||
|
localStorage.removeItem("admin_user");
|
||||||
|
localStorage.removeItem("admin_permissions");
|
||||||
|
localStorage.removeItem("admin_all_permissions");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
|
|
||||||
export const loginSchema = yup.object({
|
export const loginSchema = yup.object({
|
||||||
email: yup
|
username: yup
|
||||||
.string()
|
.string()
|
||||||
.required("ایمیل الزامی است")
|
.required("نام کاربری الزامی است")
|
||||||
.email("فرمت ایمیل صحیح نیست"),
|
.min(3, "نام کاربری باید حداقل ۳ کاراکتر باشد"),
|
||||||
password: yup
|
password: yup
|
||||||
.string()
|
.string()
|
||||||
.required("رمز عبور الزامی است")
|
.required("رمز عبور الزامی است")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue