implement admin panel with login functionality and dashboard layout
This commit is contained in:
43
admin/src/utils/loginUser.ts
Normal file
43
admin/src/utils/loginUser.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export type LoginSuccess = { success: true };
|
||||
export type LoginFailure = {
|
||||
success: false;
|
||||
message: string;
|
||||
description: string;
|
||||
};
|
||||
export type LoginResult = LoginSuccess | LoginFailure;
|
||||
|
||||
export const loginFunc = async (
|
||||
username: string,
|
||||
password: string
|
||||
): Promise<LoginResult> => {
|
||||
try {
|
||||
const response = await fetch("http://localhost:8002/api/loginAdmin", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Login failed!",
|
||||
description: "Invalid username or password.",
|
||||
};
|
||||
}
|
||||
|
||||
// Successful login
|
||||
const data = await response.json();
|
||||
Cookies.set("token", data.token);
|
||||
localStorage.setItem("userName", data.first_name);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Error logging in:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Login failed!",
|
||||
description: "Server error.",
|
||||
};
|
||||
}
|
||||
};
|
18
admin/src/utils/toastify.ts
Normal file
18
admin/src/utils/toastify.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { toast, Flip, type ToastOptions } from "react-toastify";
|
||||
|
||||
export type ToastType = "success" | "error" | "info" | "warning";
|
||||
|
||||
export const myToast = (message: string, msgType: ToastType) => {
|
||||
let config: ToastOptions = {
|
||||
position: "top-right",
|
||||
autoClose: 3000,
|
||||
hideProgressBar: false,
|
||||
closeOnClick: true,
|
||||
pauseOnHover: true,
|
||||
draggable: true,
|
||||
progress: undefined,
|
||||
theme: "colored",
|
||||
transition: Flip,
|
||||
};
|
||||
toast[msgType](message, config);
|
||||
};
|
Reference in New Issue
Block a user