Skip to content

Commit

Permalink
feat: token 저장 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongSH1 committed Dec 6, 2023
1 parent 65b95ae commit 96b4027
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db/get-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export async function getTokenByUserId(userId) {
return null; // 해당 userId에 대한 데이터가 없을 경우
}
} catch (err) {
throw err;
console.error(err);
}
}
13 changes: 13 additions & 0 deletions db/save-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { connection } from "./connection.js";

export async function saveToken(userId, token) {
try {
// SQL 쿼리
const [rows, fields] = await connection.execute('insert into tokens values(?, ?)', [userId, token]);

return rows;

} catch (err) {
console.error(err);
}
}
13 changes: 13 additions & 0 deletions noti.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import admin from "firebase-admin";
import { getTokenByUserId } from "./db/get-token.js";
import { serviceAccount } from "./key/service-account.js";
import { saveToken } from "./db/save-token.js";

// Firebase Admin SDK 초기화

Expand All @@ -15,6 +16,18 @@ const port = process.env.PORT || 3000;
app.use(express.json());


app.post('/save-token', async (req, res) => {
const { userId, token } = req.body;

const result = await saveToken(userId, token);

if (result) {
res.status(200).send('token saved successfully');
} else {
res.status(404).send('token saved failed');
}
})


// FCM 메시지 전송 엔드포인트
app.post('/send-notification', async (req, res) => {
Expand Down

0 comments on commit 96b4027

Please sign in to comment.