-
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.
- Loading branch information
OomsOoms
committed
Oct 2, 2024
1 parent
dc436bf
commit ea4ca29
Showing
15 changed files
with
604 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 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,30 @@ | ||
import HCaptcha from '@hcaptcha/react-hcaptcha'; | ||
import { showNotification } from '@mantine/notifications'; | ||
|
||
const Captcha = ({ show, onToken }) => { | ||
if (!show) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<HCaptcha | ||
sitekey={import.meta.env.MODE === 'development' | ||
? '10000000-ffff-ffff-ffff-000000000001' | ||
: '798876a4-47b6-480a-b92c-45bedfc45272'} | ||
onVerify={onToken} | ||
onExpire={() => onToken('')} | ||
onError={(err) => { | ||
onToken(''); | ||
showNotification({ | ||
title: 'Error', | ||
message: 'Cannot verify captcha', | ||
}); | ||
console.error(err); | ||
}} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default Captcha; |
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
65 changes: 65 additions & 0 deletions
65
client/src/features/authentication/components/RegisterForm.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useState, useContext } from 'react'; | ||
import { UserContext } from '../../../context/userContext.jsx'; | ||
import { useRegister } from '../hooks/useRegister.js'; | ||
import Captcha from '../../../components/ui/Captcha.jsx'; | ||
|
||
const RegisterForm = () => { | ||
const { user, loading } = useContext(UserContext); | ||
const [username, setUsername] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
const [captchaToken, setCaptchaToken] = useState(''); | ||
const { handleRegister, loading: registerLoading, error } = useRegister(); | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); // Prevent the default form submission | ||
handleRegister(username, email, password, captchaToken); | ||
} | ||
|
||
if (loading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
if (user) { | ||
window.location.href = '/'; | ||
} | ||
|
||
return ( | ||
<form onSubmit={onSubmit}> | ||
<div> | ||
<label htmlFor="username">Username:</label> | ||
<input | ||
type="text" | ||
id="username" | ||
value={username} | ||
onChange={(e) => setUsername(e.target.value)} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="email">Email:</label> | ||
<input | ||
type="email" | ||
id="email" | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="password">Password:</label> | ||
<input | ||
type="password" | ||
id="password" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
/> | ||
</div> | ||
<Captcha show={true} onToken={setCaptchaToken} /> | ||
<button type="submit" disabled={registerLoading}> | ||
{registerLoading ? 'Signing up...' : 'Signup'} | ||
</button> | ||
{error && <div>{error}</div>} | ||
</form> | ||
); | ||
}; | ||
|
||
export default RegisterForm; |
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,27 @@ | ||
import { useState } from 'react'; | ||
import { register } from '../services/registerService'; | ||
|
||
export const useRegister = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(null); | ||
|
||
const handleRegister = async (username, email, password, hCaptchaToken) => { | ||
setLoading(true); | ||
setError(null); | ||
try { | ||
const response = await register(username, email, password, hCaptchaToken); | ||
if (!response.ok) { | ||
return response; | ||
} | ||
if (response.ok) { | ||
window.location.href = '/'; | ||
} | ||
} catch (err) { | ||
setError(err.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { handleRegister, loading, 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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
import LoginForm from './components/LoginForm'; | ||
export default LoginForm; | ||
import RegisterForm from './components/RegisterForm'; | ||
|
||
export { | ||
LoginForm, | ||
RegisterForm, | ||
}; |
2 changes: 1 addition & 1 deletion
2
...es/authentication/services/authService.js → ...s/authentication/services/loginService.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
22 changes: 22 additions & 0 deletions
22
client/src/features/authentication/services/registerService.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,22 @@ | ||
export const register = async (username, email, password, hCaptchaToken) => { | ||
try { | ||
const response = await fetch('/api/users', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, email, password, hCaptchaToken }), | ||
credentials: 'include' // Include credentials (cookies) | ||
}); | ||
|
||
if (response.ok) { | ||
return { ok: true, message: 'Registration successful' }; | ||
} else { | ||
const data = await response.json(); | ||
return { ok: false, errors: data.errors }; | ||
} | ||
} catch (error) { | ||
console.error('There was a problem with the fetch operation:', error); | ||
return { ok: false, errors: [{ path: 'server', msg: 'Server 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
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 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