forked from SolidStateGroup/react-native-firebase-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (109 loc) · 3.4 KB
/
index.js
File metadata and controls
134 lines (109 loc) · 3.4 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
import * as firebase from 'firebase';
import Auth from './auth';
const FireAuth = class {
user = null;
profile = null;
onUserChange = null;
onLogout = null;
onEmailVerified = null;
onLogin = null;
onError = null;
init(googleConfig) {
Auth.Google.configure(googleConfig);
}
setup = (onLogin, onUserChange, onLogout, onEmailVerified, onError) => {
this.onUserChange = onUserChange;
this.onLogout = onLogout;
this.onEmailVerified = onEmailVerified;
this.onLogin = onLogin;
this.onError = onError;
firebase.auth().onAuthStateChanged((user)=> {
if (user) {
// Determine if user needs to verify email
var emailVerified = !user.providerData || !user.providerData.length || user.providerData[0].providerId != 'password' || user.emailVerified;
// Upsert profile information
var profileRef = firebase.database().ref(`profiles/${user.uid}`);
profileRef.update({ emailVerified: emailVerified, email: user.email });
profileRef.on('value', (profile)=> {
const val = profile.val();
// Email become verified in session
if (val.emailVerified && (this.profile && !this.profile.val().emailVerified)) {
this.onEmailVerified && this.onEmailVerified();
}
if (!this.user) {
this.onLogin && this.onLogin(user, val); // On login
} else if (val) {
this.onUserChange && this.onUserChange(user, val); // On updated
}
this.profile = profile; // Store profile
this.user = user; // Store user
});
} else {
this.profile = null;
this.user = null; // Clear user and logout
this.onLogout && this.onLogout();
}
});
}
login = (email, password) => {
try {
firebase.auth().signInWithEmailAndPassword(email, password)
.catch((err) => this.onError && this.onError(err));
} catch (e) {
this.onError && this.onError(e);
}
}
register = (username, password) => {
try {
firebase.auth().createUserWithEmailAndPassword(username, password)
.then((user)=> {
user.sendEmailVerification();
})
.catch((err) => this.onError && this.onError(err));
} catch (e) {
this.onError && this.onError(e);
}
}
resendVerification = () => {
this.user.sendEmailVerification();
}
facebookLogin = (permissions) => {
Auth.Facebook.login(permissions)
.then((token) => (
firebase.auth()
.signInWithCredential(firebase.auth.FacebookAuthProvider.credential(token))
))
.catch((err) => this.onError && this.onError(err));
}
googleLogin = () => {
Auth.Google.login()
.then((token) => (
firebase.auth()
.signInWithCredential(firebase.auth.GoogleAuthProvider.credential(null, token))
))
.catch((err) => this.onError && this.onError(err));
}
logout = () => {
firebase.auth().signOut();
}
update = (data) => {
var profileRef = firebase.database().ref(`profiles/${this.user.uid}`);
return profileRef.update(data);
}
resetPassword = (email) => {
firebase.auth().sendPasswordResetEmail(email);
}
updatePassword = (password) => {
this.user.updatePassword(password);
}
linkWithGoogle = () => {
// @TODO
}
linkWithFacebook = () => {
// @TODO
}
linkWithEmail = () => {
// @TODO
}
};
export default new FireAuth();