upd: api mobile
Deskripsi: - update api mobile register dan unregister token device No Issues
This commit is contained in:
@@ -98,7 +98,7 @@ model User {
|
||||
idVillage String
|
||||
Group Group @relation(fields: [idGroup], references: [id])
|
||||
idGroup String
|
||||
Position Position? @relation(fields: [idPosition], references: [id])
|
||||
Position Position? @relation(fields: [idPosition], references: [id])
|
||||
idPosition String?
|
||||
nik String @unique
|
||||
name String
|
||||
@@ -130,6 +130,17 @@ model User {
|
||||
Discussion Discussion[]
|
||||
DiscussionMember DiscussionMember[]
|
||||
DiscussionComment DiscussionComment[]
|
||||
TokenDeviceUser TokenDeviceUser[]
|
||||
}
|
||||
|
||||
model TokenDeviceUser {
|
||||
id String @id @default(cuid())
|
||||
User User @relation(fields: [idUser], references: [id])
|
||||
idUser String
|
||||
token String @db.Text
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model UserLog {
|
||||
|
||||
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 });
|
||||
}
|
||||
};
|
||||
@@ -1,41 +1,27 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import admin from 'firebase-admin';
|
||||
import { getApps, initializeApp, getApp } from 'firebase-admin/app';
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
if(!admin.apps.length){
|
||||
if (!admin.apps.length) {
|
||||
const serviceAccount = require("../../../../service_key.json");
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
});
|
||||
}
|
||||
|
||||
const firebaseConfig = {
|
||||
apiKey: "AIzaSyB2hbsW91J3oRQx4_jgrCCNY0tNt5-21e8",
|
||||
authDomain: "googleapis.com",
|
||||
projectId: "mobile-darmasaba",
|
||||
storageBucket: "mobile-darmasaba.appspot.com",
|
||||
messagingSenderId: "867439221179",
|
||||
appId: "1:867439221179:android:4509f77478c8dce99b0c9e",
|
||||
databaseURL: "https://mobile-darmasaba-default-rtdb.asia-southeast1.firebasedatabase.app/",
|
||||
measurementId: "G-1015652111565"
|
||||
}
|
||||
|
||||
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
|
||||
|
||||
const message = {
|
||||
notification: {
|
||||
title: 'Dari API',
|
||||
body: 'Hello World',
|
||||
title: 'Dari API',
|
||||
body: 'Hello World',
|
||||
},
|
||||
token: 'fPfBeTn4R4KsHhDw-8Edj0:APA91bGBjguuZsMhisJua_Wa3m7z7vBCE08vjyDBScmN0eIRgpfINlDx4SI6-upn-rr6tTAcoPxeQkxxbEsohcUbbV5DjUyIG4xR6wuvKOJMp3Mr4rNWFv8',
|
||||
};
|
||||
|
||||
const response = await admin.messaging().send(message);
|
||||
console.log(response)
|
||||
};
|
||||
|
||||
return NextResponse.json({ success: true, message: "Sukses" }, { status: 200 });
|
||||
const response = await admin.messaging().send(message);
|
||||
|
||||
return NextResponse.json({ success: true, message: `Sukses ${admin.apps.length}--${response}` }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal", reason: (error as Error).message, }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user