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.
create server structure | create user logic: registerUser, retrieveUs…
…er, authenticateUser | testing with html and specs b00tc4mp#361
- Loading branch information
Showing
21 changed files
with
2,039 additions
and
0 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
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
19
staff/abel-prieto/PROYECT/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,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 }) | ||
} | ||
} |
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 authenticateUserHandler from './authenticateUserHandler.js' | ||
import registerUserHandler from './registerUserHandler.js' | ||
import retrieveUserHandler from './retrieveUserHandler.js' | ||
|
||
export { | ||
authenticateUserHandler, | ||
registerUserHandler, | ||
retrieveUserHandler | ||
} |
24 changes: 24 additions & 0 deletions
24
staff/abel-prieto/PROYECT/API/handlers/registerUserHandler.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,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
21
staff/abel-prieto/PROYECT/API/handlers/retrieveUserHandler.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,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 }) | ||
} | ||
} |
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,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)) |
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 @@ | ||
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 |
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,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 |
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,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 |
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
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" | ||
} |
Oops, something went wrong.