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

Commit c8e56e8

Browse files
committed
Merge branch 'email-domain'
2 parents c2baade + 6a5efb0 commit c8e56e8

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

ReactJS-Project-CardinalKit/src/components/Firebase/firebase.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const config = {
1111
storageBucket: process.env.REACT_APP_STORAGE_BUCKET || 'cs342-master-sample.appspot.com',
1212
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID || '267563013930',
1313
appId: process.env.REACT_APP_ID || '1:267563013930:web:99eeeff653b0f07accb053',
14-
// iOSAppBundleId: process.env.IOS_APP_ID || 'edu.stanford.cs342.sample-study', // as setup on your iOS project
15-
iOSAppBundleId: process.env.IOS_APP_ID || 'com.siva.cardinalkit-example', // as setup on your iOS project
14+
iOSAppBundleId: process.env.REACT_APP_IOS_APP_ID || 'edu.stanford.cs342.sample-study', // as setup on your iOS project
1615
};
1716
const firebaseApp = app.initializeApp(config);
1817
const db = firebaseApp.firestore();

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

Lines changed: 43 additions & 31 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,22 @@ export class LoginPage extends React.Component {
3031
};
3132

3233
setVerificationCode = () => {
33-
var verifyCode = Math.floor(Math.random() * 9999 + 1);
34+
var verifyCode = Math.floor(1000 + Math.random() * 9000);
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({
41-
SecureToken: process.env.REACT_APP_EMAIL_TOKEN,
42+
Host : process.env.REACT_APP_EMAIL_HOST,
43+
Username : process.env.REACT_APP_EMAIL_USER_NAME,
44+
Password : process.env.REACT_APP_EMAIL_PASSWORD,
4245
To: email,
43-
From: process.env.REACT_APP_FROM_EMAIL,
44-
Subject: 'Verfication code',
45-
Body: 'Your verification code ' + code,
46-
})
46+
From: process.env.REACT_APP_EMAIL_USER_NAME,
47+
Subject: 'Two factor authentication code',
48+
Body: `Your two factor authentication code ${code}`,
49+
});
4750
};
4851

4952
signInWithEmailAndPasswordHandler = (event, email, password) => {
@@ -52,28 +55,35 @@ export class LoginPage extends React.Component {
5255
firebase
5356
.doSignInWithEmailAndPassword(email, password)
5457
.then(() => {
55-
const verifyCode = this.setVerificationCode()
58+
const verifyCode = this.setVerificationCode();
5659
this.sendMail(email, verifyCode);
5760
this.setState({
58-
loggedIn: true
59-
})
61+
loggedIn: true,
62+
});
6063
})
6164
.catch(error => {
6265
this.setState({ erroMsg: 'Error signing in with password and email!' });
6366
console.error('Error signing in with password and email', error);
6467
});
6568
};
6669

67-
70+
validateUser = email => email.includes(
71+
process.env.REACT_APP_VERIFIED_EMAIL_SUBDOMAIN
72+
);
6873

6974
handleSubmit = () => {
7075
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-
})
76+
firebase.doSignInWithGoogle().then(data => {
77+
if (this.validateUser(data.user.email)) {
78+
const verifyCode = this.setVerificationCode();
79+
this.sendMail(app.auth().currentUser.email, verifyCode);
80+
this.props.history.push('/verify_code');
81+
} else {
82+
const error_msg = 'email sub-domain not allowed, only '+ process.env.REACT_APP_VERIFIED_EMAIL_SUBDOMAIN + ' allowed to login with Google.';
83+
toast.error(error_msg);
84+
this.props.history.push('/login');
85+
}
86+
});
7787
};
7888

7989
render() {
@@ -88,13 +98,13 @@ export class LoginPage extends React.Component {
8898
<img
8999
aria-hidden="true"
90100
className="object-cover w-full h-full dark:hidden"
91-
src={logo}
101+
src={ logo }
92102
alt="Office"
93103
/>
94104
<img
95105
aria-hidden="true"
96106
className="hidden object-cover w-full h-full dark:block"
97-
src={logo2}
107+
src={ logo2 }
98108
alt="Office"
99109
/>
100110
</div>
@@ -109,10 +119,10 @@ export class LoginPage extends React.Component {
109119
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"
110120
type="email"
111121
name="userEmail"
112-
value={this.state.userEmail}
122+
value={ this.state.userEmail }
113123
placeholder="E.g: faruq123@gmail.com"
114124
id="userEmail"
115-
onChange={event => this.handleChange(event)}
125+
onChange={ event => this.handleChange(event) }
116126
/>
117127
</label>
118128
<label className="block mt-4 text-sm">
@@ -121,33 +131,35 @@ export class LoginPage extends React.Component {
121131
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"
122132
type="password"
123133
name="userPassword"
124-
value={this.state.userPassword}
134+
value={ this.state.userPassword }
125135
placeholder="Your Password"
126136
id="userPassword"
127-
onChange={event => this.handleChange(event)}
137+
onChange={ event => this.handleChange(event) }
128138
/>
129139
</label>
130140

131141
<a
132142
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"
133143
href="../index.html"
134-
onClick={event => {
144+
onClick={ event => {
135145
this.signInWithEmailAndPasswordHandler(
136146
event,
137147
this.state.userEmail,
138148
this.state.userPassword
139149
);
140-
}}
150+
} }
141151
>
142152
Log in
143153
</a>
144154

145155
<hr className="my-8" />
146156
<Button
147157
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}
158+
onClick={ () => {
159+
this.handleSubmit();
160+
} }
161+
selected={ loading }
162+
color={ ButtonColor.Blue }
151163
>
152164
Login with Google
153165
</Button>

0 commit comments

Comments
 (0)