Permalink
Browse files

Tweak life token example js

  • Loading branch information...
1 parent c15530e commit e4f3658887d1d2c3ac8ddb7dabdd4f6ec4b5d02f @mthurman mthurman committed Mar 25, 2014
Showing with 79 additions and 27 deletions.
  1. +1 −1 Gemfile
  2. +8 −0 Rules
  3. +66 −0 content/assets/js/app.js
  4. +4 −3 lib/helpers.rb
  5. +0 −23 static/js/app.js
View
@@ -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'
View
8 Rules
@@ -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
@@ -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
View
@@ -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));
+ });
+ });
+});
View
@@ -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>"
View
@@ -1,23 +0,0 @@
-$(function() {
-
- var token = null;
- var new_token = $.url(window.location).fparam('access_token');
- if (new_token) {
- token = {
- user_token: new_token
- };
- localStorage.token = JSON.stringify(token);
- } else if (typeof(localStorage.token) !== 'undefined') {
- token = JSON.parse(localStorage.token);
- }
-
- if (token && token.user_token) {
- var needle = "&lt;YOUR ACCESS TOKEN&gt;";
- $('pre code').each(function () {
- var el = $(this);
- el.html(el.html().replace(needle, token.user_token));
- });
- } else {
- $('.authorize-prompt').removeClass('hide');
- }
-});

0 comments on commit e4f3658

Please sign in to comment.