forked from sitefinitysteve/nativescript-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth0.android.ts
95 lines (79 loc) · 3.12 KB
/
auth0.android.ts
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
import common = require("./auth0.common");
import * as util from "utils/utils";
import * as frameModule from "ui/frame";
import * as application from "application";
import * as appSettings from 'application-settings';
var localResolve;
var localReject;
var $this;
export class Auth0Lock extends common.Auth0Lock{
public _lock: any;
public _callback: any;
constructor(options: common.Options){
super(options);
}
public show() : Promise<any>{
return new Promise((resolve, reject) => {
try
{
localResolve = resolve;
localReject = reject;
$this = this;
this._callback = new AuthCallback();
var auth0 = new com.auth0.android.Auth0(this.options.clientId, this.options.domain);
auth0.setOIDCConformant(true);
var builder = com.auth0.android.lock.Lock.newBuilder(auth0, this._callback);
var activity = frameModule.topmost().android.activity;
//Add scope
if(this.options.scope){
var scopeItems = this.options.scope.join(" ");
console.log("Adding scope of " + scopeItems);
var paramBuilder = com.auth0.android.authentication.ParameterBuilder.newBuilder();
var authenticationParameters = paramBuilder.setScope(scopeItems).asDictionary();
builder.withAuthenticationParameters(authenticationParameters)
}
this._lock = builder.build(activity);
var context = util.ad.getApplicationContext();
var lockIntent = this._lock.newIntent(activity);
if (lockIntent.resolveActivity(context.getPackageManager()) != null) {
application.android.foregroundActivity.startActivity(lockIntent);
}
}
catch(args){
reject(args);
}
});
}
}
var AuthCallback = com.auth0.android.lock.AuthenticationCallback.extend({
onAuthentication: function(credentials){
console.log("Authentication Success");
debugger;
var accessToken = credentials.getAccessToken();
var idToken = credentials.getIdToken();
var refreshToken = credentials.getRefreshToken();
let creds: common.Credentials = {
accessToken: accessToken,
idToken: idToken,
refreshToken: refreshToken,
};
appSettings.setString(common.Auth0Lock._tokenKey, JSON.stringify(creds));
$this.refresh(); //hydrate the local object, sounds fancy
localResolve({
credentials: creds,
android: credentials
});
},
onCanceled: function(){
console.log("Cancelled, user pressed back!!!");
localReject(new Error("Cancelled"));
},
onError: function(error){
console.log("Exception occurred!!! " + error.getMessage());
localReject(new Error(error.getMessage()));
},
onDestroy: function(){
console.log("DESTROY");
}
});
exports.AuthCallback = AuthCallback;