Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 84867b0

Browse files
authored
Merge pull request #4 from spritlesoftware/email-domain
WIP: Validating email domain
2 parents c2baade + 492ec34 commit 84867b0

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

ReactJS-Project-CardinalKit/src/components/LoginPage.jsx

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import * as PropTypes from 'prop-types';
33

44
import { connect } from 'react-redux';
55

6+
import app from 'firebase/app';
67
import { loginUser } from '../actions/loginActions';
78
import { getLoginState, isAuthenticated } from '../selectors/loginSelectors';
8-
import app from 'firebase/app';
9-
109

1110
import { Button, ButtonColor } from '../ui/Button';
1211
import Firebase from './Firebase';
1312
import logo from '../images/login-office.jpeg';
1413
import logo2 from '../images/cardinal_logo.svg';
14+
import { toast } from 'react-toastify';
15+
toast.configure();
1516

1617

1718
export class LoginPage extends React.Component {
@@ -30,20 +31,20 @@ export class LoginPage extends React.Component {
3031
};
3132

3233
setVerificationCode = () => {
33-
var verifyCode = Math.floor(Math.random() * 9999 + 1);
34+
const verifyCode = Math.floor(Math.random() * 9999 + 1);
3435
localStorage.setItem('verify-code', verifyCode);
3536
this.props.history.push('/verify_code');
36-
return verifyCode
37-
}
37+
return verifyCode;
38+
};
3839

3940
sendMail = (email, code) => {
4041
window.Email.send({
4142
SecureToken: process.env.REACT_APP_EMAIL_TOKEN,
4243
To: email,
4344
From: process.env.REACT_APP_FROM_EMAIL,
4445
Subject: 'Verfication code',
45-
Body: 'Your verification code ' + code,
46-
})
46+
Body: `Your verification code ${code}`,
47+
});
4748
};
4849

4950
signInWithEmailAndPasswordHandler = (event, email, password) => {
@@ -52,28 +53,35 @@ export class LoginPage extends React.Component {
5253
firebase
5354
.doSignInWithEmailAndPassword(email, password)
5455
.then(() => {
55-
const verifyCode = this.setVerificationCode()
56+
const verifyCode = this.setVerificationCode();
5657
this.sendMail(email, verifyCode);
5758
this.setState({
58-
loggedIn: true
59-
})
59+
loggedIn: true,
60+
});
6061
})
6162
.catch(error => {
6263
this.setState({ erroMsg: 'Error signing in with password and email!' });
6364
console.error('Error signing in with password and email', error);
6465
});
6566
};
6667

67-
68+
validateUser = email => email.includes(
69+
process.env.REACT_APP_VERIFIED_EMAIL_SUBDOMAIN
70+
);
6871

6972
handleSubmit = () => {
7073
const firebase = new Firebase();
71-
firebase.doSignInWithGoogle()
72-
.then(() => {
73-
const verifyCode = this.setVerificationCode()
74-
this.sendMail(app.auth().currentUser.email, verifyCode)
75-
this.props.history.push('/verify_code')
76-
})
74+
firebase.doSignInWithGoogle().then(data => {
75+
if (this.validateUser(data.user.email)) {
76+
const verifyCode = this.setVerificationCode();
77+
this.sendMail(app.auth().currentUser.email, verifyCode);
78+
this.props.history.push('/verify_code');
79+
} else {
80+
const error_msg = 'email sub-domain not allowed, only '+ process.env.REACT_APP_VERIFIED_EMAIL_SUBDOMAIN + ' allowed to login with Google.';
81+
toast.error(error_msg);
82+
this.props.history.push('/login');
83+
}
84+
});
7785
};
7886

7987
render() {
@@ -88,13 +96,13 @@ export class LoginPage extends React.Component {
8896
<img
8997
aria-hidden="true"
9098
className="object-cover w-full h-full dark:hidden"
91-
src={logo}
99+
src={ logo }
92100
alt="Office"
93101
/>
94102
<img
95103
aria-hidden="true"
96104
className="hidden object-cover w-full h-full dark:block"
97-
src={logo2}
105+
src={ logo2 }
98106
alt="Office"
99107
/>
100108
</div>
@@ -109,10 +117,10 @@ export class LoginPage extends React.Component {
109117
className="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
110118
type="email"
111119
name="userEmail"
112-
value={this.state.userEmail}
120+
value={ this.state.userEmail }
113121
placeholder="E.g: faruq123@gmail.com"
114122
id="userEmail"
115-
onChange={event => this.handleChange(event)}
123+
onChange={ event => this.handleChange(event) }
116124
/>
117125
</label>
118126
<label className="block mt-4 text-sm">
@@ -121,33 +129,35 @@ export class LoginPage extends React.Component {
121129
className="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
122130
type="password"
123131
name="userPassword"
124-
value={this.state.userPassword}
132+
value={ this.state.userPassword }
125133
placeholder="Your Password"
126134
id="userPassword"
127-
onChange={event => this.handleChange(event)}
135+
onChange={ event => this.handleChange(event) }
128136
/>
129137
</label>
130138

131139
<a
132140
className="block w-full px-4 py-2 mt-4 text-sm font-medium leading-5 text-center text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg active:bg-purple-600 hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple"
133141
href="../index.html"
134-
onClick={event => {
142+
onClick={ event => {
135143
this.signInWithEmailAndPasswordHandler(
136144
event,
137145
this.state.userEmail,
138146
this.state.userPassword
139147
);
140-
}}
148+
} }
141149
>
142150
Log in
143151
</a>
144152

145153
<hr className="my-8" />
146154
<Button
147155
className="flex items-center justify-center w-full px-4 py-2 mt-4 text-sm font-medium leading-5 text-white text-gray-700 transition-colors duration-150 border border-gray-300 rounded-lg dark:text-gray-400 active:bg-transparent hover:border-gray-500 focus:border-gray-500 active:text-gray-500 focus:outline-none focus:shadow-outline-gray"
148-
onClick={() => { this.handleSubmit() }}
149-
selected={loading}
150-
color={ButtonColor.Blue}
156+
onClick={ () => {
157+
this.handleSubmit();
158+
} }
159+
selected={ loading }
160+
color={ ButtonColor.Blue }
151161
>
152162
Login with Google
153163
</Button>

0 commit comments

Comments
 (0)