mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-02 09:48:22 +00:00
refactor: update authentication flow to use NextAuth, replace better-auth with axios for API calls, and clean up unused code
This commit is contained in:
@ -1,10 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import type { Atoll, DataResponse } from "@/lib/backend-types";
|
||||
|
||||
export async function getAtollsWithIslands(): Promise<DataResponse<Atoll>> {
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/auth/atolls`,
|
||||
);
|
||||
return response.json();
|
||||
}
|
48
queries/authentication.ts
Normal file
48
queries/authentication.ts
Normal file
@ -0,0 +1,48 @@
|
||||
"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
|
||||
}
|
Reference in New Issue
Block a user