mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-04-20 03:50:20 +00:00
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m29s
95 lines
2.3 KiB
TypeScript
95 lines
2.3 KiB
TypeScript
"use server";
|
|
import type { TAuthUser, User } from "@/lib/types/user";
|
|
import axiosInstance from "@/utils/axiosInstance";
|
|
import { handleApiResponse } from "@/utils/tryCatch";
|
|
|
|
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.SARLINK_API_BASE_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
|
|
}
|
|
|
|
export async function checkIdOrPhone({
|
|
id_card,
|
|
phone_number,
|
|
}: { id_card?: string; phone_number?: string }) {
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/filter/?id_card=${id_card}&mobile=${phone_number}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
},
|
|
);
|
|
const data = await response.json();
|
|
return data;
|
|
}
|
|
|
|
type TSignupUser = Pick<
|
|
User,
|
|
"username" | "address" | "mobile" | "id_card" | "dob"
|
|
> & {
|
|
firstname: string;
|
|
lastname: string;
|
|
atoll: number;
|
|
island: number;
|
|
acc_no: string;
|
|
terms_accepted: boolean;
|
|
policy_accepted: boolean;
|
|
};
|
|
export async function backendRegister({ payload }: { payload: TSignupUser }) {
|
|
console.log("backendRegister payload", payload);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/auth/register/`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(payload),
|
|
},
|
|
);
|
|
console.log("backendRegister response", response);
|
|
return handleApiResponse<{ t_username: string }>(response, "backendRegister");
|
|
}
|