forked from fullstackreact/react-native-firestack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
213 lines (184 loc) · 6.25 KB
/
Copy pathauth.js
File metadata and controls
213 lines (184 loc) · 6.25 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// @flow
import { NativeModules, NativeEventEmitter } from 'react-native';
import User from './user';
import { Base } from './base';
import { promisify } from '../utils';
const FirestackAuth = NativeModules.FirestackAuth;
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);
type AuthResultType = { authenticated: boolean, user: Object|null };
type CredentialType = { provider: string, token: string, secret: string };
export default class Auth extends Base {
_user: User|null;
_authResult: AuthResultType | null;
authenticated: boolean;
constructor(firestack: Object, options: Object = {}) {
super(firestack, options);
this._user = null;
this._authResult = null;
this.authenticated = false;
// start listening straight away
// generally though the initial event fired will get ignored
// but this is ok as we fake it with the getCurrentUser below
FirestackAuthEvt.addListener('listenForAuth', this._onAuthStateChanged.bind(this));
FirestackAuth.listenForAuth();
}
/**
* Internal auth changed listener
* @param auth
* @private
*/
_onAuthStateChanged(auth: AuthResultType) {
this._authResult = auth;
this.authenticated = auth ? auth.authenticated || false : false;
if (auth && auth.user && !this._user) this._user = new User(this, auth);
else if ((!auth || !auth.user) && this._user) this._user = null;
else if (this._user) this._user._updateValues(auth);
this.emit('onAuthStateChanged', this._authResult.user || null);
}
/*
* WEB API
*/
/**
* Listen for auth changes.
* @param listener
*/
onAuthStateChanged(listener: Function) {
this.log.info('Creating onAuthStateChanged listener');
this.on('onAuthStateChanged', listener);
if (this._authResult) listener(this._authResult.user || null);
return this._offAuthStateChanged.bind(this, listener);
}
/**
* Remove auth change listener
* @param listener
*/
_offAuthStateChanged(listener: Function) {
this.log.info('Removing onAuthStateChanged listener');
this.removeListener('onAuthStateChanged', listener);
}
/**
* Create a user with the email/password functionality
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise indicating the completion
*/
createUserWithEmailAndPassword(email: string, password: string): Promise<Object> {
this.log.info('Creating user with email and password', email);
return promisify('createUserWithEmail', FirestackAuth)(email, password);
}
/**
* Sign a user in with email/password
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise that is resolved upon completion
*/
signInWithEmailAndPassword(email: string, password: string): Promise<Object> {
this.log.info('Signing in user with email and password', email);
return promisify('signInWithEmail', FirestackAuth)(email, password);
}
// TODO move user methods to User class
/**
* Update the current user's email
* @param {string} email The user's _new_ email
* @return {Promise} A promise resolved upon completion
*/
updateEmail(email: string): Promise<Object> {
return promisify('updateUserEmail', FirestackAuth)(email);
}
/**
* Send verification email to current user.
*/
sendEmailVerification(): Promise<Object> {
return promisify('sendEmailVerification', FirestackAuth)();
}
/**
* Update the current user's password
* @param {string} password the new password
* @return {Promise}
*/
updatePassword(password: string): Promise<Object> {
return promisify('updateUserPassword', FirestackAuth)(password);
}
/**
* Update the current user's profile
* @param {Object} updates An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
* @return {Promise}
*/
updateProfile(updates: Object = {}): Promise<Object> {
return promisify('updateUserProfile', FirestackAuth)(updates);
}
/**
* Sign the user in with a custom auth token
* @param {string} customToken A self-signed custom auth token.
* @return {Promise} A promise resolved upon completion
*/
signInWithCustomToken(customToken: string): Promise<Object> {
return promisify('signInWithCustomToken', FirestackAuth)(customToken);
}
/**
* Sign the user in with a third-party authentication provider
* @return {Promise} A promise resolved upon completion
*/
signInWithCredential(credential: any): Promise<Object> {
return promisify('signInWithProvider', FirestackAuth)(credential);
}
/**
* Re-authenticate a user with a third-party authentication provider
* @return {Promise} A promise resolved upon completion
*/
reauthenticateUser(credential: any): Promise<Object> {
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(credential);
}
/**
* Sign a user in anonymously
* @return {Promise} A promise resolved upon completion
*/
signInAnonymously(): Promise<Object> {
return promisify('signInAnonymously', FirestackAuth)();
}
/**
* Send reset password instructions via email
* @param {string} email The email to send password reset instructions
*/
sendPasswordResetEmail(email: string): Promise<Object> {
return promisify('sendPasswordResetWithEmail', FirestackAuth)(email);
}
/**
* Delete the current user
* @return {Promise}
*/
deleteUser(): Promise<Object> {
return promisify('deleteUser', FirestackAuth)();
}
/**
* get the token of current user
* @return {Promise}
*/
getToken(): Promise<Object> {
return promisify('getToken', FirestackAuth)();
}
/**
* Sign the current user out
* @return {Promise}
*/
signOut(): Promise<Object> {
return promisify('signOut', FirestackAuth)();
}
/**
* Get the currently signed in user
* @return {Promise}
*/
getCurrentUser(): Promise<Object> {
return promisify('getCurrentUser', FirestackAuth)();
}
/**
* Get the currently signed in user
* @return {Promise}
*/
get currentUser(): User|null {
return this._user;
}
get namespace(): string {
return 'firestack:auth';
}
}