blob: 0b494496a62b9ae6a3bdb7b5777381ecc0da98b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
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){
// 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(opt){
$.ajax({
method: 'put',
url: '/api/checkin',
headers: { "csrf-token": $("[name=_csrf]").attr("value") },
success: function(data){
if (data && data.user && data.user.id !== -1) {
auth.set_user(data.user)
opt.success(data.user)
return
}
opt.error()
},
error: function(){
window.location.href = '/login'
opt.error()
},
})
}
return auth
})()
|