mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
clear session on logout
This commit is contained in:
parent
c7ebc514ea
commit
ff564c24d6
8
net/web/src/api/clearLogin.js
Normal file
8
net/web/src/api/clearLogin.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
||||||
|
|
||||||
|
export async function clearLogin(token) {
|
||||||
|
let logout = await fetchWithTimeout(`/account/apps?agent=${token}`, { method: 'DELETE' })
|
||||||
|
checkResponse(logout)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
|||||||
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
||||||
|
|
||||||
export async function setAccountAccess(token) {
|
export async function setAccountAccess(token, appName, appVersion, platform) {
|
||||||
let app = { Name: "indicom", Description: "decentralized communication" }
|
let app = { Name: "indicom", Description: "decentralized communication" }
|
||||||
let access = await fetchWithTimeout(`/account/access?token=${token}`, { method: 'PUT', body: JSON.stringify(app) })
|
let access = await fetchWithTimeout(`/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}`, { method: 'PUT', body: JSON.stringify(app) })
|
||||||
checkResponse(access)
|
checkResponse(access)
|
||||||
return await access.json()
|
return await access.json()
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
||||||
var base64 = require('base-64');
|
var base64 = require('base-64');
|
||||||
|
|
||||||
export async function setLogin(username, password) {
|
export async function setLogin(username, password, appName, appVersion, userAgent) {
|
||||||
|
const platform = encodeURIComponent(userAgent);
|
||||||
let headers = new Headers()
|
let headers = new Headers()
|
||||||
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
|
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
|
||||||
let app = { Name: "indicom", Description: "decentralized communication" }
|
let app = { Name: "indicom", Description: "decentralized communication" }
|
||||||
let login = await fetchWithTimeout('/account/apps', { method: 'POST', body: JSON.stringify(app), headers: headers })
|
let login = await fetchWithTimeout(`/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}`, { method: 'POST', body: JSON.stringify(app), headers: headers })
|
||||||
checkResponse(login)
|
checkResponse(login)
|
||||||
return await login.json()
|
return await login.json()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { useEffect, useState, useRef, useContext } from 'react';
|
import { useEffect, useState, useRef, useContext } from 'react';
|
||||||
import { getAvailable } from 'api/getAvailable';
|
import { getAvailable } from 'api/getAvailable';
|
||||||
import { setLogin } from 'api/setLogin';
|
import { setLogin } from 'api/setLogin';
|
||||||
|
import { clearLogin } from 'api/clearLogin';
|
||||||
import { setAccountAccess } from 'api/setAccountAccess';
|
import { setAccountAccess } from 'api/setAccountAccess';
|
||||||
import { addAccount } from 'api/addAccount';
|
import { addAccount } from 'api/addAccount';
|
||||||
import { getUsername } from 'api/getUsername';
|
import { getUsername } from 'api/getUsername';
|
||||||
@ -14,9 +15,14 @@ import { StoreContext } from './StoreContext';
|
|||||||
import { UploadContext } from './UploadContext';
|
import { UploadContext } from './UploadContext';
|
||||||
|
|
||||||
export function useAppContext() {
|
export function useAppContext() {
|
||||||
const [state, setState] = useState({});
|
const [state, setState] = useState({
|
||||||
|
});
|
||||||
const [appRevision, setAppRevision] = useState();
|
const [appRevision, setAppRevision] = useState();
|
||||||
|
|
||||||
|
const appName = "Databag";
|
||||||
|
const appVersion = "1.0.0";
|
||||||
|
const userAgent = window.navigator.userAgent;
|
||||||
|
|
||||||
const ws = useRef(null);
|
const ws = useRef(null);
|
||||||
const revision = useRef(null);
|
const revision = useRef(null);
|
||||||
|
|
||||||
@ -66,7 +72,7 @@ export function useAppContext() {
|
|||||||
|
|
||||||
const appCreate = async (username, password, token) => {
|
const appCreate = async (username, password, token) => {
|
||||||
await addAccount(username, password, token);
|
await addAccount(username, password, token);
|
||||||
let access = await setLogin(username, password)
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
@ -78,7 +84,7 @@ export function useAppContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const appLogin = async (username, password) => {
|
const appLogin = async (username, password) => {
|
||||||
let access = await setLogin(username, password)
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
@ -90,7 +96,7 @@ export function useAppContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const appAccess = async (token) => {
|
const appAccess = async (token) => {
|
||||||
let access = await setAccountAccess(token)
|
let access = await setAccountAccess(token, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
@ -101,7 +107,13 @@ export function useAppContext() {
|
|||||||
return access.created;
|
return access.created;
|
||||||
}
|
}
|
||||||
|
|
||||||
const appLogout = () => {
|
const appLogout = async () => {
|
||||||
|
try {
|
||||||
|
await clearLogin(state.access);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
updateState({ access: null });
|
updateState({ access: null });
|
||||||
clearWebsocket()
|
clearWebsocket()
|
||||||
localStorage.removeItem("session");
|
localStorage.removeItem("session");
|
||||||
|
Loading…
Reference in New Issue
Block a user