Skip to content

Commit 5f12293

Browse files
committed
Temporarilly not working but working on auth
1 parent 025c733 commit 5f12293

File tree

13 files changed

+592
-234
lines changed

13 files changed

+592
-234
lines changed

dist/api.min.js

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/article.html

Lines changed: 188 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" />
66
<link rel="stylesheet" href="http://node-os.com/css/default.css" />
77
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
8-
<script type="text/javascript" src="api.min.js" ></script>
8+
<script type="text/javascript" src="https://gist.githubusercontent.com/formula1/749db638e5b5ac630caf/raw/2e07d087e9991213d40183f270d15f6d54244d39/api.min.js" ></script>
99
<style>
1010
#articles img{max-width:100%}
11+
#articles article .title img{width:100px}
1112
</style>
1213
</head>
1314
<body>
@@ -35,18 +36,17 @@
3536
<section ng-app="NodeOsBlog" id="articles" class="section">
3637
<div class="container" ng-controller="ErrorListCtrl" >
3738
<article ng-repeat="(index, error) in errors" class="row">
38-
<div class="col-sm-8 col-sm-offset-2 {{error.class}}" ng-mousedown="removeError(error)">
39+
<div class="col-sm-8 col-sm-offset-2 {{error.class}}" >
3940
<header class="alert alert-danger" role="alert">
4041
<h3 class="title">{{error.name}}</h3>
42+
<p ng-mousedown="removeError(error)" > To remove this message, click here</p>
4143
</header>
42-
<div class="content">{{error.message}}</div>
44+
<div class="content" ng-bind-html="error.message | to_trusted"></div>
4345
<footer>
4446
Don't worry, it probably was not your fault... probably....
4547
<br/><br/>
4648
Even if it was,
4749
<a target="_blank" href="https://github.com/formula1/NodeOS-Blog/issues" >just submit an issue :)</a>
48-
<br/><br/>
49-
To remove this message, click it.
5050
</footer>
5151
</div>
5252
</article>
@@ -134,7 +134,7 @@ <h3 class="footer-title">Share</h3>
134134
};
135135
}]);
136136
var errors = [];
137-
var is_authed = false;
137+
138138
function add403(){
139139
var topush = {
140140
class:"four-zero-three",
@@ -144,10 +144,11 @@ <h3 class="footer-title">Share</h3>
144144
topush.message = "Apparently, people have been using our app too much...";
145145
}else{
146146
topush.message = "If you'd like to continue, please "+
147-
"<button onclick='hello(\"github\").login()'>Log in</button>";
147+
"<button onclick='login()'>Log in</button>";
148148
}
149149
errors.push(topush);
150150
}
151+
151152
NodeOsBlog.controller('ErrorListCtrl', function($scope){
152153
$scope.removeError = function(error){
153154
var l = errors.length;
@@ -163,89 +164,206 @@ <h3 class="footer-title">Share</h3>
163164
}
164165
};
165166
});
166-
var hello = require("hello");
167-
hello.on("auth.login", function(auth){
168-
hello(auth.network).api("/me").then(function(r){
169-
document.querySelector(".four-zero-three .content").innerHTML("You've authenticated!");
167+
function parseMarkdown(item,next){
168+
jQuery.post("https://api.github.com/markdown",{text:item.body})
169+
.done(function(data){
170+
item.bodyHTML = data;
171+
next(item);
172+
}).fail(function(data, status, headers, config) {
173+
errors.push({name:"Bad markdown call: "+status, message: data.message});
174+
item.bodyHTML = "<pre>"+item.body+"</pre>";
175+
next(item);
170176
});
171-
});
177+
}
178+
/*
179+
var markdown = require("markdown").markdown;
180+
function parseMarkdown(item,next){
181+
console.log("parsing");
182+
183+
try{
184+
item.bodyHTML = markdown.toHTML(item.body);
185+
console.log(item.bodyHTML);
186+
}catch(e){
187+
console.log(e);
188+
item.bodyHTML = "<pre>"+item.body+"</pre>";
189+
}
190+
setTimeout(next.bind(next,item),1);
191+
}
192+
*/
193+
function CookieStore(id){
194+
this.id = id;
195+
var cook = document.cookie.split(";");
196+
var current;
197+
var i;
198+
for(i=0;i<cook.length;i++){
199+
if(cook[i].indexOf("=") != -1){
200+
current = cook[i].split("=");
201+
if(current[0].replace(/s/, "") == id){
202+
this.cookie = JSON.parse(decodeURIComponent(current[1]));
203+
return;
204+
}
205+
}
206+
}
207+
this.cookie = {};
208+
this.save();
209+
}
210+
211+
CookieStore.prototype.get = function(key){
212+
return this.cookie[key];
213+
};
214+
CookieStore.prototype.set = function(key, value){
215+
this.cookie[key] = value;
216+
this.save();
217+
return this;
218+
};
219+
CookieStore.prototype.delete = function(key){
220+
delete this.cookie[key];
221+
this.save();
222+
return this;
223+
};
224+
225+
CookieStore.prototype.save = function(){
226+
document.cookie = this.id+"="+encodeURIComponent(JSON.stringify(this.cookie))+";";
227+
};
228+
229+
//==========Auth Start===================
230+
231+
232+
var auth = new CookieStore("auth");
233+
var is_authed = -1;
234+
var auth_queueu = [];
235+
var url = require("url");
236+
var docuri = url.parse(window.location.href);
237+
if(docuri.query && docuri.query.code){
238+
if(url.query.state != auth.get("state")){
239+
errors.push(new Error("Improper State"));
240+
}else{
241+
getAccess(docuri.query.code);
242+
}
243+
}else if(auth.get("access_token")){
244+
is_authed = 1;
245+
}
246+
247+
var config = {
248+
cid: "dafb27cb88db35267e75",
249+
yqluri: "store://gdDAnJkTXAuVgzAQ8wboA2"
250+
};
251+
252+
function login(){
253+
var state = Date.now()+"_"+Math.random();
254+
auth.set("state", state);
255+
window.location.href = "https://github.com/login/oauth/authorize" +
256+
"?client_id="+config.cid +
257+
"&state="+state;
258+
}
259+
function getAccess(code){
260+
is_authed = 0;
261+
auth.delete("state");
262+
jQuery.get(compileYQL([
263+
"env \""+config.yqlur+"\"",
264+
"select * from github where CODE=\""+code+"\""
265+
])).done(function(results){
266+
auth.set("access_token",results.query.results.OAuth.access_token);
267+
is_authed = 1;
268+
jQuery(".four-zero-three .content").text("You've authenticated!");
269+
}).error(function(e){
270+
is_authed = -1;
271+
errors.push(e);
272+
}).finally(function(){
273+
while(auth_queue.length){
274+
uriAsAuthority.apply(void(0),auth_queue.pop());
275+
}
276+
});
277+
}
278+
279+
function compileYQL(query){
280+
if(Array.isArray(query)){
281+
query = query.join(";");
282+
}
283+
if(typeof query != "string"){
284+
throw new Error("A yql query must be a string");
285+
}
286+
return "https://query.yahooapis.com/v1/public/yql?q=" +
287+
encodeURIComponent(query) +
288+
"&diagnostics=true&format=json";
289+
}
290+
291+
function uriAsAuthority(uri,next){
292+
switch(is_authed){
293+
case -1: return next(uri);
294+
case 0: return auth_queueu.push([uri,next]);
295+
case 1: uri = url.parse(uri);
296+
uri.query.access_token = auth.get("access_token");
297+
return next(url.format(uri));
298+
}
299+
}
300+
301+
function logout(){
302+
auth.delete("access_token");
303+
is_authed = -1;
304+
}
172305
var hash = document.location.toString().split("#")[1];
173306
NodeOsBlog.controller('BlogSingleCtrl', function ($scope, $http) {
174307
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash;
175308
$scope.blog = [];
176309

177310
var markdown = require("markdown").markdown;
178-
$scope.parseMarkdown = function(item,next){
179-
try{
180-
item.bodyHTML = markdown.toHTML(item.body);
181-
}catch(e){
182-
item.bodyHTML = "<pre>"+item.body+"</pre>";
183-
}
184-
next(item);
185-
};
186-
$http.get('https://api.github.com/repos'+$scope.uriPath)
187-
.success(function(data,status,headers) {
188-
var l = data.labels.length;
189-
while(l--){
190-
if(data.labels[l].name === "blog"){
191-
break;
311+
uriAsAuthority('https://api.github.com/repos'+$scope.uriPath,function(uri){
312+
$http.get(uri).success(function(data,status,headers) {
313+
var l = data.labels.length;
314+
while(l--){
315+
if(data.labels[l].name === "blog"){
316+
break;
317+
}
192318
}
193-
}
194-
if(l < 0){
195-
return errors.push({
196-
name:"Trying to load a non-blog?",
197-
message: "I technically can't stop you since this is clientside."+
198-
" Hopefully my code feels clean enough to hack"
319+
if(l < 0){
320+
return errors.push({
321+
name:"Trying to load a non-blog?",
322+
message: "I technically can't stop you since this is clientside."+
323+
" Hopefully my code feels clean enough to hack"
324+
});
325+
}
326+
parseMarkdown(data,function(item){
327+
$scope.blog.push(item);
199328
});
200-
}
201-
$scope.parseMarkdown(data,function(item){
202-
$scope.blog.push(item);
329+
}).error(function(data, status, headers, config) {
330+
if(status === 403){
331+
add403();
332+
}else{
333+
errors.push({name:"Bad issues list request: "+status, message: data.message});
334+
}
203335
});
204-
}).error(function(data, status, headers, config) {
205-
if(status === 403){
206-
add403();
207-
}else{
208-
errors.push({name:"Bad issues list request: "+status, message: data.message});
209-
}
210336
});
211337
});
212338
NodeOsBlog.controller('CommentListCtrl', function ($scope, $http) {
213339
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash+"/comments";
214340
$scope.blog = [];
215-
$scope.parseMarkdown = function(item,next){
216-
$http.post("https://api.github.com/markdown",{text:item.body})
217-
.success(function(data){
218-
item.bodyHTML = data;
219-
next(item);
220-
}).error(function(data, status, headers, config) {
221-
errors.push({name:"Bad markdown call: "+status, message: data.message});
222-
item.bodyHTML = "<pre>"+item.body+"</pre>";
223-
next(item);
224-
});
225-
};
226341
$scope.last = void(0);
227342
$scope.loadMore = function(page){
228343
if($scope.last && $scope.last < page) return;
229344
var i=0;
230345
var l=-1;
231-
$http.get('https://api.github.com/repos'+$scope.uriPath+'?labels=blog&sort=created&page='+page)
232-
.success(function(data,status,headers) {
233-
if(!$scope.last) $scope.last = headers.link?headers.link.split("=").pop():1;
234-
l = data.length;
235-
if(i === l) return; //No more
236-
var iterator = function(item){
237-
$scope.blog.push(item);
238-
i++;
239-
if(i === l) return;
240-
$scope.parseMarkdown(data[i],iterator);
241-
};
242-
$scope.parseMarkdown(data[0],iterator);
243-
}).error(function(data, status, headers, config) {
244-
if(status === 403){
245-
add403();
246-
}else{
247-
errors.push({name:"Bad issues list request: "+status, message: data.message});
248-
}
346+
uriAsAuthority(
347+
'https://api.github.com/repos'+$scope.uriPath+'?labels=blog&sort=created&page='+page,
348+
function(uri){
349+
$http.get(is_authed).success(function(data,status,headers) {
350+
if(!$scope.last) $scope.last = headers.link?headers.link.split("=").pop():1;
351+
l = data.length;
352+
if(i === l) return; //No more
353+
var iterator = function(item){
354+
$scope.blog.push(item);
355+
i++;
356+
if(i === l) return;
357+
parseMarkdown(data[i],iterator);
358+
};
359+
parseMarkdown(data[0],iterator);
360+
}).error(function(data, status, headers, config) {
361+
if(status === 403){
362+
add403();
363+
}else{
364+
errors.push({name:"Bad issues list request: "+status, message: data.message});
365+
}
366+
});
249367
});
250368
};
251369
$scope.loadMore(1);

0 commit comments

Comments
 (0)