-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 로그인을 위한 패키지 (bcrypt, express-validator, jsonwebtoken) 설치 - 로그인을 위한 router, controller, service, repository, middleware 구현 - index.js router 호출 및 사용 - 로그인 api 구현 테스트
- Loading branch information
Showing
10 changed files
with
420 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { loginUser } from '../services/authService.js'; | ||
|
||
export const login = async (req, res) => { | ||
const { id, password } = req.body; | ||
|
||
try { | ||
const token = await loginUser(id, password); | ||
if (!token) { | ||
return res.status(401).json({ message: 'Invalid ID or password' }); | ||
} | ||
return res.status(200).json({ token }); | ||
} catch (error) { | ||
console.error('Login error:', error); | ||
return res.status(500).json({ message: 'Server error occurred' }); | ||
} | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { validationResult } from 'express-validator'; | ||
|
||
export const validationMiddleware = (req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { pool } from '../db/db.js'; | ||
|
||
export const findUserById = async id => { | ||
const result = await pool.query('SELECT * FROM "main"."user" WHERE id = $1', [id]); | ||
return result.rows[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import bcrypt from 'bcrypt'; | ||
import jwt from 'jsonwebtoken'; | ||
import { findUserById } from '../repositories/userRepository.js'; | ||
|
||
export const loginUser = async (id, password) => { | ||
const user = await findUserById(id); | ||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
|
||
const isPasswordValid = await bcrypt.compare(password, user.password); | ||
if (!isPasswordValid) { | ||
throw new Error('Invalid password'); | ||
} | ||
|
||
// JWT 생성 | ||
const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET, { expiresIn: '1h' }); | ||
return { token, userId: user.id }; | ||
}; |
Oops, something went wrong.