Skip to content

Commit

Permalink
implement getUserUsername logic in app; implement getUserUsername han…
Browse files Browse the repository at this point in the history
…dler in api b00tc4mp#84
  • Loading branch information
Eden23 committed Aug 12, 2024
1 parent 75fd61d commit 7e1be6b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { logic } from 'core'

export default (req, res, next) => {
const { userId } = req.params

try {
logic.getUserUsername(userId)
.then(username => res.json(username))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
2 changes: 2 additions & 0 deletions staff/marti-herms/project/V-HUB/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mongoose.connect(process.env.MONGODB_URI)

api.post('/users/auth', jsonBodyParser, handle.authenticateUser)

api.get('/users/:userId/username', jsonBodyParser, handle.getUserUsername)

api.use(errorHandler)

api.listen(process.env.PORT, () => console.info(`API listening on PORT ${process.env.PORT}`))
Expand Down
31 changes: 31 additions & 0 deletions staff/marti-herms/project/V-HUB/app/logic/getUserUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { errors } from 'com'

const { SystemError } = errors

import extractPayloadFromToken from '../util/extractPayloadFromToken.js'

export default () => {
const { sub: userId } = extractPayloadFromToken(sessionStorage.token)

return fetch(`${import.meta.env.VITE_API_URL}/users/${userId}/username`, {
headers: { Authorization: `Bearer ${sessionStorage.token}` }
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 200) {
return response.json()
.then(username => username)
}

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
4 changes: 3 additions & 1 deletion staff/marti-herms/project/V-HUB/app/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import loginUser from './loginUser';
import logoutUser from './logoutUser';
import registerUser from './registerUser';
import isUserLoggedIn from './isUserLoggedIn';
import getUserUsername from './getUserUsername';

const logic = {
loginUser,
logoutUser,
registerUser,
isUserLoggedIn
isUserLoggedIn,
getUserUsername
}

export default logic
24 changes: 23 additions & 1 deletion staff/marti-herms/project/V-HUB/app/src/home/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import { useState, useEffect } from "react"

import useContext from '../context'

export default function Home() {
return <p>Hello, World!</p>
const [username, setUsername] = useState(null)

useEffect(() => {
try {
logic.getUserUsername()
.then(username => setName(username))
.catch(error => {
console.error(error)

alert(error.message)
})
} catch (error) {
console.error(error)

alert(error.message)
}
})

return <p>Hello, {username}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default token => {
const payload64 = token.slice(token.indexOf('.') + 1, token.lastIndexOf('.'))

const payloadJSON = atob(payloadB64)

const payload = JSON.parse(payloadJSON)

return payload
}
2 changes: 1 addition & 1 deletion staff/marti-herms/project/V-HUB/core/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const user = new Schema({
},
avatar: {
type: String,
default: '../images/defaultAvatar.svg'
required: true
},
role: {
type: String,
Expand Down

0 comments on commit 7e1be6b

Please sign in to comment.