Skip to content

Commit

Permalink
fix: handle email verification bug
Browse files Browse the repository at this point in the history
  • Loading branch information
akmalhisyammm committed Dec 6, 2021
1 parent e9f9d7e commit 46681db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 63 deletions.
6 changes: 2 additions & 4 deletions src/contexts/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export const AuthProvider: React.FC = ({ children }) => {
const [loading, setLoading] = useState<boolean>(true);

useEffect(() => {
const unsubscribe = firebaseAuth.onAuthStateChanged((user: User | null) => {
setCurrentUser(user);
const unsubscribe = firebaseAuth.onIdTokenChanged((user: User | null) => {
setCurrentUser(user?.emailVerified ? user : null);
setLoading(false);
});

console.log(currentUser);

return unsubscribe;
}, [currentUser]);

Expand Down
79 changes: 22 additions & 57 deletions src/pages/auth/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { useRef, useContext, useState, KeyboardEvent } from 'react';
import { useHistory } from 'react-router-dom';
import { Redirect, useHistory } from 'react-router-dom';
import {
IonButton,
IonCard,
IonCardContent,
IonCardHeader,
IonCol,
IonContent,
IonGrid,
IonHeader,
IonIcon,
IonInput,
IonItem,
IonList,
IonModal,
IonRouterLink,
IonRow,
IonText,
IonTitle,
IonToolbar,
useIonLoading,
useIonToast,
} from '@ionic/react';
Expand All @@ -30,6 +25,7 @@ import { AuthContext } from 'contexts/auth';
import Layout from 'components/layout';

import styles from 'styles/auth/Login.module.scss';
import ForgotPasswordModal from 'components/auth/login/ForgotPasswordModal';

const Login: React.FC = () => {
const [presentLoading, dismissLoading] = useIonLoading();
Expand All @@ -41,10 +37,14 @@ const Login: React.FC = () => {
const passwordRef = useRef<HTMLIonInputElement>(null);
const forgotPasswordEmailRef = useRef<HTMLIonInputElement>(null);

const { login } = useContext(AuthContext);
const { currentUser, login } = useContext(AuthContext);

const history = useHistory();

if (currentUser) {
return <Redirect to="/" />;
}

const handleLogin = async () => {
const email = emailRef.current?.value;
const password = passwordRef.current?.value;
Expand Down Expand Up @@ -254,56 +254,21 @@ const Login: React.FC = () => {
</div>
</Layout>

<IonModal isOpen={isForgotPassword}>
<IonHeader>
<IonToolbar color="secondary">
<IonTitle>Atur Ulang Kata Sandi</IonTitle>
</IonToolbar>
</IonHeader>

<IonContent>
<IonGrid>
<IonRow>
<IonCol>
<IonItem>
<IonIcon icon={mailOutline} color="primary" slot="start" />
<IonInput
type="text"
placeholder="Masukkan email Anda"
ref={forgotPasswordEmailRef}
required
/>
</IonItem>
</IonCol>
</IonRow>

<IonRow className="ion-text-center">
<IonCol>
<IonButton
color="primary"
expand="block"
fill="solid"
shape="round"
onClick={handleRequestPasswordReset}
>
Kirim
</IonButton>
</IonCol>
<IonCol>
<IonButton
color="danger"
expand="block"
fill="outline"
shape="round"
onClick={() => setIsForgotPassword(false)}
>
Batal
</IonButton>
</IonCol>
</IonRow>
</IonGrid>
</IonContent>
</IonModal>
<ForgotPasswordModal
isOpen={isForgotPassword}
handleSendRequest={handleRequestPasswordReset}
onDismiss={setIsForgotPassword}
>
<IonItem>
<IonIcon icon={mailOutline} color="primary" slot="start" />
<IonInput
type="text"
placeholder="Masukkan email Anda"
ref={forgotPasswordEmailRef}
required
/>
</IonItem>
</ForgotPasswordModal>
</>
);
};
Expand Down
8 changes: 6 additions & 2 deletions src/pages/auth/Register.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRef, useState, useContext, KeyboardEvent } from 'react';
import { useHistory } from 'react-router-dom';
import { Redirect, useHistory } from 'react-router-dom';
import {
IonButton,
IonCard,
Expand Down Expand Up @@ -47,10 +47,14 @@ const Register: React.FC = () => {
const passwordRef = useRef<HTMLIonInputElement>(null);
const confirmPasswordRef = useRef<HTMLIonInputElement>(null);

const { register } = useContext(AuthContext);
const { currentUser, register } = useContext(AuthContext);

const history = useHistory();

if (currentUser) {
return <Redirect to="/" />;
}

const handleRegister = async () => {
const fullName = fullNameRef.current?.value;
const email = emailRef.current?.value;
Expand Down

0 comments on commit 46681db

Please sign in to comment.