Skip to content

Commit

Permalink
edit the code b00tc4mp#140
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonzalo committed May 28, 2024
1 parent 0d9e391 commit b1cd0c6
Show file tree
Hide file tree
Showing 47 changed files with 65 additions and 24 deletions.
10 changes: 1 addition & 9 deletions staff/adrian-martin/socialcode/api/data/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import fs from 'fs'
import { SystemError } from '../error.js'

// import { fileURLToPath } from "url";
// import path from "path"

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);

const data = {}

data.findUser = (condition, callback) => {
Expand All @@ -28,9 +22,7 @@ data.findUser = (condition, callback) => {
}

data.insertUser = (user, callback) => {
// const filepath = path.resolve(__dirname, "./users.json")


fs.readFile('./data/users.json', 'utf8', (error, json) => {
if (error) {
callback(new SystemError(error.message))
Expand All @@ -46,7 +38,7 @@ data.insertUser = (user, callback) => {

const newJson = JSON.stringify(users)

fs.writeFile(filepath, newJson, error => {
fs.writeFile('./data/users.json', newJson, error => {
if (error) {
callback(new SystemError(error.message))

Expand Down
7 changes: 7 additions & 0 deletions staff/adrian-martin/socialcode/api/data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@
"email": "[email protected]",
"username": "AdrianGon",
"password": "321321321"
},
{
"name": "Inaki",
"surname": "Barrera",
"email": "[email protected]",
"username": "Brutalito",
"password": "123123123"
}
]
19 changes: 18 additions & 1 deletion staff/adrian-martin/socialcode/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,31 @@ api.post('/users/auth', jsonBodyParser, (req, res) => {

return
}

console.log(`User ${username} authenticated`)
res.status(200).send()
})
} catch (error) {
res.status(500).json({error: error.constructor.name, message: error.message})
}
})

// api.post('/home', jsonBodyParser, (req, res) => {
// const {username} = req.body

// try {
// logic.findUser(username, error => {
// if (error) {
// res.status(500).json({error: error.constructor.name, message: error.message})

// return
// }
// res.status(200).send()
// })
// } catch (error) {
// res.status(500).json({error: error.constructor.name, message: error.message})
// }
// })


api.get('/posts', (req, res) => {

Expand Down
2 changes: 1 addition & 1 deletion staff/adrian-martin/socialcode/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ logic.authenticateUser = (username, password, callback) => {

return
}
callback(null)
callback(null, user.username)
})
}

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const USERNAME_REGEX = /^[a-zA-Z0-9-_]+$/ // /^[\w-]+$/
const PASSWORD_REGEX = /^[a-zA-Z0-9-_$%&=\[\]\{\}\<\>\(\)]{8,}$/
const NAME_REGEX = /^[a-zA-Z=\[\]\{\}\<\>\(\)]{1,}$/

logic.registerUser = (name, surname, email, username, password, passwordRepeat, calback) => {
logic.registerUser = (name, surname, email, username, password, passwordRepeat, callback) => {
if (!NAME_REGEX.test(name))
throw new ContentError('name is not valid')

Expand All @@ -28,7 +28,7 @@ logic.registerUser = (name, surname, email, username, password, passwordRepeat,

xhr.onload = () => {
if (xhr.status === 201) {
calback(null)
callback(null)

return
}
Expand All @@ -37,12 +37,12 @@ logic.registerUser = (name, surname, email, username, password, passwordRepeat,

const constructor = errors[error]

calback(new constructor(message))
callback(new constructor(message))
}

xhr.open('POST', 'http://localhost:8080/users')

const body = { name, surname, email, username, password, passwordRepeat}
const body = { name, surname, email, username, password, passwordRepeat }

const json = JSON.stringify(body)

Expand All @@ -51,7 +51,7 @@ logic.registerUser = (name, surname, email, username, password, passwordRepeat,

}

logic.loginUser = (username, password, calback) => {
logic.loginUser = (username, password, callback) => {
if (!USERNAME_REGEX.test(username))
throw new ContentError('username is not valid')

Expand All @@ -65,7 +65,7 @@ logic.loginUser = (username, password, calback) => {
if (xhr.status === 200) {
sessionStorage.username = username

calback(null)
callback(null)

return
}
Expand All @@ -74,12 +74,12 @@ logic.loginUser = (username, password, calback) => {

const constructor = errors[error]

calback(new constructor(message))
callback(new constructor(message))
}

xhr.open('POST', 'http://localhost:8080/users/auth')

const body = {username, password}
const body = { username, password }

const json = JSON.stringify(body)

Expand All @@ -93,10 +93,35 @@ logic.logoutUser = () => {
delete sessionStorage.username
}

logic.getUsername = () => {
const user = data.findUser(user => user.username === sessionStorage.username)
logic.getUsername = (username, callback) => {
data.findUser((user) => {

return user.name
const xhr = new XMLHttpRequest

xhr.onload = () => {
if (xhr.status === 200) {
sessionStorage.username = username

callback(null)

return
}

const { error, message } = JSON.parse(xhr.response)

const constructor = errors[error]

callback(new constructor(message))
}
xhr.open('POST', 'http://localhost:8080/users/auth')

const body = { username }

const json = JSON.stringify(body)

xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(json)
})
}

logic.getAllPosts = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class LoginForm extends FormWithFeedback{
}

getUsername() {
// const usernameField = this.children[0]
const usernameField = this.children[0]

// return usernameField.getValue()
return usernameField.getValue()
}

getPassword() {
Expand Down

0 comments on commit b1cd0c6

Please sign in to comment.