forked from b00tc4mp/isdi-bootcamp-202409
-
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 api base code with authenticate, register and get user name logic…
…; add storage for users and posts in json; add create post logic b00tc4mp#184
- Loading branch information
1 parent
987d3dd
commit 8642007
Showing
45 changed files
with
2,190 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
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,8 @@ | ||
import uuid from './uuid.js' | ||
import storage from './storage.js' | ||
|
||
export { | ||
storage, | ||
|
||
uuid | ||
} |
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 @@ | ||
[{"id":"m2wecjrb0ar","image":"https://pixabay.com/get/g0259c3660213baf3e4aa36f843176f3d021f71f70db7da315dda8809d7fe49fd0f394a4d00203563adb597194a0868c2_1280.jpg","text":"cuando estudiaba","author":"m2wd7pkr7xq","date":"2024-10-30T21:36:32.519Z","likedBy":[],"comments":[]}] |
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,31 @@ | ||
import fs from 'fs' | ||
|
||
export default { | ||
get users() { | ||
const json = fs.readFileSync('./data/users.json', 'utf-8') | ||
|
||
const users = JSON.parse(json) | ||
|
||
return users | ||
}, | ||
|
||
set users(users) { | ||
const json = JSON.stringify(users) | ||
|
||
fs.writeFileSync('./data/users.json', json) | ||
}, | ||
|
||
get posts() { | ||
const json = fs.readFileSync('./data/posts.json', 'utf-8') | ||
|
||
const posts = JSON.parse(json) | ||
|
||
return posts | ||
}, | ||
|
||
set posts(posts) { | ||
const json = JSON.stringify(posts) | ||
|
||
fs.writeFileSync('./data/posts.json', json) | ||
} | ||
} |
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,11 @@ | ||
import uuid from './uuid.js' | ||
|
||
import storage from './storage.js' | ||
|
||
storage.users = [ | ||
{ id: uuid(), name: 'Pepito Grillo', email: '[email protected]', username: 'pepitogrillo', password: '123123123' } | ||
] | ||
|
||
storage.posts = [] | ||
|
||
console.log(storage.users) |
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,16 @@ | ||
[ | ||
{ | ||
"id": "m2wd1d38rhe", | ||
"name": "Pepito Grillo", | ||
"email": "[email protected]", | ||
"username": "pepitogrillo", | ||
"password": "123123123" | ||
}, | ||
{ | ||
"id": "m2wd7pkr7xq", | ||
"name": "Pin Ocho", | ||
"email": "[email protected]", | ||
"username": "pinocho", | ||
"password": "123123123" | ||
} | ||
] |
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,3 @@ | ||
const uuid = () => (Date.now() + Math.random()).toString(36).replace('.', '') | ||
|
||
export default uuid |
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,68 @@ | ||
import express, { json } from 'express' | ||
import logic from './logic/index.js' | ||
|
||
const server = express() | ||
|
||
const jsonBodyParser = express.json() | ||
|
||
server.use(express.static('public')) | ||
|
||
server.post('/authenticate', jsonBodyParser, (req, res) => { | ||
const { username, password } = req.body | ||
|
||
try { | ||
const userId = logic.authenticateUser(username, password) | ||
|
||
res.json(userId) | ||
} catch (error) { | ||
res.status(401).json({ error: error.constructor.name, message: error.message }) | ||
|
||
console.error(error) | ||
} | ||
}) | ||
|
||
server.post('/register', jsonBodyParser, (req, res) => { | ||
const { name, email, username, password, 'password-repeat': passwordRepeat } = req.body | ||
|
||
try { | ||
logic.registerUser(name, email, username, password, passwordRepeat) | ||
|
||
res.status(201).send() | ||
} catch (error) { | ||
res.status(400).json({ error: error.constructor.name, message: error.message }) | ||
|
||
console.error(error) | ||
} | ||
}) | ||
|
||
server.get('/users/:userId/name', (req, res) => { | ||
const { userId } = req.params | ||
|
||
try { | ||
const name = logic.getUserName(userId) | ||
|
||
res.json(name) | ||
} catch (error) { | ||
res.status(400).json({ error: error.constructor.name, message: error.message }) | ||
|
||
console.error(error) | ||
} | ||
}) | ||
|
||
server.post('/posts', jsonBodyParser, (req, res) => { | ||
const userId = req.headers.authorization.slice(6) | ||
|
||
const { image, text } = req.body | ||
|
||
try { | ||
logic.createPost(userId, image, text) | ||
|
||
res.status(201).send() | ||
} catch (error) { | ||
res.status(400).json({ error: error.constructor.name, message: error.message }) | ||
|
||
console.error(error) | ||
} | ||
}) | ||
|
||
server.listen(8080, () => console.log('api is up')) |
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,16 @@ | ||
import { storage } from '../data/index.js' | ||
import validate from './helpers/validate.js' | ||
|
||
export default (username, password) => { | ||
validate.username(username) | ||
validate.password(password) | ||
|
||
const { users } = storage | ||
|
||
const user = users.find(user => user.username === username && user.password === password) | ||
|
||
if (user === undefined) | ||
throw new Error('Wrong credentials, try again') | ||
|
||
return user.id | ||
} |
7 changes: 7 additions & 0 deletions
7
staff/luis-morlets/unsocial/api/logic/authenticateUser.test.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,7 @@ | ||
import authenticateUser from './authenticateUser.js' | ||
|
||
try { | ||
console.log(authenticateUser('pinocho', '123123123')) | ||
} 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,27 @@ | ||
import { uuid, storage } from '../data/index.js' | ||
import { validate } from './helpers/index.js' | ||
|
||
export default (userId, image, text) => { | ||
validate.id(userId, 'userId') | ||
validate.image(image) | ||
validate.text(text) | ||
|
||
const { users, posts } = storage | ||
|
||
const found = users.some(({ id }) => id === userId) | ||
|
||
if (!found) throw new Error('user not found') | ||
|
||
const post = { | ||
id: uuid(), | ||
image: image, | ||
text: text, | ||
author: userId, | ||
date: new Date, | ||
likedBy: [], | ||
comments: [] | ||
} | ||
posts.push(post) | ||
|
||
storage.posts = posts | ||
} |
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,8 @@ | ||
import createPost from './createPost.js' | ||
|
||
try { | ||
createPost('m2wd7pkr7xq', 'https://pixabay.com/get/g0259c3660213baf3e4aa36f843176f3d021f71f70db7da315dda8809d7fe49fd0f394a4d00203563adb597194a0868c2_1280.jpg', 'cuando estudiaba') | ||
|
||
} 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,14 @@ | ||
import { storage } from '../data/index.js' | ||
import validate from './helpers/validate.js' | ||
|
||
export default userId => { | ||
validate.id(userId, 'userId') | ||
|
||
const { users } = storage | ||
|
||
const user = users.find(user => user.id === userId) | ||
|
||
if (!user) throw new Error('user not found') | ||
|
||
return user.name | ||
} |
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,7 @@ | ||
import getUserName from './getUserName.js' | ||
|
||
try { | ||
console.log(getUserName('m2wd7pkr7xq')) | ||
} 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,5 @@ | ||
import validate from './validate.js' | ||
|
||
export { | ||
validate | ||
} |
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,54 @@ | ||
const validateName = name => { | ||
if (typeof name !== 'string') throw new Error('invalid text name') | ||
if (name.length < 2) | ||
throw new Error('invalid name lenght') | ||
} | ||
|
||
const validateEmail = email => { | ||
if (typeof email !== 'string') throw new Error('invalid email') | ||
if (!/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i.test(email)) | ||
throw new Error('invalid e-mail') | ||
} | ||
|
||
const validateUsername = username => { | ||
if (typeof username !== 'string') throw new Error('invalid username') | ||
if (username.length < 4 || username.length > 14) | ||
throw new Error('invalid username lenght') | ||
} | ||
|
||
const validatePassword = password => { | ||
if (typeof password !== 'string') throw new Error('invalid password') | ||
if (password.length < 8) | ||
throw new Error('invalid password lenght') | ||
} | ||
|
||
const validatePasswordsMatch = (password, passwordRepeat) => { | ||
if (typeof passwordRepeat !== 'string') throw new Error('invalid password repeat') | ||
if (password !== passwordRepeat) | ||
throw new Error('passwords do not match') | ||
} | ||
|
||
const validateImage = image => { | ||
if (typeof image !== 'string') throw new Error('invalid image') | ||
} | ||
|
||
const validateText = text => { | ||
if (typeof text !== 'string') throw new Error('invalid text') | ||
} | ||
|
||
const validateId = (id, explain = 'id') => { | ||
if (typeof id !== 'string') throw new Error(`invalid ${explain}`) | ||
} | ||
|
||
const validate = { | ||
name: validateName, | ||
email: validateEmail, | ||
username: validateUsername, | ||
password: validatePassword, | ||
passwordMatch: validatePasswordsMatch, | ||
image: validateImage, | ||
text: validateText, | ||
id: validateId | ||
} | ||
|
||
export default validate |
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,36 @@ | ||
import createPost from './createPost.js' | ||
//import deletePost from './deletePost' | ||
//import getPosts from './getPosts' | ||
//import likesInteraction from './likesInteraction' | ||
|
||
import getUserName from './getUserName.js' | ||
//import isUserLoggedIn from './isUserLoggedIn' | ||
import authenticateUser from './authenticateUser.js' | ||
//import logoutUser from './logoutUser' | ||
import registerUser from './registerUser.js' | ||
//import getUserId from './getUserId' | ||
|
||
|
||
// import createComment from './createComment' | ||
// import getComments from './getComments' | ||
// import removeComment from './removeComment' | ||
|
||
const logic = { | ||
registerUser, | ||
authenticateUser, | ||
// isUserLoggedIn, | ||
// logoutUser, | ||
getUserName, | ||
//getUserId, | ||
|
||
// getComments, | ||
// createComment, | ||
// removeComment, | ||
|
||
createPost, | ||
// getPosts, | ||
// deletePost, | ||
// likesInteraction, | ||
} | ||
|
||
export default logic |
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 validate from './helpers/validate.js' | ||
import { uuid, storage } from '../data/index.js' | ||
|
||
export default (name, email, username, password, passwordRepeat) => { | ||
|
||
validate.name(name) | ||
validate.email(email) | ||
validate.username(username) | ||
validate.password(password) | ||
validate.passwordMatch(password, passwordRepeat) | ||
|
||
const { users } = storage | ||
|
||
let user = users.find(user => user.username === username || user.email === email) | ||
|
||
if (user !== undefined) | ||
throw new Error('user already exists') | ||
|
||
user = { id: uuid(), name: name, email: email, username: username, password: password } | ||
|
||
users.push(user) | ||
|
||
storage.users = users | ||
} |
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,7 @@ | ||
import registerUser from './registerUser.js' | ||
|
||
try { | ||
registerUser('Pin Ocho', '[email protected]', 'pinocho', '123123123', '123123123') | ||
} catch (error) { | ||
console.error(error) | ||
} |
Oops, something went wrong.