Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#47 Issue: Forgot-password flow #53

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class AuthController {
const { email } = req.body;
const user = await User.findOne({ email });

if (!user) return res.status(400).json({ message: 'User not found' });
if (!user) return res.status(400).json({ message: 'User not found. Check your email!' });

const resetToken = await this.generateToken(user)
const resetLink = `${process.env.CLIENT_URL}/reset-password?token=${resetToken.token}`;
Expand Down
56 changes: 52 additions & 4 deletions frontend/src/pages/forgot-password.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
'use client'
import React from 'react';
import { useState } from 'react';
import axios from 'axios';

const ForgotPassword: React.FC = () => {
interface Submit {
text: string
}

const [text, setEmail] = useState<Submit | null>(null);
const [error, setError] = useState<string | null>(null);
const [sentReq, setReq] = useState<boolean>(false)

const handleSignin = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail({ text: e.target.value })
}

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

// check that email exists in input
if (!text?.text) {
setError("An email is needed!")
return
}

const apiURI = `${process.env.NEXT_PUBLIC_API_URL}/auth/signin`;
await axios.post(apiURI,
//send email as JSON
{
email: text?.text
}
).then((res) => {
// upon success, change the text
if (res.status == 200) {
setReq(true)
}
}).catch((err) => {
// show error upon getting one
setError(err.response.data.message);
});
}

return (
<form className="flex flex-col items-start p-4 max-w-sm mx-auto" onSubmit={handleSubmit}>
<h2 className='mb-4 text-lg'>Forgot Password</h2>
<input type="email" placeholder="Email" className="mb-4 p-2 border border-gray-300 rounded w-full" />
<button type="submit" className="bg-blue-500 text-white p-2 rounded w-full">Submit</button>
<h2 className='mb-4 text-lg'>{!sentReq ? "Forgot Password" : "Forgot Password request sent!"}</h2>
{/* main piece changes! */}
{!sentReq ? (
<div className='w-[-webkit-fill-available]'>
<input type="email" placeholder="Email"
className="mb-4 p-2 border border-gray-300 rounded w-full"
onChange={handleSignin}
/>
<button type="submit" className="bg-blue-500 text-white p-2 rounded w-full">Submit</button>
<div className="mt-2 text-red-500 w-[-webkit-fill-available]">
{error && <p className='break-words'>{error}</p>}
</div>
</div>) :
<div>
<p className='mt-[-0.5rem] font-light'>Check your email to reset your password for further instructions.</p>
</div>
}
</form>
);
}
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/pages/signin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import Link from 'next/link';
import { useRouter } from 'next/navigation'
import { useState } from 'react';

Expand All @@ -25,6 +26,7 @@ const SignIn: React.FC = () => {

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

const apiURI = `${process.env.NEXT_PUBLIC_API_URL}/auth/signin`

try {
Expand Down Expand Up @@ -53,6 +55,7 @@ const SignIn: React.FC = () => {
}
}
router.push("/")

}

return (
Expand All @@ -75,8 +78,16 @@ const SignIn: React.FC = () => {
<button type="submit" className="w-full bg-blue-500 text-white p-2 rounded ">
Sign In
</button>

<div className="mt-8 text-red-500">
<div className='pt-2'>
{"Forgot password? Click "}
<Link
href={'../forgot-password'}
className={'text-blue-500 hover:underline'}
>
here
</Link>
</div>
<div className="mt-2 text-red-500">
{error && <p>{error}</p>}
</div>
</form>
Expand Down