diff options
Diffstat (limited to 'public/assets/js/lib/sdk/auth.js')
| -rw-r--r-- | public/assets/js/lib/sdk/auth.js | 75 |
1 files changed, 75 insertions, 0 deletions
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 |
