Skip to content

Commit

Permalink
Tweak life token example js
Browse files Browse the repository at this point in the history
  • Loading branch information
mthurman committed Mar 25, 2014
1 parent c15530e commit e4f3658
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -15,4 +15,4 @@ gem 'yajl-ruby', '1.1.0'
gem 'listen', '1.2.2'
gem 'guard-nanoc', '1.0.1'
gem 'rb-readline'
gem 'w3c_validators'
gem 'w3c_validators'
8 changes: 8 additions & 0 deletions Rules
Expand Up @@ -13,6 +13,10 @@
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.

compile '/assets/js/*' do
filter :erb
end

compile '/static/*' do
end

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

route '/assets/js/*' do
item.identifier.chop + '.' + item[:extension]
end

route '*' do
if item.binary?
# Write item with identifier /foo/ to /foo.ext
Expand Down
66 changes: 66 additions & 0 deletions content/assets/js/app.js
@@ -0,0 +1,66 @@
(function () {
var _token = null;
var verify_token = function (token) {
if (token && token.match(/^[\-_a-zA-Z0-9]{98}$/)) {
return $.ajax({
url: "https://alpha-api.<%= lanai_hostname %>/stream/0/token",
headers: {
"Authorization": "BEARER " + token
}
}).promise();
} else {
return $.Deferred().reject().promise();
}
};

var Auth = {
login: function (new_token) {
var deferred = $.Deferred();

var valid_token = verify_token(new_token);
valid_token.fail(function () {
if (_token === null && typeof(localStorage.token) !== 'undefined') {
_token = JSON.parse(localStorage.token);
}
}).done(function () {
debugger
_token = {
user_token: new_token
};
localStorage.token = JSON.stringify(_token);
}).always(function () {
if (_token && _token.user_token) {
deferred.resolve(_token);
} else {
deferred.reject();
}
});

return deferred.promise();
},
logout: function () {
localStorage.token = null;
token = null;
_is_logged_in = false;
},
verify_token: verify_token
};

ADN.auth = Auth;
}());

$(function() {
// make it easy to upgrade scopes, if there's a new token just log in with that even if we're already logged in
var new_token = $.url(window.location).fparam('access_token');
var login = ADN.auth.login(new_token);

login.fail(function () {
$('.authorize-prompt').removeClass('hide');
}).done(function (token) {
var needle = "&lt;YOUR ACCESS TOKEN&gt;";
$('pre code').each(function () {
var el = $(this);
el.html(el.html().replace(needle, token.user_token));
});
});
});
7 changes: 4 additions & 3 deletions lib/helpers.rb
Expand Up @@ -73,13 +73,14 @@ def local_hostname()
end

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

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

"<script>document.write(#{link})</script>"
Expand Down
23 changes: 0 additions & 23 deletions static/js/app.js

This file was deleted.

0 comments on commit e4f3658

Please sign in to comment.