This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
auth.js
128 lines (105 loc) · 2.82 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
import React, { useState, useEffect, useContext, createContext } from 'react';
import Router from 'next/router';
import cookie from 'js-cookie';
import firebase from './firebase';
import { createUser } from './db';
const authContext = createContext();
export function AuthProvider({ children }) {
const auth = useProvideAuth();
return <authContext.Provider value={auth}>{children}</authContext.Provider>;
}
export const useAuth = () => {
return useContext(authContext);
};
function useProvideAuth() {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const handleUser = async (rawUser) => {
if (rawUser) {
const user = await formatUser(rawUser);
const { token, ...userWithoutToken } = user;
createUser(user.uid, userWithoutToken);
setUser(user);
cookie.set('fast-feedback-auth', true, {
expires: 1
});
setLoading(false);
return user;
} else {
setUser(false);
cookie.remove('fast-feedback-auth');
setLoading(false);
return false;
}
};
const signinWithEmail = (email, password) => {
setLoading(true);
return firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((response) => {
handleUser(response.user);
Router.push('/sites');
});
};
const signinWithGitHub = (redirect) => {
setLoading(true);
return firebase
.auth()
.signInWithPopup(new firebase.auth.GithubAuthProvider())
.then((response) => {
handleUser(response.user);
if (redirect) {
Router.push(redirect);
}
});
};
const signinWithGoogle = (redirect) => {
setLoading(true);
return firebase
.auth()
.signInWithPopup(new firebase.auth.GoogleAuthProvider())
.then((response) => {
handleUser(response.user);
if (redirect) {
Router.push(redirect);
}
});
};
const signout = () => {
Router.push('/');
return firebase
.auth()
.signOut()
.then(() => handleUser(false));
};
useEffect(() => {
const unsubscribe = firebase.auth().onIdTokenChanged(handleUser);
return () => unsubscribe();
}, []);
return {
user,
loading,
signinWithEmail,
signinWithGitHub,
signinWithGoogle,
signout
};
}
const getStripeRole = async () => {
await firebase.auth().currentUser.getIdToken(true);
const decodedToken = await firebase.auth().currentUser.getIdTokenResult();
return decodedToken.claims.stripeRole || 'free';
};
const formatUser = async (user) => {
const token = await user.getIdToken();
return {
uid: user.uid,
email: user.email,
name: user.displayName,
provider: user.providerData[0].providerId,
photoUrl: user.photoURL,
stripeRole: await getStripeRole(),
token
};
};