-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π add : νμκ°μ
& λ‘κ·ΈμΈ API μΆκ°
- Loading branch information
1 parent
a5c71f3
commit 5b227ed
Showing
3 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
export default async function (req, res, next) { | ||
try { | ||
const { authorization } = req.cookies; | ||
if (!authorization) throw new Error('ν ν°μ΄ μ‘΄μ¬νμ§ μμ΅λλ€.'); | ||
|
||
const [tokenType, token] = authorization.split(' '); | ||
if (tokenType !== 'Bearer') | ||
throw new Error('ν ν° νμ μ΄ μΌμΉνμ§ μμ΅λλ€.'); | ||
|
||
const decodedToken = jwt.verify(token, process.env.TOKEN_SECRET_KEY); | ||
const id = decodedToken.id; | ||
const account = getAccounts().find((account) => account.id == loginId); | ||
|
||
if (!account) { | ||
res.clearCookie('authorization'); | ||
throw new Error('ν ν° μ¬μ©μκ° μ‘΄μ¬νμ§ μμ΅λλ€.'); | ||
} | ||
|
||
req.account = account; | ||
|
||
next(); | ||
} catch (error) { | ||
res.clearCookie('authorization'); | ||
|
||
switch (error.name) { | ||
case 'TokenExpiredError': | ||
return res.status(401).json({ message: 'ν ν°μ΄ λ§λ£λμμ΅λλ€.' }); | ||
case 'JsonWebTokenError': | ||
return res.status(401).json({ message: 'ν ν°μ΄ μ‘°μλμμ΅λλ€.' }); | ||
default: | ||
return res | ||
.status(401) | ||
.json({ message: error.message ?? 'λΉμ μμ μΈ μμ²μ λλ€.' }); | ||
} | ||
} | ||
} |
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 @@ | ||
const accounts = []; | ||
|
||
export const addAccount = (account) => { | ||
accounts.push(account); | ||
}; | ||
|
||
export const getAccounts = () => { | ||
return accounts; | ||
}; |
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,65 @@ | ||
import express from 'express'; | ||
import bcrypt from 'bcrypt'; | ||
import jwt from 'jsonwebtoken'; | ||
import { addAccount, getAccounts } from '../models/account.model.js'; | ||
|
||
const router = express.Router(); | ||
const regex = /^[A-Za-z0-9]*$/; | ||
|
||
/** μ¬μ©μ νμκ°μ API **/ | ||
router.post('/sign-up', async (req, res, next) => { | ||
try { | ||
const { loginId, password } = req.body; | ||
const isExistUser = getAccounts().find((account) => account.id == loginId); | ||
console.log(getAccounts()); | ||
|
||
if (isExistUser) | ||
return res.status(409).json({ message: 'μ΄λ―Έ μ‘΄μ¬νλ IDμ λλ€.' }); | ||
else if (!regex.test(loginId)) | ||
return res | ||
.status(400) | ||
.json({ message: 'IDλ μμ΄μ μ«μλ§ μ¬μ©ν μ μμ΅λλ€.' }); | ||
else if (password.length < 6) | ||
return res | ||
.status(400) | ||
.json({ message: 'λΉλ°λ²νΈλ 6μ μ΄μμ΄μ΄μΌ ν©λλ€.' }); | ||
|
||
const hashedPassword = await bcrypt.hash(password, 10); | ||
|
||
// DB μ μ₯λΆ | ||
addAccount({ id: loginId, password: hashedPassword }); | ||
|
||
return res.status(201).json({ message: 'νμκ°μ μ΄ μλ£λμμ΅λλ€.' }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
/** μ¬μ©μ λ‘κ·ΈμΈ API **/ | ||
router.post('/sign-in', async (req, res, next) => { | ||
try { | ||
const { loginId, password } = req.body; | ||
// DB νΈμΆλΆ | ||
const account = getAccounts().find((account) => account.id == loginId); | ||
|
||
if (!account) | ||
return res.status(401).json({ message: 'μ‘΄μ¬νμ§ μλ IDμ λλ€.' }); | ||
else if (!(await bcrypt.compare(password, account.password))) | ||
return res.status(401).json({ message: 'λΉλ°λ²νΈκ° μΌμΉνμ§ μμ΅λλ€.' }); | ||
|
||
const token = jwt.sign( | ||
{ | ||
id: account.id, | ||
}, | ||
process.env.TOKEN_SECRET_KEY, | ||
); | ||
|
||
res.cookie('authorization', `Bearer ${token}`); | ||
|
||
return res.status(200).json({ message: 'λ‘κ·ΈμΈ μ±κ³΅' }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
export default router; |