Skip to content

Commit

Permalink
add authenticate user handler and add endpoint to index file b00tc4mp…
Browse files Browse the repository at this point in the history
  • Loading branch information
juditta99 committed Feb 28, 2024
1 parent e27537f commit fe905b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
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,
})
}
}
3 changes: 2 additions & 1 deletion staff/judy-grayland/project/api/handlers/index.js
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 }
9 changes: 7 additions & 2 deletions staff/judy-grayland/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import express, { json } from 'express'
import mongoose from 'mongoose'
import cors from 'cors' // the Cors package is a middleware for Express.js, that enables CORS in our API

import { registerUserHandler } from './handlers/index.js'
import {
authenticateUserHandler,
registerUserHandler,
} from './handlers/index.js'

dotenv.config()

Expand All @@ -12,7 +15,7 @@ mongoose
.then(() => {
const server = express()

// declaring endpoint for the root path. We tell it what it should do when a request of type get is made to this path: should send the response Hello World! (to the browser, to the terminal with curl, to our app with fetch)
// declaring endpoint for the root path. We tell it what it should do when a request of type get is made to this path: it should send the response Hello World! (to the browser, to the terminal with curl, to our app with fetch)
server.get('/', (req, res) => {
res.send('Hello World!')
})
Expand All @@ -23,6 +26,8 @@ mongoose

server.post('/users', jsonBodyParser, registerUserHandler)

server.post('/users/auth', jsonBodyParser, authenticateUserHandler)

// declaring endpoint for categories path
// server.get('/categories', (req, res) => {
// Category.find()
Expand Down
2 changes: 2 additions & 0 deletions staff/judy-grayland/project/api/logic/index.js
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

0 comments on commit fe905b1

Please sign in to comment.