forked from b00tc4mp/isdi-bootcamp-202405
-
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.
implement getUserUsername logic in app; implement getUserUsername han…
…dler in api b00tc4mp#84
- Loading branch information
Showing
7 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
staff/marti-herms/project/V-HUB/api/handlers/getUserUsernameHandler.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,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) | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
staff/marti-herms/project/V-HUB/app/logic/getUserUsername.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,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) | ||
}) | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -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> | ||
} |
9 changes: 9 additions & 0 deletions
9
staff/marti-herms/project/V-HUB/app/util/extractPayloadFromToken.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,9 @@ | ||
export default token => { | ||
const payload64 = token.slice(token.indexOf('.') + 1, token.lastIndexOf('.')) | ||
|
||
const payloadJSON = atob(payloadB64) | ||
|
||
const payload = JSON.parse(payloadJSON) | ||
|
||
return payload | ||
} |
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