Authorization
welaundry 회원 인증 방식을 설명합니다.
Last updated
welaundry 회원 인증 방식을 설명합니다.
Last updated
import jwt from 'jsonwebtoken';
import * as AuthRepository from '../data/auth.js';
import { config } from '../config.js'
const AUTH_ERROR = { message: '로그인이 필요한 서비스입니다.' }; // Authorization Error
export const isAuth = async (req, res, next) => {
let token;
if (!token) {
token = req.cookies['token'];
}
if (!token) {
return res.status(401).json(AUTH_ERROR);
}
// JWT(Json Web Token)
jwt.verify(
token,
config.jwt.secretKey,
async (error, decoded) => {
if (error) {
return res.status(401).json(AUTH_ERROR);
}
const user = await AuthRepository.findById(decoded.id);
if (!user) {
return res.status(401).json(AUTH_ERROR);
}
req.userId = user.id;
req.token = token;
req.userName = user.username;
next();
}
)
}