-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogin.tsx
More file actions
136 lines (127 loc) · 3.73 KB
/
Login.tsx
File metadata and controls
136 lines (127 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { useEffect, useState } from "react";
import {
IonButtons,
IonButton,
IonHeader,
IonToolbar,
IonTitle,
IonContent,
IonPage,
IonItem,
IonLabel,
IonInput,
IonAlert,
} from "@ionic/react";
import { useForm, Controller } from "react-hook-form";
import { useAuth } from "reactfire";
import { useHistory } from "react-router";
import { MyErrorDisplay } from "../components/MyErrorDisplay";
export interface CheckboxChangeEventDetail {
value: any;
checked: boolean;
}
const Login: React.FunctionComponent = () => {
const history = useHistory();
const [showErrorAlert, setShowErrorAlert] = useState("");
// SEE - https://github.com/FirebaseExtended/reactfire/issues/228
useEffect(() => {
let map = (globalThis as any)["_reactFirePreloadedObservables"];
map &&
Array.from(map.keys()).forEach(
(key: any) => key.includes("firestore") && map.delete(key)
);
}, []);
// from react-hook-form
// SEE - https://react-hook-form.com/
const { handleSubmit, control, errors } = useForm();
const auth = useAuth();
/**
* get data from form and sign the user in
*/
const signIn = async (data: any) => {
console.log(data);
try {
let r = await auth.signInWithEmailAndPassword(data.email, data.password);
history.replace("/home");
console.log(r);
} catch (e) {
setShowErrorAlert(e.message);
}
};
console.log(history);
return (
<IonPage>
<IonHeader>
<IonToolbar color="light">
<IonButtons slot="start" />
<IonTitle>Login</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent className="ion-padding">
{/** Show Error when problem Logging In **/}
<IonAlert
isOpen={showErrorAlert !== ""}
onDidDismiss={() => setShowErrorAlert("")}
header={"Firebase Error"}
subHeader={"Error Logging In"}
message={showErrorAlert}
buttons={["OK"]}
/>
<form onSubmit={handleSubmit(signIn)}>
<IonItem>
<IonLabel>Email</IonLabel>
<Controller
render={({ onChange }) => (
<IonInput type="email" onIonChange={onChange} />
)}
control={control}
defaultValue=""
autoComplete="new-password"
name="email"
rules={{
required: "'Email Address' is a required field",
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
message: "Invalid email address",
},
}}
/>
</IonItem>
<MyErrorDisplay errors={errors} elementName="email" />
<IonItem>
<IonLabel>Password</IonLabel>
<Controller
render={({ onChange }) => (
<IonInput
type="password"
onIonChange={onChange}
autocomplete="new-password"
/>
)}
control={control}
autoComplete="new-password"
name="password"
rules={{
required: "'Password' is a required field",
}}
/>
</IonItem>
<MyErrorDisplay errors={errors} elementName="password" />
<div className="ion-padding">
<IonButton expand="block" type="submit">
Log In
</IonButton>
<IonButton
expand="block"
type="button"
onClick={() => history.push("/create-account")}
>
Create Account
</IonButton>
</div>
</form>
</IonContent>
</IonPage>
);
};
export default Login;