-
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.
Merge pull request #9 from Lend-it/110_login
[NEW] Login
- Loading branch information
Showing
9 changed files
with
176 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
SALT_ROUNDS = | ||
SECRET = |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
import User from '../models/User.js'; | ||
import bcrypt from 'bcrypt'; | ||
import generateToken from '../services/auth.js'; | ||
|
||
export default { | ||
async create(request, response) { | ||
const { useremail, password } = request.body; | ||
|
||
try { | ||
const user = await User.findOne({ | ||
where: { | ||
useremail, | ||
}, | ||
}); | ||
|
||
if (!user || !(await bcrypt.compare(password, user.password))) { | ||
return response.status(404).json({ error: 'Usuário/Senha inválidos' }); | ||
} | ||
|
||
const token = generateToken({ useremail }); | ||
|
||
return response.status(201).json(token); | ||
} catch (error) { | ||
return response.status(500).json({ error: 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
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,32 @@ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
function verifyToken(request, response, next) { | ||
const authHeader = request.headers.authorization; | ||
|
||
if (!authHeader) { | ||
return response.status(400).json({ error: 'Token não encontrado' }); | ||
} | ||
|
||
const parts = authHeader.split(' '); | ||
|
||
if (!parts.lenght == 2) { | ||
return response.status(401).json({ error: 'Erro no token' }); | ||
} | ||
|
||
const [scheme, token] = parts; | ||
|
||
if (!/^Bearer$/i.test(scheme)) { | ||
return response.status(401).json({ error: 'Token mal formatado' }); | ||
} | ||
|
||
jwt.verify(token, process.env.SECRET, (err, decoded) => { | ||
if (err) { | ||
return response.status(401).json({ error: 'Token inválido' }); | ||
} | ||
|
||
request.useremail = decoded.useremail; | ||
return next(); | ||
}); | ||
} | ||
|
||
export default verifyToken; |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { Router } from 'express'; | ||
|
||
import userRouter from './routes/user.routes.js'; | ||
import sessionRouter from './routes/session.routes.js'; | ||
|
||
const routes = Router(); | ||
|
||
routes.use('/users', userRouter); | ||
routes.use('/session', sessionRouter); | ||
|
||
export default routes; |
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 { Router } from 'express'; | ||
|
||
import SessionController from '../controllers/SessionController.js'; | ||
|
||
const sessionRouter = Router(); | ||
|
||
sessionRouter.post('/', SessionController.create); | ||
|
||
export default sessionRouter; |
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 jwt from 'jsonwebtoken'; | ||
|
||
function generateToken(params = {}) { | ||
return jwt.sign(params, process.env.SECRET, { | ||
expiresIn: 86400, | ||
}); | ||
} | ||
|
||
export default generateToken; |