generated from ctc-uci/npo-frontend-vite-template
-
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.
* implemented auth (register, login, reset password, dashboard). need to figure out correct verfiy email link * Yarn packages * adjusted auth component import --------- Co-authored-by: Kristen Yee <[email protected]> Co-authored-by: maithyy <[email protected]> Co-authored-by: michellelin1 <[email protected]>
- Loading branch information
1 parent
062f12a
commit edf2832
Showing
14 changed files
with
837 additions
and
6 deletions.
There are no files selected for viewing
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,22 @@ | ||
// import React from 'react'; | ||
import { useLocation, Navigate } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
import ResetPassword from './ResetPassword'; | ||
import VerifyEmail from './VerifyEmail'; | ||
|
||
const EmailAction = ({ redirectPath }) => { | ||
const { search } = useLocation(); | ||
const mode = new URLSearchParams(search).get('mode'); | ||
const code = new URLSearchParams(search).get('oobCode'); | ||
|
||
if (code === null) { | ||
return <Navigate to={redirectPath} />; | ||
} | ||
return mode === 'resetPassword' ? <ResetPassword code={code} /> : <VerifyEmail code={code} />; | ||
}; | ||
|
||
EmailAction.propTypes = { | ||
redirectPath: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default EmailAction; |
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,43 @@ | ||
import { useState } from 'react'; | ||
import { sendPasswordReset } from '../../utils/auth_utils'; | ||
|
||
const ForgotPassword = () => { | ||
const [email, setEmail] = useState(); | ||
const [errorMessage, setErrorMessage] = useState(); | ||
const [confirmationMessage, setConfirmationMessage] = useState(); | ||
|
||
const handleForgotPassword = async e => { | ||
try { | ||
e.preventDefault(); | ||
await sendPasswordReset(email); | ||
setConfirmationMessage( | ||
'If the email entered is associated with an account, you should receive an email to reset your password shortly.', | ||
); | ||
setErrorMessage(''); | ||
setEmail(''); | ||
} catch (err) { | ||
setErrorMessage(err.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>Send Reset Email</h2> | ||
{errorMessage && <p>{errorMessage}</p>} | ||
<form onSubmit={handleForgotPassword}> | ||
<input | ||
type="text" | ||
value={email} | ||
onChange={({ target }) => setEmail(target.value)} | ||
placeholder="Email" | ||
/> | ||
<br /> | ||
<button type="submit">Send Email</button> | ||
</form> | ||
{confirmationMessage && <p>{confirmationMessage}</p>} | ||
<a href="/">Back to Login</a> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ForgotPassword; |
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,62 @@ | ||
import { useState } from 'react'; | ||
import { instanceOf } from 'prop-types'; | ||
import { Cookies, withCookies } from '../../utils/cookie_utils'; | ||
import { logInWithEmailAndPassword, useNavigate } from '../../utils/auth_utils'; | ||
// import { logInWithEmailAndPassword , signInWithGoogle, useNavigate } from '../utils/auth_utils'; | ||
|
||
const Login = ({ cookies }) => { | ||
const navigate = useNavigate(); | ||
const [email, setEmail] = useState(); | ||
const [password, setPassword] = useState(); | ||
const [errorMessage, setErrorMessage] = useState(); | ||
|
||
const handleStdLogin = async e => { | ||
try { | ||
e.preventDefault(); | ||
await logInWithEmailAndPassword(email, password, '/dashboard', navigate, cookies); | ||
} catch (err) { | ||
setErrorMessage(err.message); | ||
} | ||
}; | ||
|
||
// const handleGoogleLogin = async e => { | ||
// try { | ||
// e.preventDefault(); | ||
// await signInWithGoogle('/new-user', '/dashboard', navigate, cookies); | ||
// } catch (err) { | ||
// setErrorMessage(err.message); | ||
// } | ||
// }; | ||
|
||
return ( | ||
<div> | ||
<h2>Login</h2> | ||
{errorMessage && <p>{errorMessage}</p>} | ||
<form onSubmit={handleStdLogin}> | ||
<input type="text" onChange={({ target }) => setEmail(target.value)} placeholder="Email" /> | ||
<br /> | ||
<input | ||
type="password" | ||
onChange={({ target }) => setPassword(target.value)} | ||
placeholder="Password" | ||
/> | ||
<br /> | ||
<a href="/forgotpassword">Forgot Password</a> | ||
<br /> | ||
<button type="submit">Sign In</button> | ||
</form> | ||
|
||
{/* <br /> | ||
<form onSubmit={handleGoogleLogin}> | ||
<button type="submit">Sign In with Google</button> | ||
</form> | ||
<br /> */} | ||
</div> | ||
); | ||
}; | ||
|
||
Login.propTypes = { | ||
cookies: instanceOf(Cookies).isRequired, | ||
}; | ||
|
||
export default withCookies(Login); |
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,33 @@ | ||
import { useState } from 'react'; | ||
import { instanceOf } from 'prop-types'; | ||
import { logout, useNavigate } from '../../utils/auth_utils'; | ||
import { Cookies, withCookies } from '../../utils/cookie_utils'; | ||
|
||
const Logout = ({ cookies }) => { | ||
const navigate = useNavigate(); | ||
const [errorMessage, setErrorMessage] = useState(); | ||
|
||
const handleLogout = async () => { | ||
try { | ||
await logout('/', navigate, cookies); | ||
} catch (err) { | ||
setErrorMessage(err.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>Logout</h2> | ||
{errorMessage && <p>{errorMessage}</p>} | ||
<button type="submit" onClick={handleLogout}> | ||
Logout | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
Logout.propTypes = { | ||
cookies: instanceOf(Cookies).isRequired, | ||
}; | ||
|
||
export default withCookies(Logout); |
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,59 @@ | ||
import { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { confirmNewPassword } from '../../utils/auth_utils'; | ||
|
||
const ResetPassword = ({ code }) => { | ||
const [password, setPassword] = useState(); | ||
const [checkPassword, setCheckPassword] = useState(); | ||
const [errorMessage, setErrorMessage] = useState(); | ||
const [confirmationMessage, setConfirmationMessage] = useState(); | ||
const handleResetPassword = async e => { | ||
try { | ||
e.preventDefault(); | ||
if (password !== checkPassword) { | ||
throw new Error("Passwords don't match"); | ||
} | ||
await confirmNewPassword(code, password); | ||
setConfirmationMessage('Password changed. You can now sign in with your new password.'); | ||
setErrorMessage(''); | ||
setPassword(''); | ||
} catch (err) { | ||
setErrorMessage(err.message); | ||
} | ||
}; | ||
return ( | ||
<div> | ||
<h2>Reset Password</h2> | ||
{errorMessage && <p>{errorMessage}</p>} | ||
{!confirmationMessage && ( | ||
<form onSubmit={handleResetPassword}> | ||
<input | ||
type="password" | ||
onChange={({ target }) => setPassword(target.value)} | ||
placeholder="New Password" | ||
/> | ||
<br /> | ||
<input | ||
type="password" | ||
onChange={({ target }) => setCheckPassword(target.value)} | ||
placeholder="Re-enter Password" | ||
/> | ||
<br /> | ||
<button type="submit">Reset Password</button> | ||
</form> | ||
)} | ||
{confirmationMessage && ( | ||
<div> | ||
<p>{confirmationMessage}</p> | ||
<a href="/">Back to Login</a> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
ResetPassword.propTypes = { | ||
code: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default ResetPassword; |
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,43 @@ | ||
import { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { confirmVerifyEmail } from '../../utils/auth_utils'; | ||
|
||
const VerifyEmail = ({ code }) => { | ||
const [errorMessage, setErrorMessage] = useState(); | ||
const [confirmationMessage, setConfirmationMessage] = useState(); | ||
|
||
useEffect(() => { | ||
const verifyEmail = async () => { | ||
try { | ||
await confirmVerifyEmail(code); | ||
setConfirmationMessage( | ||
'Your email has been verified. You can now sign in with your new account.', | ||
); | ||
setErrorMessage(''); | ||
} catch (err) { | ||
setErrorMessage(err.message); | ||
} | ||
}; | ||
|
||
verifyEmail(); | ||
}, [code]); | ||
|
||
return ( | ||
<div> | ||
<h2>Verify Email</h2> | ||
{errorMessage && <p>{errorMessage}</p>} | ||
{confirmationMessage && ( | ||
<div> | ||
<p>{confirmationMessage}</p> | ||
<a href="/">Back to Login</a> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
VerifyEmail.propTypes = { | ||
code: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default VerifyEmail; |
Oops, something went wrong.