diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-11 09:22:10 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-11 09:22:10 +0100 |
| commit | 75227818ae83dded3152f3c8667db6e87f94fde7 (patch) | |
| tree | edecac4a26f1f66923083cf671498594c25054b5 /public/assets/js/lib | |
| parent | 8340d74a0b953b12134302eca14aaec0a0a67fba (diff) | |
log in and out
Diffstat (limited to 'public/assets/js/lib')
| -rw-r--r-- | public/assets/js/lib/router.js | 1 | ||||
| -rw-r--r-- | public/assets/js/lib/sdk/_sdk.js | 34 | ||||
| -rw-r--r-- | public/assets/js/lib/sdk/auth.js | 75 | ||||
| -rw-r--r-- | public/assets/js/lib/views/details/comments.js | 1 | ||||
| -rw-r--r-- | public/assets/js/lib/views/login/login.js | 18 |
5 files changed, 123 insertions, 6 deletions
diff --git a/public/assets/js/lib/router.js b/public/assets/js/lib/router.js index 8146906..6802a86 100644 --- a/public/assets/js/lib/router.js +++ b/public/assets/js/lib/router.js @@ -18,7 +18,6 @@ var SiteRouter = Router.extend({ }, initialize: function(){ - this.route() }, index: function(keyword){ diff --git a/public/assets/js/lib/sdk/_sdk.js b/public/assets/js/lib/sdk/_sdk.js new file mode 100644 index 0000000..320113e --- /dev/null +++ b/public/assets/js/lib/sdk/_sdk.js @@ -0,0 +1,34 @@ +var sdk = (function(){ + var sdk = {} + + sdk.env = "development" + + var endpoint = window.location.origin + + sdk.init = function(opt){ + switch (sdk.env = opt.env || "development") { + case 'test': + break + default: + case 'development': + break + case 'production': + break + } + } + + sdk.path = function(api){ + return endpoint + api + } + + sdk.image = function(file, size){ + return "https://i.asdf.us/bucky/data/" + file.thread + "/" + file.id + } + +// $.ajaxSetup({ +// // possibly: application/json; charset=utf-8" +// contentType: "application/json", +// }) + + return sdk +})()
\ No newline at end of file diff --git a/public/assets/js/lib/sdk/auth.js b/public/assets/js/lib/sdk/auth.js new file mode 100644 index 0000000..a5a735a --- /dev/null +++ b/public/assets/js/lib/sdk/auth.js @@ -0,0 +1,75 @@ +var auth = (function(){ + var auth = {} + + auth.appname = is_iphone ? "bucky-web/3.0.0" : "bucky-web/3.0.0" + auth.apikey = "influssiblefraubuzzardsunsetideogrammatton" + auth.device = "browser" + + auth.user = {id:-1} + + auth.next_view = null + + auth.init = function(fn){ + console.log("auth init") + // if we're on an authentication page, ignore the current user + if (window.location.pathname !== '/login' || window.location.pathname !== '/signup') { + fn && fn( false ) + return + } + auth.load_user(function(user){ + var logged_in = auth.logged_in() + if (logged_in) { + fn && fn() + return + } + auth.checkin({ + success: function(user){ + if (user) { + auth.set_user(user, fn) + } + }, + error: function(){ + window.location.href = "/login" + } + }) + }) + } + auth.get_user = function(cb){ + cb && cb(auth.user) + } + auth.set_user = function(user, cb){ + auth.user = user + localStorage.setItem("bucky.user", JSON.stringify(user)) + cb && cb() + } + auth.load_user = function(cb){ + var user + var user_str = localStorage.getItem("bucky.user") + if (user_str && user_str.length) { + try { + user = JSON.parse(user_str) + if (! user.id) user = {id:-1} + } catch(e) { + user = {id:-1} + } + } + auth.user = user + cb && cb(user) + } + auth.clear_user = function(cb){ + auth.user = {id:-1} + localStorage.removeItem("bucky.user") + cb && cb() + } + auth.log_out = function(){ + auth.clear_user() + } + auth.logged_in = function(){ + return (auth.user.id && auth.user.id !== -1 && auth.user.id !== "undefined") + } + auth.checkin = function(){ + + } + + return auth +})()
\ No newline at end of file diff --git a/public/assets/js/lib/views/details/comments.js b/public/assets/js/lib/views/details/comments.js index 65473c6..5d99138 100644 --- a/public/assets/js/lib/views/details/comments.js +++ b/public/assets/js/lib/views/details/comments.js @@ -26,7 +26,6 @@ var CommentsView = FormView.extend({ .replace(/{{comment}}/g, tidy_urls(comment.comment)) .replace(/{{date}}/g, datetime[0]) .replace(/{{time}}/g, datetime[1]) - console.log(t) var $t = $(t) return $t }, diff --git a/public/assets/js/lib/views/login/login.js b/public/assets/js/lib/views/login/login.js index 90c1b67..48676e9 100644 --- a/public/assets/js/lib/views/login/login.js +++ b/public/assets/js/lib/views/login/login.js @@ -2,20 +2,30 @@ var LoginView = FormView.extend({ el: "#login", action: "/api/login", - method: "POST", + method: "put", initialize: function(opt){ this.__super__.initialize.call(this) + $("body").removeClass("loading") this.$("[name=username]").focus() }, - showErrors: function(errors){ - console.log(errors) + showErrors: function(err){ + $(".errors").show().css({ opacity: 1 }).html("Bad username/password combo") }, success: function(data){ + console.log("LOGGED IN?", data) + if (data.user) { + auth.set_user(data.user) + } + else { + this.showErrors() + return + } if (data.returnTo) { - window.location.href = "/index" + console.log(data.returnTo) + window.location.href = data.returnTo } else { window.location.href = "/index" |
