Skip to content

Commit

Permalink
create server structure | create user logic: registerUser, retrieveUs…
Browse files Browse the repository at this point in the history
…er, authenticateUser | testing with html and specs b00tc4mp#361
  • Loading branch information
abelpriem committed Feb 17, 2024
1 parent 08d48a0 commit 247ebd8
Show file tree
Hide file tree
Showing 21 changed files with 2,039 additions and 0 deletions.
20 changes: 20 additions & 0 deletions staff/abel-prieto/PROYECT/API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## API - HIINIT v0.1

## USER REQUEST

`Register User`

- Request: POST /users "Content-Type": application/json { name, email, password }
- Response: 201
- Response (error) : 500|409 "Content-Type: application/json" { error, message }

`Authenticate User`
- Request: POST /users/auth "Content-Type": application/json { email, password }
- Response: 200 "Content-Type": application/json { userId }
- Respone (error): 500 "Content-Type": application/json { error, message }

`Retrieve User`
- Request: GET /users Authorization: Bearer '65d0e63fa0232cfaf1c8c411'
- Response: 200 "Content-Type": application/json { username, [ group ] }
- Response (error) : 500|404 "Content-Type": application/json { error, message }

19 changes: 19 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import authenticateUser from '../logic/authenticateUser.js'

export default (req, res) => {
try {
const { email, password } = req.body

authenticateUser(email, password)
.catch(error => {
let status = 500

res.status(status).json({ error: error.constructor.name, message: error.message })
})
.then(userId => res.json(userId))
} catch (error) {
let status = 500

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
9 changes: 9 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import authenticateUserHandler from './authenticateUserHandler.js'
import registerUserHandler from './registerUserHandler.js'
import retrieveUserHandler from './retrieveUserHandler.js'

export {
authenticateUserHandler,
registerUserHandler,
retrieveUserHandler
}
24 changes: 24 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import registerUser from '../logic/registerUser.js'

export default (req, res) => {
try {
const { name, email, password } = req.body

registerUser(name, email, password)
.catch(error => {
let status = 500

res.status(status).json({ error: error.constructor.name, message: error.message })

return
})
.then(() => res.status(201).send())
} catch (error) {
let status = 500

if (error.code === 11000)
status = 409

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
21 changes: 21 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/retrieveUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import retrieveUser from '../logic/retrieveUser.js'

export default (req, res) => {
try {
const userId = req.headers.authorization.substring(7)

retrieveUser(userId)
.catch(error => {
let status = 500

res.status(status).json({ error: error.constructor.name, message: error.message })

return
})
.then(user => res.json(user))
} catch (error) {
let status = 500

res.satus(status).json({ error: error.constructor.name, message: error.message })
}
}
34 changes: 34 additions & 0 deletions staff/abel-prieto/PROYECT/API/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import mongoose from 'mongoose'
import express from 'express'
import dotenv from 'dotenv'
import cors from 'cors'
import {
authenticateUserHandler,
registerUserHandler,
retrieveUserHandler
} from './handlers/index.js'

dotenv.config()

mongoose.connect(process.env.URL_MONGODB_HIINIT_API)
.then(() => {
const server = express()
const jsonBodyParser = express.json()

server.use(cors())

// ALL API REQUEST
server.get('/hello', (req, res) => res.send('Hello HIINIT API v0.0'))

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

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

// RETRIEVE USER
server.get('/users', retrieveUserHandler)

server.listen(process.env.PORT, () => console.log(`server online! Listen on: ${process.env.PORT}`))
})
.catch(error => console.error(error))
24 changes: 24 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/authenticateUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { User } from '../data/models.js'
import bcrypt from 'bcrypt'

function authenticateUser(email, password) {
return User.findOne({ email }).lean()
.catch(error => { throw new Error(error.message) })
.then(user => {
if (!user) {
throw new Error('user not found')
}

return bcrypt.compare(password, user.password)
.catch(error => { throw new Error(error.message) })
.then(match => {
if (!match) {
throw new Error('wrong credentials')
}

return user._id
})
})
}

export default authenticateUser
21 changes: 21 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/registerUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { User } from '../data/models.js'
import bcrypt from 'bcrypt'

function registerUser(username, email, password) {

return bcrypt.hash(password, 5)
.catch(error => { throw new Error(error.message) })
.then(hash => {
return User.create({ username, email, password: hash, group: 'localhost' })
.catch(error => {
if (error.code === 11000) {
throw new Error('duplicity error')
}

throw new Error(error.message)
})
.then(user => { })
})
}

export default registerUser
20 changes: 20 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/retrieveUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { User } from '../data/models.js'

function retrieveUser(userId) {
return User.findById(userId).lean()
.catch(error => { throw new Error(error.message) })
.then(user => {
if (!user) {
throw new Error('user not found')
}

delete user._id
delete user.__v
delete user.email
delete user.password

return user
})
}

export default retrieveUser
27 changes: 27 additions & 0 deletions staff/abel-prieto/PROYECT/API/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions staff/abel-prieto/PROYECT/API/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"main": "index.js",
"scripts": {
"start": "node .",
"watch": "node --watch .",
"inspect": "node --inspect-brk .",
"test": "mocha test/*.spec.js",
"test-inspect": "mocha --inspect-brk test/*.spec.js"
},
"dependencies": {
"bcrypt": "^5.1.1",
"chai": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.4.4",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"mocha": "^10.3.0",
"mongoose": "^8.1.2",
"pnpm": "^8.15.2"
},
"type": "module"
}
Loading

0 comments on commit 247ebd8

Please sign in to comment.