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 register and authenticator handler in api; implement regist…
…er and login in app b00tc4mp#84
- Loading branch information
Showing
37 changed files
with
312 additions
and
114 deletions.
There are no files selected for viewing
30 changes: 29 additions & 1 deletion
30
staff/marti-herms/project/V-HUB/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 |
---|---|---|
@@ -1 +1,29 @@ | ||
export default {} | ||
import 'dotenv/config' | ||
import jwt from 'jsonwebtoken' | ||
|
||
import { logic } from 'core' | ||
import { errors } from 'com' | ||
|
||
const { SessionError } = errors | ||
|
||
export default (req, res, next) => { | ||
const { username, password } = req.body | ||
|
||
try { | ||
logic.authenticateUser(username, password) | ||
.then(userId => | ||
jwt.sign({ sub: userId }, process.env.JWT_SECRET, (error, token) => { | ||
if (error) { | ||
next(new SessionError(error.message)) | ||
|
||
return | ||
} | ||
|
||
res.json(token) | ||
}) | ||
) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
14 changes: 13 additions & 1 deletion
14
staff/marti-herms/project/V-HUB/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 |
---|---|---|
@@ -1 +1,13 @@ | ||
export default {} | ||
import { logic } from 'core' | ||
|
||
export default (req, res, next) => { | ||
const { username, email, password, role } = req.body | ||
|
||
try { | ||
logic.registerUser(username, email, password, role) | ||
.then(() => res.status(201).send()) | ||
.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
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
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 @@ | ||
curl -v http://localhost:8080/users/auth -X POST -d '{"username":"eden","password":"123123123"}' -H "Content-Type: application/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 @@ | ||
curl -v http://localhost:8080/users -X POST -d '{"username":"eden","email":"[email protected]","password":"123123123","dev":"false"}' -H "Content-Type: application/json" |
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
...rti-herms/project/V-HUB/app/src/index.jsx → ...f/marti-herms/project/V-HUB/app/index.jsx
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,7 +1,13 @@ | ||
import loginUser from "./loginUser"; | ||
import registerUser from "./registerUser"; | ||
import loginUser from './loginUser'; | ||
import logoutUser from './logoutUser'; | ||
import registerUser from './registerUser'; | ||
import isUserLoggedIn from './isUserLoggedIn'; | ||
|
||
export default logic = { | ||
const logic = { | ||
loginUser, | ||
registerUser | ||
} | ||
logoutUser, | ||
registerUser, | ||
isUserLoggedIn | ||
} | ||
|
||
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 @@ | ||
export default () => !!sessionStorage.token |
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,5 +1,34 @@ | ||
import { validate, errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (username, password) => { | ||
//validation | ||
validate.username(username) | ||
validate.password(password) | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/auth`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, password }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 200) { | ||
return response.json() | ||
.then(token => sessionStorage.token = token) | ||
} | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
//fetch | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => delete sessionStorage.token |
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,5 +1,35 @@ | ||
export default (username, email, password) => { | ||
//validation | ||
import { validate, errors } from "com" | ||
|
||
//fetch | ||
const { SystemError, ValidationError } = errors | ||
|
||
export default (username, email, password, rePassword, role) => { | ||
validate.username(username) | ||
validate.email(email) | ||
validate.password(password) | ||
validate.string(role, 'role') | ||
|
||
if (password !== rePassword) throw new ValidationError('passwords do not match') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, email, password, role }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 201) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
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
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 was deleted.
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,16 @@ | ||
import Button from '../library/Button' | ||
import Paragraph from '../library/Paragraph' | ||
import Container from '../library/Container' | ||
|
||
export default function Alert({ message, onAccept }) { | ||
return <> | ||
<Container className='fixed w-screen top-0 h-screen bg-black opacity-50'></Container> | ||
|
||
<Container className='fixed w-screen top-0 h-screen flex items-center justify-center'> | ||
<Container className='p-4 border bg-white dark:bg-black dark: text-white flex-col'> | ||
<Paragraph>{message}</Paragraph> | ||
<Button onClick={onAccept}>Accept</Button> | ||
</Container> | ||
</Container> | ||
</> | ||
} |
Oops, something went wrong.