-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgoogle.android.js
More file actions
executable file
·149 lines (102 loc) · 4.7 KB
/
google.android.js
File metadata and controls
executable file
·149 lines (102 loc) · 4.7 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
var Application = require("@nativescript/core").Application
var RC_SIGN_IN = 9001
var GoogleSignInOptions = com.google.android.gms.auth.api.signin.GoogleSignInOptions
var GoogleSignIn = com.google.android.gms.auth.api.signin.GoogleSignIn
var GoogleSignInClient = com.google.android.gms.auth.api.signin.GoogleSignInClient
var Task = com.google.android.gms.tasks.Task
var OnCanceledListener = com.google.android.gms.tasks.OnCanceledListener
var OnCompleteListener = com.google.android.gms.tasks.OnCompleteListener
var OnFailureListener = com.google.android.gms.tasks.OnFailureListener
var Google = function(){
var scopes = [ "profile", "email" ]
// args = {scopes, shouldFetchBasicProfile, clientID}
Google.initSdk = function(args) {
var self = this
var activity = Application.android.foregroundActivity || Application.android.startActivity;
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
var gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestId()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
this._googleApiClient = GoogleSignIn.getClient(Application.android.context, gso)
console.log("## initSdk")
}
Google.registerCallback = function(successCallback, failCallback){
this._successCallback = successCallback
this._failCallback = failCallback
}
Google.handleSignInResult = function(task) {
console.log("## handleSignInResult = " + task.isSuccessful())
try{
var account = task.getResult()
// Signed in successfully, show authenticated UI.
if(this._successCallback)
this._successCallback(account);
if(this._profileInfoCallback){
var result = {
userId: account.getId(), // For client-side use only!
idToken: account.getIdToken(), // Safe to send to the server
fullName: account.getDisplayName(),
email: account.getEmail(),
}
this._profileInfoCallback(result)
}
}catch(err){
console.log("handleSignInResult error: " + err)
if(this._failCallback)
this._failCallback("Error: " + err);
}
}
Google.logOut = function(callback){
if(this._googleApiClient)
this._googleApiClient.signOut()
}
Google.logIn = function(profileInfoCallback, failCallback){
this._profileInfoCallback = profileInfoCallback
if(failCallback)
this._failCallback = failCallback
var signInIntent = this._googleApiClient.getSignInIntent();
var act = Application.android.foregroundActivity || Application.android.startActivity;
var self = this
Application.android.on("activityResult", function(eventData) {
Application.android.off("activityResult")
if (eventData.requestCode === RC_SIGN_IN) {
var task = GoogleSignIn.getSignedInAccountFromIntent(eventData.intent)
task.addOnCanceledListener(new OnCanceledListener({
onCanceled: function(){
console.log("login cancelled")
}
}))
task.addOnFailureListener(new OnFailureListener({
onFailure: function(err){
console.log("google login error: " + err)
if(self._failCallback)
self._failCallback("Login error: " + err)
}
}))
task.addOnCompleteListener(new OnCompleteListener({
onComplete: function(task0){
if(task0.isCanceled()){
console.log("login cancelled")
}else{
self.handleSignInResult(task0);
}
}
}))
}else{
console.log("cannot handle activityResult code " + eventData.requestCode
+ ", expected is " + RC_SIGN_IN)
}
})
act.startActivityForResult(signInIntent, RC_SIGN_IN);
}
Google.isLoggedIn = function(){
var account = GoogleSignIn.getLastSignedInAccount(this)
return !!account
}
return Google
}
exports.Google = Google