Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
74 lines (69 loc) · 2.75 KB
/
Copy pathauth.js
File metadata and controls
74 lines (69 loc) · 2.75 KB
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
/**
* Eraya - Auth helper: current user, login redirect, logout.
* Include on pages that need login state or API calls with credentials.
*/
(function() {
'use strict';
var currentUser = null;
function fetchOptions(method, body) {
var opts = { method: method || 'GET', credentials: 'include' };
if (body) {
opts.headers = { 'Content-Type': 'application/json' };
opts.body = JSON.stringify(body);
}
return opts;
}
function getCurrentUser() {
return fetch('/api/me', fetchOptions()).then(function(r) { return r.json(); }).then(function(data) {
currentUser = data.user || null;
return currentUser;
});
}
function requireLogin(onLoggedIn) {
getCurrentUser().then(function(user) {
if (user) {
if (onLoggedIn) onLoggedIn(user);
} else {
if (confirm('You need to sign in to use this feature. Go to login page?')) {
window.location.href = 'login.html?next=' + encodeURIComponent(window.location.href);
}
}
});
}
function renderAuthStatus(containerId) {
var container = document.getElementById(containerId || 'auth-status');
if (!container) return;
getCurrentUser().then(function(user) {
if (user) {
container.innerHTML = '<span style="color:var(--white);">' + (user.name || user.email) + '</span> ' +
'<a href="#" id="auth-logout" style="color:var(--white); margin-left:8px;">Logout</a>';
var logout = document.getElementById('auth-logout');
if (logout) {
logout.addEventListener('click', function(e) {
e.preventDefault();
fetch('/api/logout', { method: 'POST', credentials: 'include' }).then(function() {
window.location.reload();
});
});
}
} else {
container.innerHTML = '<a href="login.html" style="color:var(--white);">Login</a>';
}
});
}
window.ErayaAuth = {
getCurrentUser: getCurrentUser,
requireLogin: requireLogin,
renderAuthStatus: renderAuthStatus,
fetchWithAuth: function(url, opts) {
opts = opts || {};
opts.credentials = 'include';
if (opts.body && typeof opts.body === 'object' && !(opts.body instanceof FormData)) {
opts.headers = opts.headers || {};
opts.headers['Content-Type'] = 'application/json';
opts.body = JSON.stringify(opts.body);
}
return fetch(url, opts);
}
};
})();