-
Notifications
You must be signed in to change notification settings - Fork 0
/
freshvine.devicecode.js
184 lines (158 loc) · 6.26 KB
/
freshvine.devicecode.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
/*!
* Fresh Vine API Device Code javascript library
* The functional side of using a device code apprach for app authenication with the Fresh Vine API.
*
* @author Paul Prins
* @link http://freshvine.co/
* @version 1.0
* @requires jQuery v1.7 or later
*
* Find instructions and source on GitHub: https://github.com/FreshVine/Fresh-Vine-API-js
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
var FV_code_response = undefined,
FV_code_polling_timer,
FV_code_state = 'inactive'; // Possible States 'fetching','displaying','inactive'
if( typeof FV_API_SCOPE == 'undefined' ){ var FV_API_SCOPE = ''; }
var FV_DeviceCodeRegister = function( StateValue ){
FV_ConnectionOnline(); // Ensure we set the system as online
if( typeof FV_code_response == 'object' ){ return; } // We're already doing it
if( FV_code_state == 'fetching' ){ return; } // Currenting running this step
FV_DeviceCode_UI_Show(); // Fire up the UI
FV_removeItem('FV_api_key'); // Ensure the current key has been removed
if( typeof StateValue === 'undefined' )
StateValue = '';
syncTimer = 0; // Clear the sync timer
var endPoint = 'auth/key/device/code';
if( FV_API_AUTH_APPROACH === 'token' ){ endPoint = 'auth/oauth/device/code'; }
var thisData = {'FVappid': FV_API_CLIENT_ID,
'FVsecret': FV_API_CLIENT_SECRET,
'state': StateValue, // this is just for us
'scope': FV_API_SCOPE };
//
// Queue up a polling check
clearTimeout( FV_code_polling_timer );
FV_MakeRequest( endPoint, 'POST', thisData,
function( data, textStatus, jqXHR ) {
if ( typeof FV_DeviceCode_UI_ShowCode == 'function' ) {
syncTimer = 0; // Clear the timer
FV_DeviceCode_UI_ShowCode( data.user_code );
}
FV_code_polling_timer = setTimeout(function(){ FV_DeviceCodeRegisterCheck(); }, data.interval * 1050 );
console.log( 'FV_DeviceCodeRegister - Made it here');
FV_code_response = data;
FV_setItem('FV_user_code', data.user_code);
FV_setItem('FV_device_code', data.device_code);
return;
},
function(data, textStatus, errorThrown ){
console.log('Error getting a device code', data, textStatus);
FV_code_polling_timer = setTimeout(function(){
syncTimer = 0;
FV_DeviceCodeRegister();
}, 10 * 1050 ); // Set this to 10 seconds, to try again
return;
}, true );
},
FV_DeviceCodeRegisterCheck = function(){
FV_ConnectionOnline(); // Ensure we set the system as online
var endPoint = 'auth/key/device/polling';
if( FV_API_AUTH_APPROACH === 'token' )
var endPoint = 'auth/oauth/device/polling';
var thisData = {'FVappid': FV_API_CLIENT_ID,
'FVsecret': FV_API_CLIENT_SECRET,
'device_code': FV_code_response.device_code };
clearTimeout( FV_code_polling_timer );
FV_MakeRequest( endPoint, 'POST', thisData,
function( data, textStatus, jqXHR ) {
console.log( 'FV_DeviceCodeRegisterCheck Made it here');
//
// We're polling and polling like a champ
if( typeof data.error === 'string' ){
switch( data.error ){
case 'auth_pending':
case 'slow_down':
// Keep the loop going but at a slower pace
clearTimeout( FV_code_polling_timer );
FV_code_polling_timer = setTimeout(function(){ FV_DeviceCodeRegisterCheck(); }, FV_code_response.interval * 1500 );
break;
case 'auth_expired':
FV_removeItem('FV_user_code'); // Clear the user code
FV_code_response = undefined;
if ( typeof FV_DeviceCodeRegister == 'function' ) { FV_DeviceCodeRegister( ); }
break;
case 'auth_rejected':
FV_removeItem('FV_user_code'); // Clear the user code
FV_code_response = undefined;
if ( typeof FV_DeviceCodeRegister == 'function' ) { FV_DeviceCodeRegister( ); }
break;
case 'invalid':
FV_removeItem('FV_user_code'); // Clear the user code
FV_code_response = undefined;
if ( typeof FV_DeviceCodeRegister == 'function' ) { FV_DeviceCodeRegister( ); }
break;
case 'destoryed':
FV_removeItem('FV_user_code'); // Clear the user code
FV_code_response = undefined;
if ( typeof FV_DeviceCodeRegister == 'function' ) { FV_DeviceCodeRegister( ); }
break;
}
return;
}
FV_removeItem('FV_device_code'); // Clear the user code
FV_removeItem('FV_user_code'); // Clear the user code
if( typeof data.access_token === 'string' ){
FV_code_response = undefined;
clearTimeout( FV_code_polling_timer );
FV_setItem('FV_access_token', data.access_token);
FV_setItem('FV_refresh_token', data.refresh_token);
if ( typeof FV_DeviceCode_UI_Hide == 'function' ) {
syncTimer = 0;
FV_DeviceCode_UI_Hide( data.device_name );
}
}
if( typeof data.access_key === 'string' ){
FV_code_response = undefined;
clearTimeout( FV_code_polling_timer );
FV_setItem('FV_api_key', data.access_key);
if ( typeof FV_DeviceCode_UI_Hide == 'function' ) {
syncTimer = 0;
FV_DeviceCode_UI_Hide( data.device_name );
}
}
// if
},
function(jqXHR, textStatus, errorThrown ){
console.log('Error getting a device token');
FV_code_response = undefined;
FV_removeItem('FV_user_code'); // Clear the user code
if ( typeof FV_DeviceCode_UI_Show == 'function' ) { syncTimer = 0; FV_DeviceCode_UI_Show(); }
}, true);
};
if( typeof FV_DeviceCode_UI_Show !== 'function' ){
/*
* Function called when the device code process starts - allows you to prep the user
*/
var FV_DeviceCode_UI_Show = function(){
};
}
if( typeof FV_DeviceCode_UI_ShowCode !== 'function' ){
/*
* Function called when the device code is retreived from the Fresh Vine api.
* You must somehow display the device code to the User.
*/
FV_DeviceCode_UI_ShowCode = function( deviceCode ){
// $('.deviceCode').html( deviceCode.substr(0, 3) + ' ' + deviceCode.substr(3) );
};
}
if( typeof FV_DeviceCode_UI_Hide !== 'function' ){
/*
* Function called when the device code has been successfully registered You can hide the UI and trigger other events
* Also supplies the device name in
*/
FV_DeviceCode_UI_Hide = function( deviceName ){
};
}