feat : add api
This commit is contained in:
18
src/app/api/auth/get-user-by-cookies/route.ts
Normal file
18
src/app/api/auth/get-user-by-cookies/route.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { prisma, pwd_key_config } from "@/module/_global";
|
||||
import { unsealData } from "iron-session";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET() {
|
||||
const sessionCookie = cookies().get("sessionCookie");
|
||||
const userId = await unsealData(sessionCookie!.value, {
|
||||
password: pwd_key_config,
|
||||
});
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: String(userId),
|
||||
},
|
||||
});
|
||||
|
||||
return Response.json(user);
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
import prisma from "@/module/_global/bin/prisma";
|
||||
import { Login } from "@/types/auth/login";
|
||||
|
||||
import { prisma } from "@/module/_global";
|
||||
import { ILogin } from "@/types";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { email }: Login = await req.json();
|
||||
const { phone }: ILogin = await req.json();
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email, isActive: true },
|
||||
where: { phone, isActive: true },
|
||||
select: { id: true, phone: true },
|
||||
});
|
||||
|
||||
|
||||
7
src/app/api/auth/logout/route.ts
Normal file
7
src/app/api/auth/logout/route.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function DELETE() {
|
||||
cookies().delete('sessionCookie')
|
||||
|
||||
return Response.json({ success: true })
|
||||
}
|
||||
16
src/app/api/auth/set-cookies/route.ts
Normal file
16
src/app/api/auth/set-cookies/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { pwd_key_config } from "@/module/_global";
|
||||
import { sealData } from "iron-session";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const { user } = await req.json();
|
||||
const encryptedUserData = await sealData(user, { password: pwd_key_config });
|
||||
|
||||
cookies().set({
|
||||
name: "sessionCookie",
|
||||
value: encryptedUserData,
|
||||
});
|
||||
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
Reference in New Issue
Block a user