mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-29 01:19:41 +05:00
feat: add authorization!
This commit is contained in:
parent
f3667eb209
commit
3a800a4933
7 changed files with 248 additions and 1 deletions
21
app/api/profile/[id]/route.js
Normal file
21
app/api/profile/[id]/route.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { fetchDataViaGet } from "@/app/api/utils";
|
||||
import { ENDPOINTS } from "@/app/api/config";
|
||||
|
||||
export async function GET(request, params) {
|
||||
const token = request.nextUrl.searchParams.get(["token"]) || null;
|
||||
let url = new URL(`${ENDPOINTS.profile}/${params["params"]["id"]}`);
|
||||
if (token) {
|
||||
url.searchParams.set("token", token);
|
||||
}
|
||||
|
||||
const response = await fetchDataViaGet(url.toString());
|
||||
if (!response) {
|
||||
return NextResponse.json({ message: "Server Error" }, { status: 500 });
|
||||
}
|
||||
if (!response.profile) {
|
||||
return NextResponse.json({ message: "Profile not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(response);
|
||||
}
|
15
app/api/profile/login/route.js
Normal file
15
app/api/profile/login/route.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { authorize } from "@/app/api/utils";
|
||||
import { ENDPOINTS } from "@/app/api/config";
|
||||
|
||||
export async function POST(request) {
|
||||
const response = await authorize(ENDPOINTS.auth, await request.json());
|
||||
if (!response) {
|
||||
return NextResponse.json({ message: "Server Error" }, { status: 500 });
|
||||
}
|
||||
if (!response.profile) {
|
||||
return NextResponse.json({ message: "Profile not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(response);
|
||||
}
|
|
@ -6,11 +6,11 @@ export async function GET(request) {
|
|||
const page = parseInt(request.nextUrl.searchParams.get(["page"])) || 0;
|
||||
const query = request.nextUrl.searchParams.get(["q"]) || null;
|
||||
const token = request.nextUrl.searchParams.get(["token"]) || null;
|
||||
let url = new URL(`${ENDPOINTS.search}/${page}`);
|
||||
if (token) {
|
||||
url.searchParams.set("token", token);
|
||||
}
|
||||
const data = { query, searchBy: 0 };
|
||||
let url = new URL(`${ENDPOINTS.search}/${page}`);
|
||||
|
||||
const response = await fetchDataViaPost(url.toString(), data);
|
||||
if (!response) {
|
||||
|
|
|
@ -9,6 +9,9 @@ export const fetchDataViaGet = async (url) => {
|
|||
const response = await fetch(url, {
|
||||
headers: HEADERS,
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error fetching data");
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
|
@ -23,9 +26,43 @@ export const fetchDataViaPost = async (url, body) => {
|
|||
headers: HEADERS,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error fetching data");
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const authorize = async (url, data) => {
|
||||
try {
|
||||
const response = await fetch(`${url}?login=${data.login}&password=${data.password}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"User-Agent": USER_AGENT,
|
||||
Sign: "9aa5c7af74e8cd70c86f7f9587bde23d",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error authorizing user");
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
export function setJWT(user_id, jwt) {
|
||||
const data = { jwt: jwt, user_id: user_id };
|
||||
localStorage.setItem("JWT", JSON.stringify(data));
|
||||
}
|
||||
export function getJWT() {
|
||||
const data = localStorage.getItem("JWT");
|
||||
return JSON.parse(data);
|
||||
}
|
||||
export function removeJWT() {
|
||||
localStorage.removeItem("JWT");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue