Skip to content

Commit e4f3658

Browse files
committed
Tweak life token example js
1 parent c15530e commit e4f3658

File tree

5 files changed

+79
-27
lines changed

5 files changed

+79
-27
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ gem 'yajl-ruby', '1.1.0'
1515
gem 'listen', '1.2.2'
1616
gem 'guard-nanoc', '1.0.1'
1717
gem 'rb-readline'
18-
gem 'w3c_validators'
18+
gem 'w3c_validators'

Rules

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
1414
# because “*” matches zero or more characters.
1515

16+
compile '/assets/js/*' do
17+
filter :erb
18+
end
19+
1620
compile '/static/*' do
1721
end
1822

@@ -39,6 +43,10 @@ route '/static/*' do
3943
'/assets'+item.identifier[7..-2]
4044
end
4145

46+
route '/assets/js/*' do
47+
item.identifier.chop + '.' + item[:extension]
48+
end
49+
4250
route '*' do
4351
if item.binary?
4452
# Write item with identifier /foo/ to /foo.ext

content/assets/js/app.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
(function () {
2+
var _token = null;
3+
var verify_token = function (token) {
4+
if (token && token.match(/^[\-_a-zA-Z0-9]{98}$/)) {
5+
return $.ajax({
6+
url: "https://alpha-api.<%= lanai_hostname %>/stream/0/token",
7+
headers: {
8+
"Authorization": "BEARER " + token
9+
}
10+
}).promise();
11+
} else {
12+
return $.Deferred().reject().promise();
13+
}
14+
};
15+
16+
var Auth = {
17+
login: function (new_token) {
18+
var deferred = $.Deferred();
19+
20+
var valid_token = verify_token(new_token);
21+
valid_token.fail(function () {
22+
if (_token === null && typeof(localStorage.token) !== 'undefined') {
23+
_token = JSON.parse(localStorage.token);
24+
}
25+
}).done(function () {
26+
debugger
27+
_token = {
28+
user_token: new_token
29+
};
30+
localStorage.token = JSON.stringify(_token);
31+
}).always(function () {
32+
if (_token && _token.user_token) {
33+
deferred.resolve(_token);
34+
} else {
35+
deferred.reject();
36+
}
37+
});
38+
39+
return deferred.promise();
40+
},
41+
logout: function () {
42+
localStorage.token = null;
43+
token = null;
44+
_is_logged_in = false;
45+
},
46+
verify_token: verify_token
47+
};
48+
49+
ADN.auth = Auth;
50+
}());
51+
52+
$(function() {
53+
// make it easy to upgrade scopes, if there's a new token just log in with that even if we're already logged in
54+
var new_token = $.url(window.location).fparam('access_token');
55+
var login = ADN.auth.login(new_token);
56+
57+
login.fail(function () {
58+
$('.authorize-prompt').removeClass('hide');
59+
}).done(function (token) {
60+
var needle = "&lt;YOUR ACCESS TOKEN&gt;";
61+
$('pre code').each(function () {
62+
var el = $(this);
63+
el.html(el.html().replace(needle, token.user_token));
64+
});
65+
});
66+
});

lib/helpers.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ def local_hostname()
7373
end
7474

7575
def login_url(text)
76-
base_domain = "https://account.app.net"
76+
base_domain = lanai_hostname()
7777
scopes = ["basic"]
78-
client_id = "gvfM7pVsPBAkVmFWeCDF22uLTyTuMzdd"
78+
# testing
79+
client_id = ENV['CLIENT_ID'] || "gvfM7pVsPBAkVmFWeCDF22uLTyTuMzdd"
7980
redirect_uri = "<script>document.write(window.location);</script>"
8081
path = "/oauth/authenticate?response_type=token"
8182

82-
url = "#{base_domain}#{path}&scope=#{scopes.join(' ')}&client_id=#{client_id}&redirect_uri="
83+
url = "https://account.#{base_domain}#{path}&scope=#{scopes.join(' ')}&client_id=#{client_id}&redirect_uri="
8384
link = "\"<a href='#{url}\" + window.location + \"'>#{text}</a>\""
8485

8586
"<script>document.write(#{link})</script>"

static/js/app.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)