@@ -3,15 +3,16 @@ import * as PropTypes from 'prop-types';
33
44import { connect } from 'react-redux' ;
55
6+ import app from 'firebase/app' ;
67import { loginUser } from '../actions/loginActions' ;
78import { getLoginState , isAuthenticated } from '../selectors/loginSelectors' ;
8- import app from 'firebase/app' ;
9-
109
1110import { Button , ButtonColor } from '../ui/Button' ;
1211import Firebase from './Firebase' ;
1312import logo from '../images/login-office.jpeg' ;
1413import logo2 from '../images/cardinal_logo.svg' ;
14+ import { toast } from 'react-toastify' ;
15+ toast . configure ( ) ;
1516
1617
1718export 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