-
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.
- Loading branch information
1 parent
3bccc59
commit d9d4903
Showing
10 changed files
with
70 additions
and
57 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
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,5 +1,7 @@ | ||
const userController = require('./userController'); | ||
const loginController = require('./loginController'); | ||
|
||
module.exports = { | ||
userController, | ||
loginController, | ||
}; |
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,15 @@ | ||
const { loginService } = require('../services'); | ||
|
||
const loginController = async (req, res, next) => { | ||
const { email, password } = req.body; | ||
const response = await loginService( { email, password }); | ||
if (response.error) return next(response.error); | ||
if (response.status === 400) { | ||
const err = new Error(response.message); | ||
err.status = response.status; | ||
return next(err); | ||
} | ||
res.status(200).json({ token: response.token }); | ||
}; | ||
|
||
module.exports = loginController |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
const userService = require('./userService'); | ||
|
||
const loginService = require('./loginService'); | ||
module.exports = { | ||
userService, | ||
loginService, | ||
}; |
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,24 @@ | ||
const { loginValidation } = require('./validations'); | ||
const { findByEmail, createToken } = require('./userService'); | ||
|
||
const loginService = async ({ email, password }) => { | ||
const error = loginValidation({ email, password }); | ||
if (error) return { error }; | ||
|
||
const user = await findByEmail(email); | ||
if (!user || user.password !== password) { | ||
return { | ||
status: 400, | ||
message: 'Invalid fields', | ||
}; | ||
} | ||
|
||
const token = createToken({ email }); | ||
|
||
return { | ||
status: 200, | ||
token, | ||
}; | ||
}; | ||
|
||
module.exports = loginService |
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
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