forked from b00tc4mp/isdi-parttime-202309
-
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 authenticate user handler and add endpoint to index file b00tc4mp…
- Loading branch information
Showing
4 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
staff/judy-grayland/project/api/handlers/authenticateUserHandler.js
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,41 @@ | ||
import jwt from 'jsonwebtoken' | ||
import logic from '../logic/index.js' | ||
import { errors } from '../../shared/index.js' | ||
const { NotFoundError, CredentialsError, ContentError } = errors | ||
|
||
export default (req, res) => { | ||
const { email, password } = req.body | ||
|
||
try { | ||
logic | ||
.authenticateUser(email, password) | ||
.then((userId) => { | ||
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, { | ||
expiresIn: process.env.JWT_EXP, | ||
}) | ||
res.json(token) | ||
}) | ||
.catch((error) => { | ||
let status = 500 | ||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} else if (error instanceof CredentialsError) { | ||
status = 401 | ||
} | ||
res.status(status).json({ | ||
error: error.constructor.name, | ||
message: error.constructor.message, | ||
}) | ||
}) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
res.status(status).json({ | ||
error: error.constructor.name, | ||
message: error.constructor.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import registerUserHandler from './registerUserHandler.js' | ||
import authenticateUserHandler from './authenticateUserHandler.js' | ||
|
||
export { registerUserHandler } | ||
export { registerUserHandler, authenticateUserHandler } |
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,7 +1,9 @@ | ||
import registerUser from './registerUser.js' | ||
import authenticateUser from './authenticateUser.js' | ||
|
||
const logic = { | ||
registerUser, | ||
authenticateUser, | ||
} | ||
|
||
export default logic |