Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 3254938

Browse files
committed
Add Email Verification
1 parent 071ed3a commit 3254938

File tree

5 files changed

+117
-6
lines changed

5 files changed

+117
-6
lines changed

frontend/src/components/Account/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Header } from "semantic-ui-react";
44

55
import { PasswordForgetForm } from '../PasswordForget';
66
import PasswordChangeForm from '../PasswordChange';
7-
import { withAuthorization, AuthUserContext } from '../Session';
7+
import { withAuthorization, AuthUserContext, withEmailVerification } from '../Session';
8+
import { compose } from 'recompose';
89

910
const AccountPage = () => (
1011
<AuthUserContext.Consumer>
@@ -25,4 +26,7 @@ const AccountPage = () => (
2526

2627
const condition = authUser => authUser != null;
2728

28-
export default withAuthorization(condition)(AccountPage);
29+
export default compose(
30+
withEmailVerification,
31+
withAuthorization(condition),
32+
)(AccountPage);

frontend/src/components/Firebase/firebase.js

+32
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,39 @@ class Firebase {
2626

2727
doSignInWithEmailAndPassword = (email, password) =>
2828
this.auth.signInWithEmailAndPassword(email, password);
29+
30+
doSendEmailVerification = () =>
31+
this.auth.currentUser.sendEmailVerification();
2932

33+
onAuthUserListener = (next, fallback) =>
34+
this.auth.onAuthStateChanged(authUser => {
35+
if (authUser) {
36+
this.user(authUser.uid)
37+
.once('value')
38+
.then(snapshot => {
39+
const dbUser = snapshot.val();
40+
41+
// default empty roles
42+
if (!dbUser.roles) {
43+
dbUser.roles = {};
44+
}
45+
46+
// merge auth and db user
47+
authUser = {
48+
uid: authUser.uid,
49+
email: authUser.email,
50+
emailVerified: authUser.emailVerified,
51+
providerData: authUser.providerData,
52+
...dbUser,
53+
};
54+
55+
next(authUser);
56+
});
57+
} else {
58+
fallback();
59+
}
60+
});
61+
3062

3163
doSignOut = () => this.auth.signOut();
3264

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import AuthUserContext from './context';
22
import withAuthentication from "./withAuthentication";
33
import withAuthorization from "./withAuthorization";
4+
import withEmailVerification from './withEmailVerification';
45

5-
export { AuthUserContext, withAuthentication, withAuthorization };
6+
export {
7+
AuthUserContext,
8+
withAuthentication,
9+
withAuthorization,
10+
withEmailVerification,
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
3+
import AuthUserContext from './context';
4+
import { withFirebase } from '../Firebase';
5+
6+
const withEmailVerification = Component => {
7+
class WithEmailVerification extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
11+
this.state = {
12+
isSent: false,
13+
}
14+
}
15+
onSendEmailVerification = () => {
16+
this.props.firebase
17+
.doSendEmailVerification()
18+
.then(() => this.setState({
19+
isSent: true,
20+
}));
21+
}
22+
render() {
23+
return (
24+
<AuthUserContext.Consumer>
25+
{authUser =>
26+
needsEmailVerification(authUser) ? (
27+
<div>
28+
{this.state.isSent ? (
29+
<p>
30+
E-Mail confirmation sent: Check you E-Mails (Spam
31+
folder included) for a confirmation E-Mail.
32+
Refresh this page once you confirmed your E-Mail.
33+
</p>
34+
) : (
35+
<p>
36+
Verify your E-Mail: Check you E-Mails (Spam folder
37+
included) for a confirmation E-Mail or send
38+
another confirmation E-Mail.
39+
</p>
40+
)}
41+
<button
42+
type="button"
43+
onClick={this.onSendEmailVerification}
44+
disabled={this.state.isSent} >
45+
Send confirmation E-Mail
46+
</button>
47+
</div>
48+
) : (
49+
<Component {...this.props} />
50+
)}
51+
</AuthUserContext.Consumer>
52+
);
53+
}
54+
}
55+
56+
return withFirebase(WithEmailVerification);
57+
};
58+
59+
const needsEmailVerification = authUser =>
60+
authUser &&
61+
!authUser.emailVerified &&
62+
authUser.providerData
63+
.map(provider => provider.providerId)
64+
.includes('password');
65+
66+
export default withEmailVerification;

frontend/src/components/SignUp/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ class SignUpFormBase extends React.Component {
4141
email,
4242
});
4343
})
44+
.then(() => {
45+
return this.props.firebase.doSendEmailVerification();
46+
})
4447
.then(authUser => {
4548
this.setState({ ...INITIAL_STATE });
4649
this.props.history.push(ROUTES.HOME);
47-
alert(authUser);
4850
})
4951
.catch(error => {
50-
alert(error.message);
52+
if (error != undefined)
53+
alert(error.message);
5154
this.setState({ error });
5255
});
5356

@@ -82,7 +85,7 @@ class SignUpFormBase extends React.Component {
8285
value={username}
8386
onChange={this.onChange}
8487
type="text"
85-
placeholder="Full Name"
88+
placeholder="User Name"
8689
/>
8790
<input
8891
name="email"

0 commit comments

Comments
 (0)