lmc/logout/logout.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, oauthPost, parseParams } from "/global.js";
import { clientID } from "/clientID.js";
(function()
{
// Data recieved from API calls
var data = {};
// Callback for token revoke
var redirect = function(response)
{
localStorage.clear();
location.href = "/";
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// Revoke only if we have an unexpired token
if (localStorage.apiKey && localStorage.apiExpire) {
var now = new Date();
var expires = new Date(parseInt(localStorage.apiExpire));
if (expires > now) {
var req = new URLSearchParams();
req.set("client_id", clientID);
req.set("token", localStorage.apiKey);
oauthPost("/revoke", req, redirect);
return;
}
}
// Otherwise clear everything and redirect to login
localStorage.clear();
location.href = "/";
};
// Attach onload handler
window.addEventListener("load", setup);
})();