Fix session checking

main
Gabe Farrell 3 years ago
parent befae76138
commit ec09fb8bf1

@ -1,13 +1,26 @@
export function checkLogin() {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.indexOf('session=') === 0) {
if (getSessionKey() !== null) {
// The "session" cookie exists
return true;
}
}
// The "session" cookie doesn't exist
fetch('https://api.bb.gabefarrell.com/userinfo', {
method: 'GET',
headers: {
'x-session-key': getSessionKey(),
},
})
.then(data => data.json())
.then((r) => {
if (r.status !== 200) {
// key is invalid
handleLogout()
return false
} else {
// key is OK
return true
}
})
}
// The "session" cookie doesn't exist or is invalid
return false;
}

Loading…
Cancel
Save