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 | |
| parent | 8340d74a0b953b12134302eca14aaec0a0a67fba (diff) | |
log in and out
Diffstat (limited to 'public/assets/js')
| -rw-r--r-- | public/assets/js/index.js | 6 | ||||
| -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 | ||||
| -rw-r--r-- | public/assets/js/vendor/view/formview.js | 33 |
7 files changed, 151 insertions, 17 deletions
diff --git a/public/assets/js/index.js b/public/assets/js/index.js index 8f1a059..4576fd4 100644 --- a/public/assets/js/index.js +++ b/public/assets/js/index.js @@ -7,6 +7,12 @@ var app = (function(){ $(window).on("focus", app.focus) $(window).on("blur", app.blur) + + auth.init(app.ready) + } + + app.ready = function(){ + app.router.route() } app.focused = true 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" diff --git a/public/assets/js/vendor/view/formview.js b/public/assets/js/vendor/view/formview.js index f71c550..b2825a2 100644 --- a/public/assets/js/vendor/view/formview.js +++ b/public/assets/js/vendor/view/formview.js @@ -33,30 +33,40 @@ var FormView = View.extend({ }, serialize: function(){ - var fd = new FormData(), hasCSRF = false + var fd, fh, hasCSRF = false + var file_els = this.$("input[name][type='file']") + if (file_els.length) { + fd = new FormData() + } + else { + fh = {} + } this.$("input[name], select[name], textarea[name]").each( function(){ if (this.type == "file") { for (var i = 0; i < this.files.length; i++) { fd.append(this.name, this.files[i]); } } - else if (this.type == "password") { - if (this.value.length > 0) { - fd.append(this.name, SHA1.hex('bucky$' + this.value + '$bucky')) - } - } else { - fd.append(this.name, this.value); + if (fd) { + fd.append(this.name, this.value) + } else { + fh[this.name] = this.value + } hasCSRF = hasCSRF || this.name == "_csrf" } }); if (! hasCSRF) { - fd.append("_csrf", $("[name=_csrf]").attr("value")) + if (fd) { + fd.append("_csrf", $("[name=_csrf]").attr("value")) + } else { + fh["_csrf"] = $("[name=_csrf]").attr("value") + } } - return fd + return fd || JSON.stringify(fh) }, save: function(e, successCallback, errorCallback){ @@ -80,14 +90,15 @@ var FormView = View.extend({ var action = typeof this.action == "function" ? this.action() : this.action if (! action) return + var data = this.serialize() var request = $.ajax({ url: action, type: this.method, - data: this.serialize(), + data: data, headers: { "csrf-token": $("[name=_csrf]").attr("value") }, + contentType: data instanceof FormData ? false : "application/json", dataType: "json", processData: false, - contentType: false, success: function(response){ console.log(response) if (response.error) { |
