-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
483 lines (458 loc) · 20.3 KB
/
index.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import React, { Component } from 'react';
import firebase from 'firebase'
import PropTypes from 'prop-types';
import { AsyncStorage } from 'react-native';
import { Toast } from 'native-base'
import R from 'ramda'
let instance = null;
class SetupSingleton{
constructor() {
if(!instance){
instance = this;
instance.setupReady = false
instance.user = null
instance.token = null
instance.provider = null
}
return instance;
}
async initialize(){
let user = await AsyncStorage.getItem('RNS:user')
let token = await AsyncStorage.getItem('RNS:token')
let provider = await AsyncStorage.getItem('RNS:provider')
instance.user = user && JSON.parse(user) || null
instance.token = token || null
instance.provider = provider || null
}
setSetupReady(){
instance.setupReady = true
return instance.setupReady
}
async setUser(user){
console.log('RNS setUser')
instance.user = user
await AsyncStorage.setItem('RNS:user', JSON.stringify(user))
return instance.user
}
async setToken(token){
console.log('RNS setToken')
instance.token = token
await AsyncStorage.setItem('RNS:token', token)
return instance.token
}
async setProvider(provider){
console.log('RNS setProvider')
instance.provider = provider
await AsyncStorage.setItem('RNS:provider', provider)
return instance.provider
}
async logout(){
console.log('RNS logout')
instance.user = null
instance.token = null
instance.provider = null
await AsyncStorage.removeItem('RNS:user')
await AsyncStorage.removeItem('RNS:token')
await AsyncStorage.removeItem('RNS:provider')
}
}
export default class RNSession extends Component {
state = {
user: null,
token: null,
provider: null,
}
constructor(props) {
super(props)
this.setup = new SetupSingleton()
let setup = this.setup
if(!props.config){
if(props.logsLevel > 0) console.log('firebase config not provided. login will not work')
}
else if (firebase.apps.length === 0) {
firebase.initializeApp(this.props.config);
}
if( !setup.setupReady ){
this.configure()
setup.setSetupReady()
}
//when changes are made on the instance's user, state should change
var interval = setInterval(() => {
if(setup.user && !this.state.user) this.setState({ user: setup.user })
else if(!setup.user && this.state.user) this.setState({ user: setup.user })
if(setup.token && !this.state.token) this.setState({ token: setup.token })
else if(!setup.token && this.state.token) this.setState({ token: setup.token })
if(setup.provider && !this.state.provider) this.setState({ provider: setup.provider })
else if(!setup.provider && this.state.provider) this.setState({ provider: setup.provider })
}, 50)
this.state = { user: setup.user, interval }
}
setUser(user){
if(!user) this.setup.logout()
else this.setup.setUser(user)
}
setToken(token){
this.setup.setToken(token)
}
setProvider(provider){
this.setup.setProvider(provider)
}
componentWillUnmount(){
clearInterval(this.state.interval)
}
async configure(){
//tries to restore old sessions
/*await AsyncStorage.getItem('RNS:user').then(res => {
if(res){
if(this.props.logsLevel > 2) console.log("oldSessionRestored", res);
//this.setUser(res)
if(this.props.onOldSessionRestored)
this.props.onOldSessionRestored(res)
else if(this.props.logsLevel > 0) console.log('onOldSessionRestored not provided')
}
}).catch(err => {
if(this.props.logsLevel > 1) console.log('oldSessionRestored error', err)
});*/
this.setup.initialize()
//
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
/*.then(function() {
// New sign-in will be persisted with local persistence.
return firebase.auth().signInWithEmailAndPassword(email, password);
})*/
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
//handles changes on firebase session state
firebase.auth().onAuthStateChanged(async user => {
if(this.props.logsLevel > 2) console.log('onAuthStateChanged', user)
this.setUser(user)
//pedir token
if(user && user.providerData.length === 1) {
if(this.props.onLogin) this.props.onLogin(user)
else if(this.props.logsLevel > 0) console.log('onLogin not provided')
}else{
if(this.props.onLogout) this.props.onLogout()
else if(this.props.logsLevel > 0) console.log('onLogout not provided')
}
});
}
login(email, pass) {
if(email === '') {
Toast.show({ text:
R.pathOr('you must input your email', ['language','auth/empty-email'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(email.indexOf('@') === -1 || email.indexOf('.') === -1) {
Toast.show({ text:
R.pathOr('invalid email', ['language','auth/invalid-email'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(pass === '') {
Toast.show({ text:
R.pathOr('you must input a password and the password confirmation', ['language','auth/empty-password'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.pass)
.then(res => { this.setProvider('email') })
.catch(error => {
var errorCode = error.code;
var errorMessage = error.message;
switch(errorCode){
case 'auth/invalid-email':
//Thrown if the email address is not valid.
Toast.show({ text:
R.pathOr(errorMessage, ['language','auth/auth/invalid-email'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/user-disabled':
//Thrown if the user corresponding to the given email has been disabled.
Toast.show({ text:
R.pathOr(errorMessage, ['language','auth/auth/user-disabled'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/user-not-found':
//Thrown if there is no user corresponding to the given email.
Toast.show({ text:
R.pathOr(errorMessage, ['language','auth/auth/user-not-found'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/wrong-password':
//Thrown if the password is invalid for the given email, or the account corresponding to the email does not have a password set.
Toast.show({ text:
R.pathOr(errorMessage, ['language','auth/auth/wrong-password'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
}
if(this.props.logsLevel > 1) console.log('signInWithEmailAndPassword error', error);
})
}
register(email, pass, pass_conf) {
if(typeof email !== 'string' && this.props.logsLevel > 1) console.log('email not a string')
if(typeof pass !== 'string' && this.props.logsLevel > 1) console.log('pass not a string')
if(typeof pass_conf !== 'string' && this.props.logsLevel > 1) console.log('pass_conf not a string')
if(email === '') {
Toast.show({ text:
R.pathOr('you must input your email', ['language','auth/empty-email'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(email.indexOf('@') === -1 || email.indexOf('.') === -1) {
Toast.show({ text:
R.pathOr('invalid email', ['language','auth/invalid-email'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(pass === '' || pass_conf === '') {
Toast.show({ text:
R.pathOr('you must input a password and the password confirmation', ['language','auth/empty-password-or-password-confirmation'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(pass.length < 6) {
Toast.show({ text:
R.pathOr('password is too short, it must be at least 6 characters long', ['language','auth/weak-password'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else if(pass !== pass_conf) {
Toast.show({ text:
R.pathOr('the password and password confirmation should be the same', ['language','auth/password-and-password-confirmation-not-equal'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}else {
this.registerWithMail(email, pass);
}
}
registerWithMail(email, pass) {
firebase.auth().createUserWithEmailAndPassword(email, pass)
.then(res => { this.setProvider('email') })
.catch(error => {
var errorCode = error.code;
var errorMessage = error.message;
switch(errorCode){
case 'auth/email-already-in-use':
//Thrown if there already exists an account with the given email address.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/email-already-in-use' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/invalid-email':
//Thrown if the email address is not valid.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-email' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/operation-not-allowed':
//Thrown if email/password accounts are not enabled. Enable email/password accounts in the Firebase Console, under the Auth tab.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/operation-not-allowed' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/weak-password':
//Thrown if the password is not strong enough.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/weak-password' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
}
if(this.props.logsLevel > 1 ) console.log('register error', error)
})
}
resetPassword( email ) {
firebase.auth().sendPasswordResetEmail( email )
.then(res => {
if(this.props.logsLevel > 2 ) console.log('sendPasswordResetEmail', res);
Toast.show({ text:
R.pathOr('E-mail sended to reset password', ['language','auth/reset-successful'], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
}).catch(error => {
if(this.props.logsLevel > 1 ) console.log('sendPasswordResetEmail error', err);
let errorCode = error.code
let errorMessage = error.message
switch(errorCode){
case 'auth/invalid-email':
//Thrown if the email address is not valid.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-email' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/missing-android-pkg-name':
//An Android package name must be provided if the Android app is required to be installed.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/missing-android-pkg-name' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/missing-continue-uri':
//A continue URL must be provided in the request.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/missing-continue-uri' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/missing-ios-bundle-id':
//An iOS Bundle ID must be provided if an App Store ID is provided.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/missing-ios-bundle-id' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/invalid-continue-uri':
//The continue URL provided in the request is invalid.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-continue-uri' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/unauthorized-continue-uri':
//The domain of the continue URL is not whitelisted. Whitelist the domain in the Firebase console.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/unauthorized-continue-uri' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
case 'auth/user-not-found':
//Thrown if there is no user corresponding to the email address.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/user-not-found' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break
}
});
}
async google() {
try {
const result = await Expo.Google.logInAsync({
androidClientId: '935866938730-5el1phcis1lvg32rpunv0lvoumfcggl5.apps.googleusercontent.com',
iosClientId: '935866938730-npthauqju6v96ueolrp3ei77jv4j8if2.apps.googleusercontent.com',
scopes: ['profile', 'email'],
});
if (result.type === 'success') {
//return result.accessToken;
this.setProvider('google')
this.setToken(result.idToken)
// Build Firebase credential with the Facebook access token.
const credential = firebase.auth.GoogleAuthProvider.credential(result.idToken);
// Sign in with credential from the Facebook user.
this.handleSocialErrors(firebase.auth().signInWithCredential(credential))
} else {
return {cancelled: true};
}
} catch(e) {
return {error: true};
}
}
async facebook() {
if(!this.props.facebookAppId){
Toast.show({ text:
R.pathOr('Facebook login is not working currently, try another form of login', ['language', 'auth/facebook-app-id-not-provided' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
if(this.props.logsLevel > 0) console.log('facebookAppId not provided')
return
}
const { type, token } = await Expo.Facebook.logInWithReadPermissionsAsync(this.props.facebookAppId, {
permissions: this.props.facebookPermissions? this.props.facebookPermissions : ['public_profile'],
});
if(this.props.logsLevel > 2 ) console.log('facebook return type', type)
if (type === 'success') {
// Get the user's name using Facebook's Graph API
this.setProvider('facebook')
this.setToken(token)
// Build Firebase credential with the Facebook access token.
const credential = firebase.auth.FacebookAuthProvider.credential(token);
// Sign in with credential from the Facebook user.
this.handleSocialErrors(firebase.auth().signInWithCredential(credential))
/*const response = await fetch(
`https://graph.facebook.com/me?access_token=${token}`);
Alert.alert(
'Logged in!',
`Hi ${(await response.json()).name}!`,
);*/
//email
//name
//age_range
//gender
//picture
//locale
}
}
handleSocialErrors(promise){
promise
.catch(error => {
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
switch(errorCode){
case 'auth/account-exists-with-different-credential':
//Thrown if there already exists an account with the email address asserted by the credential. Resolve this by calling firebase.auth.Auth#fetchProvidersForEmail and then asking the user to sign in using one of the returned providers. Once the user is signed in, the original credential can be linked to the user with firebase.User#linkWithCredential.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/account-exists-with-different-credential' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/invalid-credential':
//Thrown if the credential is malformed or has expired.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-credential' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/operation-not-allowed':
//Thrown if the type of account corresponding to the credential is not enabled. Enable the account type in the Firebase Console, under the Auth tab.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/operation-not-allowed' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/user-disabled':
//Thrown if the user corresponding to the given credential has been disabled.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/user-disabled' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/user-not-found':
//Thrown if signing in with a credential from firebase.auth.EmailAuthProvider#credential and there is no user corresponding to the given email.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/user-not-found' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/wrong-password':
//Thrown if signing in with a credential from firebase.auth.EmailAuthProvider#credential and the password is invalid for the given email, or if the account corresponding to the email does not have a password set.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/wrong-password' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/invalid-verification-code':
//Thrown if the credential is a firebase.auth.PhoneAuthProvider#credential and the verification code of the credential is not valid.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-verification-code' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
case 'auth/invalid-verification-id':
//Thrown if the credential is a firebase.auth.PhoneAuthProvider#credential and the verification ID of the credential is not valid.
Toast.show({ text:
R.pathOr(errorMessage, ['language', 'auth/invalid-verification-id' ], this.props)
, position: this.props.toastPosition, buttonText: this.props.toastText })
break;
}
if(this.props.logsLevel > 1 ) console.log('social error', error )
});
}
logout() {
firebase.auth().signOut().then(async () => {
if(this.props.onLogout) this.props.onLogout()
else if(this.props.logsLevel > 0) console.log('onLogout not provided')
}).catch(error => {
if(this.props.logsLevel > 1) console.log("Logout error", error);
});
}
render() {
return ''
}
}
RNSession.propTypes = {
config: PropTypes.object,
facebookAppId: PropTypes.string,
facebookPermissions: PropTypes.array,
onLogin: PropTypes.func,
onLogout: PropTypes.func,
toastPosition: PropTypes.string,
toastText: PropTypes.string,
logsLevel: PropTypes.number,
}
RNSession.defaultProps = {
toastPosition: 'bottom',
toastText: 'Ok',
//0: none
//1: most important, missing props
//2: errors
//3: all
logsLevel: 2,
}