Skip to content

Commit ee39471

Browse files
committed
feat: add email verification
1 parent 842c983 commit ee39471

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import React, { useState, useEffect, useRef } from 'react';
2+
3+
import { connect } from 'react-redux';
4+
import { Alert } from '@mui/material';
5+
import CircularProgress from '@mui/material/CircularProgress';
6+
7+
import { restVerifyEmail, verifyEmail } from '../../redux/actions';
8+
import { ReducerType } from '../../redux/reducers/rootReducer';
9+
import LogInComponent from '../../pages/login';
10+
11+
const VerfiyEmailPageComponent = (props: any) => {
12+
const autoScrollToBottomRef = useRef<HTMLDivElement>(null);
13+
const [showAlert, setShowAlert] = useState<boolean>(false);
14+
const [logIn, setLogIn] = useState<boolean>(false);
15+
16+
const { confirmEmailIsLoading, confirmEmailIsSuccess, confirmEmailIsError, confirmEmailIsMessage } = props.authState;
17+
18+
19+
useEffect(() => {
20+
setLogIn(() => false);
21+
props.restVerifyEmail();
22+
}, [])
23+
24+
25+
useEffect(() => {
26+
if (props.userId, props.token) {
27+
const finalData = {
28+
userId: props.userId,
29+
token: props.token
30+
};
31+
props.verifyEmail(finalData);
32+
}
33+
}, [props.userId, props.token]);
34+
35+
36+
useEffect(() => {
37+
window.scrollTo({
38+
top: 0,
39+
behavior: 'smooth'
40+
});
41+
// Auto Scroll functionality
42+
autoScrollToBottomRef?.current?.scrollIntoView({
43+
behavior: 'smooth',
44+
});
45+
}, []);
46+
47+
useEffect(() => {
48+
if (confirmEmailIsSuccess) {
49+
const timer = setTimeout(() => {
50+
setShowAlert(() => false);
51+
setLogIn(() => true);
52+
props.restVerifyEmail();
53+
}, 5000);
54+
return () => clearTimeout(timer);
55+
}
56+
}, [confirmEmailIsSuccess]);
57+
58+
useEffect(() => {
59+
if (confirmEmailIsSuccess || confirmEmailIsError) {
60+
setShowAlert(() => true);
61+
if (confirmEmailIsError) {
62+
const timer = setTimeout(() => {
63+
setShowAlert(() => false);
64+
setLogIn(() => false);
65+
props.restVerifyEmail();
66+
}, 30000);
67+
return () => clearTimeout(timer);
68+
}
69+
}
70+
}, [confirmEmailIsSuccess, confirmEmailIsError]);
71+
72+
if (logIn) {
73+
return <LogInComponent />;
74+
}
75+
76+
return (
77+
<div className="flex items-center justify-center py-[3rem] ">
78+
<div className=" mx-auto w-[90%] md:max-w-[30rem] md:min-w-[20rem]">
79+
<div
80+
className=" w-full rounded-[6px] mt-[2rem] "
81+
style={{
82+
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.2)'
83+
}}
84+
>
85+
{confirmEmailIsLoading && (
86+
<div className=" flex items-center justify-center pt-4 ">
87+
<CircularProgress color="secondary" />
88+
</div>
89+
)}
90+
{showAlert && (confirmEmailIsError || confirmEmailIsSuccess) && (
91+
<Alert
92+
variant="filled"
93+
severity={confirmEmailIsError ? 'error' : 'success'}
94+
onClose={() => setShowAlert(false)}
95+
>
96+
{confirmEmailIsMessage}
97+
</Alert>
98+
)}
99+
<section>
100+
<div className="title p-[1rem] text-center ">
101+
<div className="flex justify-center mt-[2rem]">
102+
<button
103+
onClick={() => setLogIn(true)}
104+
className="py-[8px] px-[1.3rem] block bg-[#42b72a] transition duration-150 text-white text-[1rem] border border-[#42b72a] hover:border-[#256818] hover:bg-[#256818] h-[2.7rem] duration-150 rounded-[4px] text-white font-bold h-[2.7rem] "
105+
>
106+
Back to Login?
107+
</button>
108+
</div>
109+
</div>
110+
</section>
111+
</div>
112+
113+
<div className="flex justify-center mb-[8px] mt-[2rem] space-x-3">
114+
<span className="custom-span">Conditions of Use</span>
115+
<span className="custom-span"> Privacy Notice </span>
116+
<span className="custom-span"> Help </span>
117+
</div>
118+
<div className="flex justify-center mb-[8px] space-x-3">
119+
<span className="text-[14.4px] text-[#555]">&copy; 2004-2021, saddamarbaa.com, Inc. or its affiliates</span>
120+
</div>
121+
</div>
122+
</div>
123+
);
124+
};
125+
126+
const mapStateToProps = (state: ReducerType) => {
127+
return {
128+
authState: state.auth
129+
};
130+
};
131+
132+
const mapDispatchToProps = {
133+
restVerifyEmail,
134+
verifyEmail
135+
};
136+
137+
export default connect(
138+
mapStateToProps,
139+
mapDispatchToProps
140+
)(VerfiyEmailPageComponent);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Head from 'next/head';
2+
import React, { useEffect, useState } from 'react';
3+
import { Fragment } from 'react';
4+
import { useRouter } from 'next/router';
5+
6+
import VerifyEmailPageComponent from '../../components/verify-email-page/VerifyEmailPageComponent';
7+
8+
const VerifyEmailScreen = () => {
9+
const router = useRouter();
10+
const [token, setToken] = useState();
11+
const [userId, setUserId] = useState();
12+
const { verifyEmail } = router.query;
13+
14+
15+
16+
useEffect(() => {
17+
if (verifyEmail ) {
18+
const firstSplitString = verifyEmail?.split('token=');
19+
const secondSplitString = firstSplitString[1]?.split('&id=');
20+
21+
setToken(() => secondSplitString && secondSplitString[0]);
22+
setUserId(() => secondSplitString && secondSplitString[1]);
23+
}
24+
}, [router]);
25+
26+
return (
27+
<Fragment>
28+
<Head>
29+
<title>Verify Email</title>
30+
</Head>
31+
<meta name="description" content="Verify Email" />
32+
33+
<VerifyEmailPageComponent token={token} userId={userId}/>
34+
</Fragment>
35+
);
36+
};
37+
38+
export default VerifyEmailScreen;

0 commit comments

Comments
 (0)