mirror of
https://github.com/Steffo99/bluelib.git
synced 2024-12-22 19:44:21 +00:00
0.12.0
This commit is contained in:
parent
4487925168
commit
9d7cbb606f
5 changed files with 74 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"private": false,
|
"private": false,
|
||||||
"name": "bluelib",
|
"name": "bluelib",
|
||||||
"version": "0.11.6",
|
"version": "0.12.0",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"source": "src/index.js",
|
"source": "src/index.js",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|
3
src/contexts/RoyalnetLoginStatus.js
Normal file
3
src/contexts/RoyalnetLoginStatus.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { createContext } from 'preact';
|
||||||
|
|
||||||
|
export default createContext(null);
|
55
src/hooks/useLoginDataStorage.js
Normal file
55
src/hooks/useLoginDataStorage.js
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
|
import { royalnetApiRequest } from '../utils/royalnetApiRequest';
|
||||||
|
import { route } from 'preact-router';
|
||||||
|
|
||||||
|
export default function(defaultInstanceUrl) {
|
||||||
|
const [instanceUrl, setInstanceUrl] = useState(defaultInstanceUrl);
|
||||||
|
const [loginStatus, setLoginStatus] = useState(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.debug("Checking if an instanceUrl is stored in the localStorage...");
|
||||||
|
let instanceUrlStore = window.localStorage.getItem("instanceUrl");
|
||||||
|
if(instanceUrlStore === null) return;
|
||||||
|
console.debug(`Found instanceUrl ${instanceUrlStore}, setting it...`);
|
||||||
|
setInstanceUrl(instanceUrlStore);
|
||||||
|
console.debug("Checking if a valid login token is stored in the localStorage...");
|
||||||
|
let loginStatusStore = JSON.parse(window.localStorage.getItem("loginStatus"));
|
||||||
|
if(loginStatusStore === null) return;
|
||||||
|
console.debug("Found a login token; checking its validity...");
|
||||||
|
royalnetApiRequest(instanceUrlStore, "GET", "/api/auth/token/v1", {token: loginStatusStore.token}).then((data => {
|
||||||
|
let expiration = new Date(data.expiration);
|
||||||
|
console.debug(`Login token expires: ${expiration}`);
|
||||||
|
let now = new Date();
|
||||||
|
if(expiration >= now ) {
|
||||||
|
console.debug(`Login token is valid, logging in...`);
|
||||||
|
setLoginStatus(data);
|
||||||
|
console.debug(`Successfully logged in as ${data.user.username} @ ${instanceUrlStore} !`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.debug(`Login token has expired, clearing...`);
|
||||||
|
window.localStorage.setItem("loginStatus", null);
|
||||||
|
}
|
||||||
|
})).catch((err) => {
|
||||||
|
console.error(`Could not check validity of the login token: ${err}`)
|
||||||
|
})
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function onSuccessfulLogin(newInstanceUrl, newLoginStatus) {
|
||||||
|
console.debug(`Successfully logged in as ${newLoginStatus.user.username} @ ${newInstanceUrl} !`);
|
||||||
|
setInstanceUrl(newInstanceUrl);
|
||||||
|
setLoginStatus(newLoginStatus);
|
||||||
|
console.debug("Saving login data in the localStorage...");
|
||||||
|
window.localStorage.setItem("instanceUrl", newInstanceUrl);
|
||||||
|
window.localStorage.setItem("loginStatus", JSON.stringify(newLoginStatus));
|
||||||
|
route("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function requestLogout() {
|
||||||
|
console.debug("User requested logout, clearing loginStatus and localStorage...");
|
||||||
|
setLoginStatus(null);
|
||||||
|
window.localStorage.setItem("loginStatus", null);
|
||||||
|
route("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
return [instanceUrl, loginStatus, onSuccessfulLogin, requestLogout]
|
||||||
|
}
|
|
@ -1,14 +1,24 @@
|
||||||
import RoyalnetInstanceUrl from '../contexts/RoyalnetInstanceUrl';
|
import RoyalnetInstanceUrl from '../contexts/RoyalnetInstanceUrl';
|
||||||
|
import LoginStatus from "../contexts/RoyalnetLoginStatus";
|
||||||
import {useContext, useState} from 'preact/hooks';
|
import {useContext, useState} from 'preact/hooks';
|
||||||
import {royalnetApiRequest} from '../utils/royalnetApiRequest';
|
|
||||||
import useDeepCompareEffect from "use-deep-compare-effect";
|
import useDeepCompareEffect from "use-deep-compare-effect";
|
||||||
|
import {royalnetApiRequest} from '../utils/royalnetApiRequest';
|
||||||
|
|
||||||
|
|
||||||
export default function(method, path, body) {
|
export default function(method, path, body) {
|
||||||
const instanceUrl = useContext(RoyalnetInstanceUrl);
|
const instanceUrl = useContext(RoyalnetInstanceUrl);
|
||||||
|
const loginStatus = useContext(LoginStatus);
|
||||||
const [data, setData] = useState(undefined);
|
const [data, setData] = useState(undefined);
|
||||||
const [error, setError] = useState(undefined);
|
const [error, setError] = useState(undefined);
|
||||||
|
|
||||||
|
if(body === undefined) {
|
||||||
|
body = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(loginStatus !== null) {
|
||||||
|
body["token"] = loginStatus["token"]
|
||||||
|
}
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
setData(undefined);
|
setData(undefined);
|
||||||
setError(undefined);
|
setError(undefined);
|
||||||
|
|
|
@ -22,8 +22,10 @@ import LatexDefaultDisplay from "./contexts/LatexDefaultDisplay";
|
||||||
import LatexDefaultInline from "./contexts/LatexDefaultInline";
|
import LatexDefaultInline from "./contexts/LatexDefaultInline";
|
||||||
import LatexRenderColor from "./contexts/LatexRenderColor";
|
import LatexRenderColor from "./contexts/LatexRenderColor";
|
||||||
import RoyalnetInstanceUrl from "./contexts/RoyalnetInstanceUrl";
|
import RoyalnetInstanceUrl from "./contexts/RoyalnetInstanceUrl";
|
||||||
|
import RoyalnetLoginStatus from "./contexts/RoyalnetLoginStatus";
|
||||||
|
|
||||||
import useFormValidator from "./hooks/useFormValidator";
|
import useFormValidator from "./hooks/useFormValidator";
|
||||||
|
import useLoginDataStorage from "./hooks/useLoginDataStorage";
|
||||||
import useRoyalnetData from "./hooks/useRoyalnetData";
|
import useRoyalnetData from "./hooks/useRoyalnetData";
|
||||||
import useRoyalnetInstanceValidator from "./hooks/useRoyalnetInstanceValidator";
|
import useRoyalnetInstanceValidator from "./hooks/useRoyalnetInstanceValidator";
|
||||||
|
|
||||||
|
@ -62,8 +64,10 @@ export {
|
||||||
LatexDefaultInline,
|
LatexDefaultInline,
|
||||||
LatexRenderColor,
|
LatexRenderColor,
|
||||||
RoyalnetInstanceUrl,
|
RoyalnetInstanceUrl,
|
||||||
|
RoyalnetLoginStatus,
|
||||||
|
|
||||||
useFormValidator,
|
useFormValidator,
|
||||||
|
useLoginDataStorage,
|
||||||
useRoyalnetData,
|
useRoyalnetData,
|
||||||
useRoyalnetInstanceValidator,
|
useRoyalnetInstanceValidator,
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue