-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAuthService.js
61 lines (58 loc) · 1.81 KB
/
SimpleAuthService.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
'use strict'
//pull in modules for device storage and Parse functions and general object helpers
var AsyncStorage = require('react-native').AsyncStorage
var ParseModule = require('NativeModules').ParseModule
var _ = require('lodash');
//set keys for device storage
var userNameKey = 'userName';
var isAdminKey = 'isAdmin';
var leagueNameKey = 'leagueName';
class SimpleAuthService {
//authorize function to log user in
authorize(username, league){
//return a promise to allow promise chaining
return new Promise((resolve, reject) =>{
//call login function from parse helper
ParseModule.login( username, league, (user,isAdmin)=>{
//ParseModule.loginEmail(league, (user,isAdmin)=>{
//store username, isAdmin, and league on the device
AsyncStorage.multiSet([[userNameKey, user],
[isAdminKey, isAdmin.toString()],
[leagueNameKey, league]],
//callback function for AsyncStorage set
(err)=>
{
if(err){
reject(err);
}
//send object with user info
resolve({isAdmin, username, league});
})
})
})
}
//check device for user info which means they have logged in before
getAuthInfo(cb){
//get info with keys
AsyncStorage.multiGet([userNameKey, isAdminKey, leagueNameKey], (err, val) =>{
if(err){
return cb();
}
//if no values, return nothing
if(!val){
return cb();
}
else{
//unzip the val object to get to all the keys
var zippedObject = _.zipObject(val);
//if username is blank then no user so return nothing
if (!zippedObject[userNameKey]) {
return cb();
};
//otherwise return no error and each piece of the user data.
return cb(null, zippedObject[userNameKey], zippedObject[isAdminKey] == 'true'? true:false, zippedObject[leagueNameKey]);
}
})
}
}
module.exports = new SimpleAuthService();