@@ -126,26 +126,28 @@ <h3 class="footer-title">Share</h3>
126
126
< script type ="text/javascript " src ="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js " > </ script >
127
127
<!-- START: BIND DATA TO VIEW -->
128
128
< script type ="text/javascript ">
129
-
130
129
var NodeOsBlog = angular . module ( 'NodeOsBlog' , [ ] ) ;
131
- NodeOsBlog . filter ( 'to_trusted' , [ '$sce' , function ( $sce ) {
132
- return function ( text ) {
133
- return $sce . trustAsHtml ( text ) ;
134
- } ;
135
- } ] ) ;
136
- var errors = [ ] ;
130
+ NodeOsBlog . filter ( 'to_trusted' , [ '$sce' , function ( $sce ) {
131
+ return function ( text ) {
132
+ return $sce . trustAsHtml ( text ) ;
133
+ } ;
134
+ } ] ) ;
135
+ var errors = [ ] ;
136
+ var has403 = false ;
137
137
138
138
function add403 ( ) {
139
+ if ( has403 ) return ;
140
+ has403 = true ;
139
141
var topush = {
140
142
class :"four-zero-three" ,
141
143
name :"You've hit max data"
142
144
} ;
143
- if ( is_authed === 1 ) {
145
+ if ( user . isLoggedIn ) {
144
146
topush . message = "Apparently, people have been using our app too much..." +
145
- "<button onclick='login()'>Log in</button>" ;
147
+ "<button onclick='user. login()'>Log in</button>" ;
146
148
} else {
147
149
topush . message = "If you'd like to continue, please " +
148
- "<button onclick='login()'>Log in</button>" ;
150
+ "<button onclick='user. login()'>Log in</button>" ;
149
151
}
150
152
errors . push ( topush ) ;
151
153
}
@@ -167,7 +169,7 @@ <h3 class="footer-title">Share</h3>
167
169
} ) ;
168
170
/* */
169
171
function parseMarkdown ( item , next ) {
170
- uriAsAuthority ( "https://api.github.com/markdown/raw" , function ( uri ) {
172
+ user . asAuthority ( "https://api.github.com/markdown/raw" , function ( uri ) {
171
173
jQuery . ajax ( {
172
174
url :uri ,
173
175
type :'POST' ,
@@ -177,7 +179,7 @@ <h3 class="footer-title">Share</h3>
177
179
data :item . body
178
180
} ) . done ( function ( data ) {
179
181
item . bodyHTML = data ;
180
- } ) . fail ( function ( response , type , title , config ) {
182
+ } ) . fail ( function ( response , textStatus , data ) {
181
183
if ( response . status === 403 ) {
182
184
add403 ( ) ;
183
185
} else {
@@ -208,102 +210,6 @@ <h3 class="footer-title">Share</h3>
208
210
setTimeout(next.bind(next,item),1);
209
211
}
210
212
/* */
211
- function CookieStore ( id ) {
212
- this . id = id ;
213
- var cook = document . cookie . split ( ";" ) ;
214
- var current ;
215
- var i ;
216
- for ( i = 0 ; i < cook . length ; i ++ ) {
217
- if ( cook [ i ] . indexOf ( "=" ) != - 1 ) {
218
- current = cook [ i ] . split ( "=" ) ;
219
- if ( current [ 0 ] . replace ( / s / , "" ) == id ) {
220
- this . cookie = JSON . parse ( decodeURIComponent ( current [ 1 ] ) ) ;
221
- return ;
222
- }
223
- }
224
- }
225
- this . cookie = { } ;
226
- this . save ( ) ;
227
- }
228
-
229
- CookieStore . prototype . get = function ( key ) {
230
- return this . cookie [ key ] ;
231
- } ;
232
- CookieStore . prototype . set = function ( key , value ) {
233
- this . cookie [ key ] = value ;
234
- this . save ( ) ;
235
- return this ;
236
- } ;
237
- CookieStore . prototype . delete = function ( key ) {
238
- delete this . cookie [ key ] ;
239
- this . save ( ) ;
240
- return this ;
241
- } ;
242
-
243
- CookieStore . prototype . save = function ( ) {
244
- document . cookie = this . id + "=" + encodeURIComponent ( JSON . stringify ( this . cookie ) ) + ";" ;
245
- } ;
246
-
247
- //==========Auth Start===================
248
-
249
-
250
- var auth = new CookieStore ( "auth" ) ;
251
- var is_authed = - 1 ;
252
- var auth_queue = [ ] ;
253
- var url = require ( "url" ) ;
254
- var querystring = require ( "querystring" ) ;
255
- var docuri = url . parse ( window . location . href ) ;
256
- docuri . query = querystring . parse ( docuri . query ) ;
257
- var config = {
258
- cid : "dafb27cb88db35267e75" ,
259
- yqluri : "store://gdDAnJkTXAuVgzAQ8wboA2"
260
- } ;
261
- if ( docuri . query && docuri . query . code ) {
262
- if ( docuri . query . state != auth . get ( "state" ) ) {
263
- errors . push ( new Error ( "Improper State" ) ) ;
264
- } else {
265
- getAccess ( docuri . query . code ) ;
266
- }
267
- } else if ( auth . get ( "access_token" ) ) {
268
- console . log ( auth . get ( "access_token" ) ) ;
269
- is_authed = 1 ;
270
- }
271
-
272
- function login ( ) {
273
- var state = Date . now ( ) + "_" + Math . random ( ) ;
274
- auth . set ( "state" , state ) ;
275
- window . location . href = "https://github.com/login/oauth/authorize" +
276
- "?client_id=" + config . cid +
277
- "&state=" + state ;
278
- }
279
- function getAccess ( code ) {
280
- is_authed = 0 ;
281
- auth . delete ( "state" ) ;
282
- jQuery . get ( compileYQL ( [
283
- "env \"" + config . yqluri + "\"" ,
284
- "select * from github where CODE=\"" + code + "\""
285
- ] ) ) . done ( function ( results ) {
286
- console . log ( "auth success" ) ;
287
- console . log ( arguments ) ;
288
- auth . set ( "access_token" , results . query . results . OAuth . access_token ) ;
289
- is_authed = 1 ;
290
- jQuery ( ".four-zero-three .content" ) . text ( "You've authenticated!" ) ;
291
- } ) . fail ( function ( e ) {
292
- console . error ( arguments ) ;
293
- is_authed = - 1 ;
294
- errors . push ( {
295
- class :"four-zero-three" ,
296
- name :"You've failed authorization" ,
297
- message : "You can always try again"
298
- } ) ;
299
- } ) . always ( function ( ) {
300
- console . log ( "always" ) ;
301
- while ( auth_queue . length ) {
302
- uriAsAuthority . apply ( void ( 0 ) , auth_queue . pop ( ) ) ;
303
- }
304
- } ) ;
305
- }
306
-
307
213
function compileYQL ( query ) {
308
214
if ( Array . isArray ( query ) ) {
309
215
query = query . join ( ";" ) ;
@@ -315,30 +221,63 @@ <h3 class="footer-title">Share</h3>
315
221
encodeURIComponent ( query ) +
316
222
"&diagnostics=true&format=json" ;
317
223
}
224
+ function yql_access ( yql_uri ) {
225
+ return function ( code , next ) {
226
+ var url ;
227
+ try {
228
+ url = compileYQL ( [
229
+ "env \"" + yql_uri + "\"" ,
230
+ "select * from github where CODE=\"" + code + "\""
231
+ ] ) ;
232
+ } catch ( e ) {
233
+ return setTimeout ( next . bind ( next , e ) , 1 ) ;
234
+ }
318
235
319
- function uriAsAuthority ( uri , next ) {
320
- if ( is_authed === - 1 ) return next ( uri ) ;
321
- if ( is_authed === 0 ) return auth_queue . push ( [ uri , next ] ) ;
322
- uri = url . parse ( uri ) ;
323
- uri . query = querystring . parse ( uri . query ) ;
324
- uri . query . access_token = auth . get ( "access_token" ) ;
325
- uri . search = "?" + querystring . stringify ( uri . query ) ;
326
- next ( url . format ( uri ) ) ;
236
+ jQuery . ajax ( url ) . done ( function ( results ) {
237
+ next ( void ( 0 ) , results . query . results . OAuth . access_token ) ;
238
+ } ) . fail ( function ( x , status , e ) {
239
+ next ( e ) ;
240
+ } ) ;
241
+ } ;
327
242
}
328
243
329
- function logout ( ) {
330
- auth . delete ( "access_token" ) ;
331
- is_authed = - 1 ;
332
- }
244
+ var config = {
245
+ cid : "dafb27cb88db35267e75" ,
246
+ yqluri : "store://gdDAnJkTXAuVgzAQ8wboA2"
247
+ } ;
248
+
249
+ var AuthProvider = require ( "AuthProvider" ) ;
250
+
251
+ window . user = new AuthProvider (
252
+ config . cid ,
253
+ yql_access ( config . yqluri )
254
+ ) ;
255
+ user . on ( "parse-error" , function ( e ) {
256
+ errors . push ( {
257
+ class :"four-zero-three" ,
258
+ name :"You've failed authorization" ,
259
+ message : e
260
+ } ) ;
261
+ } ) ;
262
+ user . on ( "access-error" , function ( e ) {
263
+ errors . push ( {
264
+ class :"four-zero-three" ,
265
+ name :"You've failed authorization" ,
266
+ message : "You can always try again"
267
+ } ) ;
268
+ } ) ;
269
+ user . on ( "access-success" , function ( token ) {
270
+ jQuery ( ".four-zero-three .content" ) . text ( "You've authenticated!" ) ;
271
+ } ) ;
333
272
var hash = document . location . toString ( ) . split ( "#" ) [ 1 ] ;
334
273
NodeOsBlog . controller ( 'BlogSingleCtrl' , function ( $scope , $http ) {
335
274
$scope . uriPath = "/NodeOS/NodeOS/issues/" + hash ;
275
+ $scope . user = user ;
336
276
$scope . blog = [ ] ;
337
- $scope . uriAsAuthority = uriAsAuthority ;
338
277
$scope . parseMarkdown = parseMarkdown ;
339
278
340
279
var markdown = require ( "markdown" ) . markdown ;
341
- $scope . uriAsAuthority ( 'https://api.github.com/repos' + $scope . uriPath , function ( uri ) {
280
+ $scope . user . asAuthority ( 'https://api.github.com/repos' + $scope . uriPath , function ( uri ) {
342
281
$http . get ( uri ) . success ( function ( data , status , headers ) {
343
282
var l = data . labels . length ;
344
283
while ( l -- ) {
@@ -368,15 +307,15 @@ <h3 class="footer-title">Share</h3>
368
307
} ) ;
369
308
NodeOsBlog . controller ( 'CommentListCtrl' , function ( $scope , $http ) {
370
309
$scope . uriPath = "/NodeOS/NodeOS/issues/" + hash + "/comments" ;
310
+ $scope . user = user ;
371
311
$scope . blog = [ ] ;
372
312
$scope . last = void ( 0 ) ;
373
- $scope . uriAsAuthority = uriAsAuthority ;
374
313
$scope . parseMarkdown = parseMarkdown ;
375
314
$scope . loadMore = function ( page ) {
376
315
if ( $scope . last && $scope . last < page ) return ;
377
316
var i = 0 ;
378
317
var l = - 1 ;
379
- $scope . uriAsAuthority (
318
+ $scope . user . asAuthority (
380
319
'https://api.github.com/repos' + $scope . uriPath + '?labels=blog&sort=created&page=' + page ,
381
320
function ( uri ) {
382
321
$http . get ( uri ) . success ( function ( data , status , headers ) {
0 commit comments