feat
Deskripsi: - Create validasi - Create register - create global prisma, color tune,dan global state
This commit is contained in:
20
src/app/api/auth/login/route.ts
Normal file
20
src/app/api/auth/login/route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { MyConsole } from "@/app/fun/my_console";
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (req.method === "POST") {
|
||||
const body = await req.json();
|
||||
MyConsole(body);
|
||||
|
||||
try {
|
||||
await fetch(
|
||||
`https://wa.wibudev.com/code?nom=${body.nomor}&text=${body.otp}`
|
||||
);
|
||||
return NextResponse.json({ body, status: 200, message: "Login Success" });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ status: 500, message: "Server Error !!!" });
|
||||
}
|
||||
}
|
||||
return NextResponse.json({ success: false });
|
||||
}
|
||||
12
src/app/api/auth/logout/route.ts
Normal file
12
src/app/api/auth/logout/route.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET() {
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: "",
|
||||
maxAge: 0,
|
||||
});
|
||||
|
||||
return NextResponse.json({ status: 200, message: "Logout" });
|
||||
}
|
||||
55
src/app/api/auth/register/route.ts
Normal file
55
src/app/api/auth/register/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { sealData } from "iron-session";
|
||||
import { MyConsole } from "@/app/fun/my_console";
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { data } from "autoprefixer";
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (req.method === "POST") {
|
||||
const body = await req.json();
|
||||
// MyConsole(body);
|
||||
|
||||
const cekUsername = await prisma.user.findUnique({
|
||||
where: {
|
||||
username: body.username,
|
||||
},
|
||||
});
|
||||
|
||||
MyConsole(cekUsername);
|
||||
|
||||
if (cekUsername)
|
||||
return NextResponse.json({ status: 400, message: "Username sudah ada" });
|
||||
|
||||
const data = await prisma.user.create({
|
||||
data: {
|
||||
username: body.username,
|
||||
nomor: body.nomor,
|
||||
},
|
||||
});
|
||||
|
||||
if (data) {
|
||||
const seal = await sealData(
|
||||
JSON.stringify({
|
||||
id: data.id,
|
||||
username: data.username,
|
||||
}),
|
||||
{
|
||||
password: process.env.PWD as string,
|
||||
}
|
||||
);
|
||||
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: seal,
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
});
|
||||
|
||||
|
||||
return NextResponse.json({ status: 201});
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
return NextResponse.json({ success: false });
|
||||
}
|
||||
52
src/app/api/auth/validasi/route.ts
Normal file
52
src/app/api/auth/validasi/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { MyConsole } from "@/app/fun/my_console";
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { sealData, unsealData } from "iron-session";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (req.method === "POST") {
|
||||
const body = await req.json();
|
||||
MyConsole(body);
|
||||
|
||||
const data = await prisma.user.findUnique({
|
||||
where: {
|
||||
nomor: body.nomor,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
nomor: true,
|
||||
username: true,
|
||||
active: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) return NextResponse.json({ status: 404 });
|
||||
|
||||
if (data) {
|
||||
const res = await sealData(
|
||||
JSON.stringify({
|
||||
id: data.id,
|
||||
username: data.username,
|
||||
}),
|
||||
{
|
||||
password: process.env.PWD as string,
|
||||
}
|
||||
);
|
||||
|
||||
const un = await unsealData(res, { password: process.env.PWD as string });
|
||||
// console.log(JSON.stringify(un), "route validasi")
|
||||
|
||||
cookies().set({
|
||||
name: "ssn",
|
||||
value: res,
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
});
|
||||
|
||||
return NextResponse.json({ status: 200, data });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
return NextResponse.json({ success: false });
|
||||
}
|
||||
Reference in New Issue
Block a user