-
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.
Browse files
Browse the repository at this point in the history
[BE][Feat] : #42 : 로그인 api 구현
- Loading branch information
Showing
12 changed files
with
454 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import express from 'express'; | ||
import { body } from 'express-validator'; | ||
import { login } from '../controllers/authController.js'; | ||
import { validationMiddleware } from '../middleware/validationMiddleware.js'; | ||
|
||
export const authRouter = express.Router(); | ||
|
||
authRouter.post( | ||
'/login', | ||
[ | ||
body('id').notEmpty().withMessage('ID is required'), | ||
body('password') | ||
.isLength({ min: 6 }) | ||
.withMessage('Password must be at least 6 characters long'), | ||
], | ||
validationMiddleware, | ||
login, | ||
); |
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 }; | ||
}; |
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
Oops, something went wrong.