-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthContext.js
61 lines (56 loc) · 1.52 KB
/
authContext.js
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
import * as React from "react";
import { useState, useContext, useCallback, useEffect } from "react";
import { Alert } from "react-native";
import Toast, { BaseToast } from "react-native-toast-message";
import { auth } from "./firebase";
export const AuthContext = React.createContext();
export function AuthProvider({ children }) {
const [email, setEmail] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState("");
const [error, setError] = useState("");
const handleSignup = () => {
auth
.createUserWithEmailAndPassword(email, password)
.then((userCredentials) => {
const user = userCredentials.user;
console.log("registered:", user.email);
})
.catch((error) => alert(error.message));
};
const handleLogin = () => {
auth
.signInWithEmailAndPassword(email, password)
.then((userCredentials) => {
const user = userCredentials.user;
console.log("logged in", user.email);
})
.catch((error) => {
console.log(error);
alert(error.message);
});
};
return (
<AuthContext.Provider
value={{
email,
password,
setEmail,
username,
setUsername,
setPassword,
loading,
handleSignup,
handleLogin,
error,
setError,
// success,
// setSuccess,
setLoading,
}}
>
{children}
</AuthContext.Provider>
);
}