mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-04-20 03:50:20 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
"use server";
|
|
import type { TAuthUser } from "@/lib/types/user";
|
|
import axiosInstance from "@/utils/axiosInstance";
|
|
|
|
export async function login({
|
|
password,
|
|
username,
|
|
}: {
|
|
username: string;
|
|
password: string;
|
|
}): Promise<TAuthUser> {
|
|
const response = await axiosInstance
|
|
.post("/auth/login/", {
|
|
username: username,
|
|
password: password,
|
|
})
|
|
.then((res) => {
|
|
console.log(res);
|
|
return res.data; // Return the data from the response
|
|
})
|
|
.catch((err) => {
|
|
console.log(err.response);
|
|
throw err; // Throw the error to maintain the Promise rejection
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
export async function logout({ token }: { token: string }) {
|
|
const response = await fetch(
|
|
`${process.env.NEXT_PUBLIC_API_URL}/auth/logout/`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${token}`, // Include the token for authentication
|
|
},
|
|
},
|
|
);
|
|
|
|
if (response.status !== 204) {
|
|
throw new Error("Failed to log out from the backend");
|
|
}
|
|
console.log("logout res in backend", response);
|
|
|
|
// Since the API endpoint returns 204 No Content on success, we don't need to parse JSON
|
|
return null; // Return null to indicate a successful logout with no content
|
|
}
|