upd: api mobile
Deskripsi: - update api mobile register dan unregister token device No Issues
This commit is contained in:
61
src/app/api/mobile/auth-token/route.ts
Normal file
61
src/app/api/mobile/auth-token/route.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { token, user } = (await request.json());
|
||||
|
||||
const userMobile = await funGetUserById({ id: user })
|
||||
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||
}
|
||||
|
||||
const cek = await prisma.tokenDeviceUser.count({
|
||||
where: {
|
||||
idUser: userMobile.id,
|
||||
token
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
const data = await prisma.tokenDeviceUser.create({
|
||||
data: {
|
||||
token,
|
||||
idUser: userMobile.id
|
||||
}
|
||||
});
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendaftarkan token" }, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, message: "Token sudah terdaftar" }, { status: 200 })
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menambahkan token, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const { token, user } = (await request.json());
|
||||
|
||||
const userMobile = await funGetUserById({ id: user })
|
||||
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||
}
|
||||
|
||||
const data = await prisma.tokenDeviceUser.deleteMany({
|
||||
where: {
|
||||
token,
|
||||
idUser: userMobile.id
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil menghapus token", }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menghapus token, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user