Skip to content

Commit

Permalink
Add reset password page
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranahmedse committed Apr 10, 2023
1 parent 725aa2b commit 3072e42
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 151 deletions.
98 changes: 98 additions & 0 deletions src/components/AuthenticationFlow/ResetPasswordForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { useEffect, useState } from 'preact/hooks';
import Spinner from '../Spinner';
import { httpPost } from '../../lib/http';
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';

export default function ResetPasswordForm() {
const [code, setCode] = useState('');
const [password, setPassword] = useState('');
const [passwordConfirm, setPasswordConfirm] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');

if (!code) {
window.location.href = '/login';
} else {
setCode(code);
}
}, []);

const handleSubmit = async (e: Event) => {
e.preventDefault();
setIsLoading(true);

if (password !== passwordConfirm) {
setIsLoading(false);
setError('Passwords do not match.');
return;
}

const { response, error } = await httpPost(
`${import.meta.env.PUBLIC_API_URL}/v1-reset-forgotten-password`,
{
newPassword: password,
confirmPassword: passwordConfirm,
code,
}
);

if (error?.message) {
setIsLoading(false);
setError(error.message);
return;
}

if (!response?.token) {
setIsLoading(false);
setError('Something went wrong. Please try again later.');
return;
}

const token = response.token;
Cookies.set(TOKEN_COOKIE_NAME, token);
window.location.href = '/';
};

return (
<form className="mx-auto w-full" onSubmit={handleSubmit}>
<input
type="password"
className="mb-2 mt-2 block w-full appearance-none rounded-lg border border-gray-300 px-3 py-2 shadow-sm outline-none transition duration-150 ease-in-out placeholder:text-gray-400 focus:ring-2 focus:ring-black focus:ring-offset-1"
required
minLength={6}
placeholder="New Password"
value={password}
onInput={(e) => setPassword((e.target as HTMLInputElement).value)}
/>

<input
type="password"
className="mt-2 block w-full appearance-none rounded-lg border border-gray-300 px-3 py-2 shadow-sm outline-none transition duration-150 ease-in-out placeholder:text-gray-400 focus:ring-2 focus:ring-black focus:ring-offset-1"
required
minLength={6}
placeholder="Confirm New Password"
value={passwordConfirm}
onInput={(e) =>
setPasswordConfirm((e.target as HTMLInputElement).value)
}
/>

{error && (
<p className="mt-2 rounded-lg bg-red-100 p-2 text-red-700">{error}</p>
)}

<button
type="submit"
disabled={isLoading}
className="mt-2 inline-flex w-full items-center justify-center rounded-lg bg-black p-2 py-3 text-sm font-medium text-white outline-none focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:bg-gray-400"
>
{isLoading ? 'Please wait...' : 'Reset Password'}
</button>
</form>
);
}
9 changes: 8 additions & 1 deletion src/components/Authenticator/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ function handleGuest() {

// Prepares the UI for the user who is logged out
function handleAuthenticated() {
const guestRoutes = ['/login', '/signup'];
const guestRoutes = [
'/login',
'/signup',
'/verify-account',
'/verification-pending',
'/reset-password',
'/forgot-password',
];

showHideGuestElements('hide');
showHideAuthElements('show');
Expand Down
135 changes: 0 additions & 135 deletions src/components/Profile/ResetPasswordForm.tsx

This file was deleted.

31 changes: 16 additions & 15 deletions src/pages/reset-password.astro
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
---
import ResetPasswordForm from '../components/Profile/ResetPasswordForm';
import ResetPasswordForm from '../components/AuthenticationFlow/ResetPasswordForm';
import SettingLayout from '../layouts/SettingLayout.astro';
---

<SettingLayout title='Reset Password'>
<div class='container py-16'>
<ResetPasswordForm client:load />
<div class='container'>
<div
class='mx-auto flex flex-col items-start justify-start pb-28 pt-10 sm:max-w-[400px] sm:items-center sm:justify-center sm:pt-20'
>
<div class='mb-2 text-left sm:mb-5 sm:text-center'>
<h1 class='mb-2 text-3xl font-semibold sm:mb-5 sm:text-5xl'>
Reset Password
</h1>
<p class='mb-3 text-base leading-6 text-gray-600'>
Enter and confirm your new password below.
</p>
</div>

<ResetPasswordForm client:load />
</div>
</div>
</SettingLayout>

<script>
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../lib/constants';
const token = Cookies.get(TOKEN_COOKIE_NAME);
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');

if (!code || token) {
window.location.href = '/settings/profile';
}
</script>

0 comments on commit 3072e42

Please sign in to comment.