forked from sreevardhanreddi/bhagavad-gita-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
181 lines (157 loc) · 4.33 KB
/
auth.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useState, useEffect, useContext, createContext } from "react";
import queryString from "query-string";
import firebase from "./firebase";
import { createUser } from "./db";
const authContext = createContext();
// Context Provider component that wraps your app and makes auth object
// available to any child component that calls the useAuth() hook.
export function ProvideAuth({ children }) {
const auth = useProvideAuth();
return <authContext.Provider value={auth}>{children}</authContext.Provider>;
}
// Hook that enables any component to subscribe to auth state
export const useAuth = () => {
return useContext(authContext);
};
// Provider hook that creates auth object and handles state
function useProvideAuth() {
const [user, setUser] = useState(null);
// Handle a new user object (updates db and sets user state)
const handleUser = (rawUser) => {
if (rawUser) {
// Get user object in format expected by front-end
const user = formatUser(rawUser);
// Add or update user in database
createUser(user.uid, { email: user.email });
setUser(user);
return user;
} else {
setUser(false);
return false;
}
};
const signup = (email, password) => {
return firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((response) => handleUser(response.user));
};
const signin = (email, password) => {
return firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((response) => handleUser(response.user));
};
const signinWithProvider = (providerName) => {
const { providerMethod, parameters } = providers.find(
(p) => p.name === providerName
);
const provider = new providerMethod();
if (parameters) {
provider.setCustomParameters(parameters);
}
return firebase
.auth()
.signInWithPopup(provider)
.then((response) => handleUser(response.user));
};
const signout = () => {
return firebase
.auth()
.signOut()
.then(() => handleUser(false));
};
const sendPasswordResetEmail = (email) => {
return firebase
.auth()
.sendPasswordResetEmail(email)
.then(() => {
return true;
});
};
const confirmPasswordReset = (password, code) => {
// Get code from query string object
const resetCode = code || getFromQueryString("oobCode");
return firebase
.auth()
.confirmPasswordReset(resetCode, password)
.then(() => {
return true;
});
};
const updateEmail = (email) => {
return firebase
.auth()
.currentUser.updateEmail(email)
.then(() => {
handleUser(firebase.auth().currentUser);
});
};
const updatePassword = (password) => {
return firebase.auth().currentUser.updatePassword(password);
};
useEffect(() => {
// Subscribe to user on mount
const unsubscribe = firebase.auth().onAuthStateChanged(handleUser);
// Unsubscribe on cleanup
return () => unsubscribe();
}, []);
return {
user,
signup,
signin,
signinWithProvider,
signout,
sendPasswordResetEmail,
confirmPasswordReset,
updateEmail,
updatePassword,
};
}
// Format user object
// If there are extra fields you want from the original user
// object then you'd add those here.
const formatUser = (user) => {
return {
uid: user.uid,
email: user.email,
// Create an array containing the user's providers (password, google, etc).
providers: user.providerData.map(({ providerId }) => {
// Get the name for this providerId
return providers.find((p) => p.id === providerId).name;
}),
};
};
const providers = [
{
id: "password",
name: "password",
},
{
id: "google.com",
name: "google",
providerMethod: firebase.auth.GoogleAuthProvider,
},
{
id: "facebook.com",
name: "facebook",
providerMethod: firebase.auth.FacebookAuthProvider,
parameters: {
// Tell fb to show popup size UI instead of full website
display: "popup",
},
},
{
id: "twitter.com",
name: "twitter",
providerMethod: firebase.auth.TwitterAuthProvider,
},
{
id: "github.com",
name: "github",
providerMethod: firebase.auth.GithubAuthProvider,
},
];
const getFromQueryString = (key) => {
return queryString.parse(window.location.search)[key];
};