Mobile Formatting for Guides and Account Pages
This commit is contained in:
parent
13305e0f96
commit
1aa75f4be2
4 changed files with 86 additions and 43 deletions
|
|
@ -24,12 +24,19 @@ function loginError(message, code = null) {
|
|||
async function generateToken(username, password) {
|
||||
const credentials = btoa(`${username} ${password}`);
|
||||
|
||||
const response = await fetch("https://account.spfn.net/api/v2/oauth2/generate_token", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": `Basic ${credentials}`,
|
||||
}
|
||||
})
|
||||
let response;
|
||||
try {
|
||||
response = await fetch("https://account.spfn.net/api/v2/oauth2/generate_token", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": `Basic ${credentials}`,
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
loginError(`Internal Server Error: ${err.message}`)
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status == 400) { // Invalid Login
|
||||
loginError("Invalid SFID or Password");
|
||||
|
|
@ -55,26 +62,19 @@ async function getToken(username, password) {
|
|||
let expiryStr = sessionStorage.getItem("authExpires");
|
||||
if (expiryStr) { // Expiry exists so token should exist
|
||||
let expiry = new Date(expiryStr);
|
||||
if (expiry < new Date()) {
|
||||
console.log("Token Expired - Generating new token")
|
||||
token = await generateToken(username, password); // Generate Token and Save to Session Storage
|
||||
} else if (!token) {
|
||||
console.log("Expiry found but no token wtf? - Generating new token")
|
||||
token = await generateToken(username, password); // Generate Token and Save to Session Storage
|
||||
} else {
|
||||
console.log("Expiry is less than the current Date() and there is a token")
|
||||
if (expiry < new Date()) { // Expired token
|
||||
token = await generateToken(username, password);
|
||||
} else if (!token) { // Expiry Saved but No Token (shouldn't be possible but it'll be caught if it happens)
|
||||
token = await generateToken(username, password);
|
||||
}
|
||||
} else {
|
||||
console.log("No expiry found - Generating new token")
|
||||
token = await generateToken(username, password); // Generate Token and Save to Session Storage
|
||||
} else { // Token Never Saved in Session
|
||||
token = await generateToken(username, password);
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
function updateUserDataDisplay(data) {
|
||||
console.log(data)
|
||||
|
||||
document.getElementById("display-name").textContent = data["mii"]["name"];
|
||||
document.getElementById("sfid").textContent = `SFID: ${data["user_id"]}`;
|
||||
|
||||
|
|
@ -115,6 +115,7 @@ document.getElementById("login").addEventListener("submit", async function(event
|
|||
const password = await document.getElementById("password").value;
|
||||
|
||||
let token = await getToken(username, password);
|
||||
if (!token) return;
|
||||
|
||||
document.getElementById("password").value = "";
|
||||
|
||||
|
|
@ -141,11 +142,9 @@ document.getElementById("login").addEventListener("submit", async function(event
|
|||
window.onload = async function() {
|
||||
let expiryStr = sessionStorage.getItem("authExpires");
|
||||
if (expiryStr) {
|
||||
console.log("Expiry found")
|
||||
let expiry = new Date(expiryStr);
|
||||
|
||||
if (expiry > new Date()) { // Hasn't expired - Get user data
|
||||
console.log("Not expired - Requesting data")
|
||||
let token = sessionStorage.getItem("authToken");
|
||||
|
||||
const response = await fetch("https://account.spfn.net/api/v2/users/@me/profile", {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue