Add email verification

This commit is contained in:
Piotr Rogowski 2021-12-26 15:11:18 +01:00
parent 2ad15e334b
commit ba3833b7dd
No known key found for this signature in database
GPG Key ID: 4A842D702D9C6F8F
5 changed files with 27 additions and 8 deletions

View File

@ -17,6 +17,7 @@ import {
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { Routes } from '../../routes'; import { Routes } from '../../routes';
import validateMessages from './validateMessages'; import validateMessages from './validateMessages';
import emailNotVerifiedWarning from './emailNotVerifiedWarning';
const { Item } = Form; const { Item } = Form;
@ -29,12 +30,16 @@ const Login = () => {
const onFinish = async ({ email, password }: { form: any, email: string, password: string }) => { const onFinish = async ({ email, password }: { form: any, email: string, password: string }) => {
setIsLoading(true); setIsLoading(true);
try { try {
await login(email, password); const userCredentials = await login(email, password);
notification.success({ notification.success({
message: 'Login successful', message: 'Login successful',
description: 'Welcome back!', description: 'Welcome back!',
}); });
setIsLoading(false);
if (!userCredentials.user.emailVerified) {
emailNotVerifiedWarning();
}
history.push(Routes.ROOT); history.push(Routes.ROOT);
} catch (err) { } catch (err) {
form.resetFields(); form.resetFields();
@ -43,8 +48,8 @@ const Login = () => {
message: 'Login failed', message: 'Login failed',
description: (err as Error).message, description: (err as Error).message,
}); });
setIsLoading(false);
} }
setIsLoading(false);
}; };
return ( return (

View File

@ -17,10 +17,11 @@ import {
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { Routes } from '../../routes'; import { Routes } from '../../routes';
import validateMessages from './validateMessages'; import validateMessages from './validateMessages';
import emailNotVerifiedWarning from './emailNotVerifiedWarning';
const { Item } = Form; const { Item } = Form;
const strongPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/; const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/;
const SignUp = () => { const SignUp = () => {
const [form] = Form.useForm(); const [form] = Form.useForm();
@ -36,7 +37,8 @@ const SignUp = () => {
message: 'Sign Up successful', message: 'Sign Up successful',
description: 'Welcome on board!', description: 'Welcome on board!',
}); });
setIsLoading(false); emailNotVerifiedWarning();
history.push(Routes.ROOT); history.push(Routes.ROOT);
} catch (err) { } catch (err) {
form.resetFields(); form.resetFields();
@ -45,8 +47,8 @@ const SignUp = () => {
message: 'Failed to create an account', message: 'Failed to create an account',
description: (err as Error).message, description: (err as Error).message,
}); });
setIsLoading(false);
} }
setIsLoading(false);
}; };
return ( return (
@ -77,7 +79,7 @@ const SignUp = () => {
name="password" name="password"
rules={[ rules={[
{ required: true }, { required: true },
{ pattern: strongPassword, message: 'Password is too weak!' }, { pattern: passwordPattern, message: 'Password is too weak!' },
]} ]}
hasFeedback hasFeedback
> >

View File

@ -0,0 +1,8 @@
import { notification } from 'antd';
const emailNotVerifiedWarning = () => notification.warn({
message: 'Check your email',
description: 'Your email address has to be verified before you can upload files!',
});
export default emailNotVerifiedWarning;

View File

@ -11,6 +11,7 @@ import {
auth, auth,
createUserWithEmailAndPassword, createUserWithEmailAndPassword,
signInWithEmailAndPassword, signInWithEmailAndPassword,
sendEmailVerification,
signOut, signOut,
} from '../firebase'; } from '../firebase';
@ -32,7 +33,8 @@ const AuthProvider = (props: { children: ReactNode }) => {
const value = useMemo(() => ({ const value = useMemo(() => ({
currentUser, currentUser,
signUp: (email: string, password: string) => createUserWithEmailAndPassword(auth, email, password), signUp: (email: string, password: string) => createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => sendEmailVerification(userCredential.user)),
login: (email: string, password: string) => signInWithEmailAndPassword(auth, email, password), login: (email: string, password: string) => signInWithEmailAndPassword(auth, email, password),
logout: () => signOut(auth), logout: () => signOut(auth),
}), [currentUser]); }), [currentUser]);

View File

@ -4,6 +4,7 @@ import {
createUserWithEmailAndPassword, createUserWithEmailAndPassword,
signInWithEmailAndPassword, signInWithEmailAndPassword,
signOut, signOut,
sendEmailVerification,
} from 'firebase/auth'; } from 'firebase/auth';
import { getAnalytics } from 'firebase/analytics'; import { getAnalytics } from 'firebase/analytics';
@ -26,5 +27,6 @@ export {
analytics, analytics,
createUserWithEmailAndPassword, createUserWithEmailAndPassword,
signInWithEmailAndPassword, signInWithEmailAndPassword,
sendEmailVerification,
signOut, signOut,
}; };