Skip to content

Commit

Permalink
Merge pull request #85 from TeamHARA/feat/push-alarm
Browse files Browse the repository at this point in the history
[FEAT] 푸시알람 설정
  • Loading branch information
leGit-y authored Dec 3, 2023
2 parents e6e9bb6 + d2fd753 commit 1b4d378
Show file tree
Hide file tree
Showing 10 changed files with 4,006 additions and 84 deletions.
3,010 changes: 2,958 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"firebase-admin": "^11.11.1",
"jsonwebtoken": "^9.0.1",
"jwks-rsa": "^3.1.0",
"moment": "^2.29.4",
Expand Down
7 changes: 4 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ model worry {
}

model token {
user_id Int @id
refresh_token String @unique @db.VarChar(150)
user user @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "token_user_id_fk")
user_id Int @id
refresh_token String @unique @db.VarChar(150)
device_token String?
user user @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "token_user_id_fk")
}
41 changes: 41 additions & 0 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NextFunction, Request, Response } from "express";

const admin = require('firebase-admin');
let serviceAccount = require("../../firebase-admin.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})

const pushAlarm = (req: Request, res: Response, next: NextFunction) => {
try{
const { deviceToken } = req.body;

let message = {
notification: {
title: '테스트 발송',
body: '우와~!!!!!!',
},
token: deviceToken,
}

admin
.messaging()
.send(message)
.then(function (response:Response) {
console.log('Successfully sent message: : ', response)
return res.status(200).json({success : true})
})
.catch(function (err:Error) {
console.log('Error Sending message!!! : ', err)
return res.status(400).json({success : false})
});
} catch (error) {
next(error);
}

}

export default{
pushAlarm
}
12 changes: 9 additions & 3 deletions src/controller/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N

const kakaoLogin =async (req: Request, res:Response, next:NextFunction) => {
try{
const { accessToken } = req.body;
const { accessToken, deviceToken } = req.body;

// get user kakao info
const response = await axios({
Expand All @@ -84,9 +84,14 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N
'Content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
})

const DTO = {
user: response.data,
deviceToken: deviceToken
}


const data = await authService.serviceLogin("kakao", response.data);
const data = await authService.serviceLogin("kakao", DTO);


// 경우에 따라 다른 response message 출력
Expand All @@ -113,12 +118,13 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N

const appleLogin =async (req: Request, res:Response, next:NextFunction) => {
try {
const { identityToken, user, fullName, email } = req.body
const { identityToken, user, fullName, email, deviceToken } = req.body
const DTO = {
identityToken: identityToken,
id: user,
fullName: fullName,
email: email,
deviceToken: deviceToken
}
const data = await authService.serviceLogin("apple", DTO);

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/DTO/userDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface userCreateDTO {
gender: string | null;
email: string | null;
refreshToken: string;
deviceToken: string;
}


Expand Down
7 changes: 4 additions & 3 deletions src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ const findIdByRefreshToken = async (refreshToken: string) => {
});
};

const updateRefreshTokenById = async (userId: number, token:string) => {
const updateTokenById = async (userId: number, refreshToken: string, deviceToken: string) => {
return await prisma.token.update({
where: {
user_id: userId,
},
data:{
refresh_token: token
refresh_token: refreshToken,
device_token: deviceToken
}

});
Expand All @@ -60,7 +61,7 @@ export default {
createRefreshToken,
findRefreshTokenById,
findIdByRefreshToken,
updateRefreshTokenById,
updateTokenById,
disableRefreshTokenById

}
3 changes: 2 additions & 1 deletion src/repository/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const createUser = async(userCreateDTO:userCreateDTO) => {

token:{
create:{
refresh_token: userCreateDTO.refreshToken
refresh_token: userCreateDTO.refreshToken,
device_token: userCreateDTO.deviceToken
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/service/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import tokenType from "../constants/tokenType";


// 캐라 서비스의 로그인 함수
const serviceLogin = async (provider:string, user:any) => {
const serviceLogin = async (provider:string, DTO:any) => {
const userCreateDTO: any = {};
let isNew = false;
let foundUser;
const { deviceToken } = DTO;

// kakao login
if(provider == "kakao"){
const { user } = DTO;
const { id, kakao_account } = user;

foundUser = await userService.getUserByKakaoId(id);
Expand Down Expand Up @@ -51,8 +53,8 @@ const serviceLogin = async (provider:string, user:any) => {
});

// 전달받은 identityToken이 변조되지 않은 올바른 토큰인지 확인하는 과정
const {identityToken, id, fullName, email} = user;
// console.log(identityToken,id,fullName)
const {identityToken, id, fullName, email} = DTO;

const decoded = jwt.decode(identityToken, { complete: true})
const kid = decoded.header.kid

Expand Down Expand Up @@ -95,6 +97,7 @@ const serviceLogin = async (provider:string, user:any) => {
// 신규회원일 경우 회원가입 진행
if(isNew){
userCreateDTO.refreshToken = refreshToken;
userCreateDTO.deviceToken = deviceToken;
const createdUser = await userService.createUser(userCreateDTO);
foundUser = createdUser
}
Expand All @@ -105,11 +108,11 @@ const serviceLogin = async (provider:string, user:any) => {
}


// 기존회원의 경우 이전 refresh token을 갱신하여 DB에 저장
// 기존회원의 경우 이전 refresh token, device token을 갱신하여 DB에 저장
if(!isNew){
const updatedToken = await tokenRepository.updateRefreshTokenById(foundUser.id,refreshToken);
const updatedToken = await tokenRepository.updateTokenById(foundUser.id,refreshToken,deviceToken);
if(!updatedToken){
throw new ClientException("refresh token 갱신 실패");
throw new ClientException("token 갱신 실패");
}
}

Expand Down
Loading

0 comments on commit 1b4d378

Please sign in to comment.