From 9bb2a4ac7c86ec361c7c4bfcfb4cbdb9596f0600 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sun, 20 Mar 2022 22:12:42 -0700 Subject: [PATCH] using param based token --- net/web/src/AppContext/fetchUtil.js | 17 ++++++++++------- net/web/src/AppContext/useAppContext.hook.js | 5 +++-- .../User/SideBar/Identity/useIdentity.hook.js | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/net/web/src/AppContext/fetchUtil.js b/net/web/src/AppContext/fetchUtil.js index cee95ccf..6492d27f 100644 --- a/net/web/src/AppContext/fetchUtil.js +++ b/net/web/src/AppContext/fetchUtil.js @@ -8,6 +8,10 @@ function checkResponse(response) { } } +export function getProfileImageUrl(token) { + return '/profile/image?agent=' + token +} + async function fetchWithTimeout(url, options) { return Promise.race([ fetch(url, options).catch(err => { throw new Error(url + ' failed'); }), @@ -45,18 +49,17 @@ export async function createAccount(username, password) { } export async function getProfile(token) { - let headers = new Headers() - headers.append('Authorization', 'Bearer ' + token); - let profile = await fetchWithTimeout('/profile', { method: 'GET', timeout: FETCH_TIMEOUT, headers: headers }); + let profile = await fetchWithTimeout('/profile?agent=' + token, { method: 'GET', timeout: FETCH_TIMEOUT }); checkResponse(profile) return await profile.json() } export async function getGroups(token, revision) { - let headers = new Headers() - headers.append('Authorization', 'Bearer ' + token); - let param = revision == null ? '' : '?revision=' + revision - let groups = await fetchWithTimeout('/alias/groups' + param, { method: 'GET', timeout: FETCH_TIMEOUT, headers: headers }); + let param = "?agent=" + token + if (revision != null) { + param += '&revision=' + revision + } + let groups = await fetchWithTimeout('/alias/groups' + param, { method: 'GET', timeout: FETCH_TIMEOUT }); checkResponse(groups) return await groups.json() } diff --git a/net/web/src/AppContext/useAppContext.hook.js b/net/web/src/AppContext/useAppContext.hook.js index 43ffc74f..78381fb1 100644 --- a/net/web/src/AppContext/useAppContext.hook.js +++ b/net/web/src/AppContext/useAppContext.hook.js @@ -1,5 +1,5 @@ import { useEffect, useState, useRef } from 'react'; -import { getProfile, getGroups, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; +import { getProfileImageUrl, getProfile, getGroups, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; async function updateProfile(token, updateData) { let profile = await getProfile(token); @@ -70,7 +70,8 @@ export function useAppContext() { logout: () => { appLogout(updateState, clearWebsocket); resetData(); - } + }, + profileImageUrl: () => getProfileImageUrl(state.token) } const adminActions = { diff --git a/net/web/src/User/SideBar/Identity/useIdentity.hook.js b/net/web/src/User/SideBar/Identity/useIdentity.hook.js index 137f1a4c..9bc56b9e 100644 --- a/net/web/src/User/SideBar/Identity/useIdentity.hook.js +++ b/net/web/src/User/SideBar/Identity/useIdentity.hook.js @@ -37,7 +37,7 @@ export function useIdentity() { updateState({ name: profile.name }); } if (profile.image != null) { - updateState({ imageUrl: 'https://' + profile.node + '/profile/image?token=' + app.state.token }) + updateState({ imageUrl: app.actions.profileImageUrl() }) } else { updateState({ imageUrl: '' }) }