-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplatform.js
93 lines (84 loc) · 2.53 KB
/
platform.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
const RingCentral = require('@ringcentral/sdk').SDK
var fs = require('fs')
var async = require("async");
require('dotenv').load()
function RCPlatform(mode) {
this.token_json = null
this.subscriptionId = ""
this.extensionId = ""
var cachePrefix = `user_${this.extensionId}`
var rcsdk = null
if (mode == "production"){
rcsdk = new RingCentral({
cachePrefix: cachePrefix,
server: RingCentral.server.production,
clientId: process.env.CLIENT_ID_PROD,
clientSecret:process.env.CLIENT_SECRET_PROD,
redirectUri: process.env.RC_APP_REDIRECT_URL,
})
}else if (mode == "sandbox"){
rcsdk = new RingCentral({
cachePrefix: cachePrefix,
server: RingCentral.server.sandbox,
clientId: process.env.CLIENT_ID_SB,
clientSecret:process.env.CLIENT_SECRET_SB,
redirectUri: process.env.RC_APP_REDIRECT_URL,
})
}
this.platform = rcsdk.platform()
this.platform.on(this.platform.events.loginSuccess, this.loginSuccess)
this.platform.on(this.platform.events.logoutSuccess, this.logoutSuccess)
this.platform.on(this.platform.events.refreshError, this.refreshError)
var boundFunction = ( async function() {
console.log("WONDERFUL")
console.log(this.extensionId);
}).bind(this);
this.platform.on(this.platform.events.refreshSuccess, boundFunction);
this.autoRefreshTimer = undefined
return this
}
RCPlatform.prototype = {
login: async function(code){
try{
var resp = await this.platform.login({
code: code,
redirectUri: process.env.RC_APP_REDIRECT_URL
})
var tokenObj = await this.platform.auth().data()
this.token_json = tokenObj
this.extensionId = tokenObj.owner_id
return this.extensionId
}catch(e) {
console.log('PLATFORM LOGIN ERROR ' + e.message || 'Server cannot authorize user');
return null
}
},
logout: function(){
this.platform.logout()
},
getPlatform: async function(){
console.log("getPlatform")
if (await this.platform.loggedIn()){
console.log("Logged in!")
return this.platform
}else{
console.log("BOTH TOKEN TOKENS EXPIRED")
console.log("CAN'T REFRESH")
return null
}
},
getSDKPlatform: function(){
return this.platform
},
loginSuccess: function(e){
console.log("Login success")
},
logoutSuccess: function(e){
console.log("logout Success")
},
refreshError: function(e){
console.log("refresh Error")
console.log("Error " + e.message)
}
}
module.exports = RCPlatform;