using param based token

This commit is contained in:
Roland Osborne 2022-03-20 22:12:42 -07:00
parent f5e0ef29f8
commit 9bb2a4ac7c
3 changed files with 14 additions and 10 deletions

View File

@ -8,6 +8,10 @@ function checkResponse(response) {
} }
} }
export function getProfileImageUrl(token) {
return '/profile/image?agent=' + token
}
async function fetchWithTimeout(url, options) { async function fetchWithTimeout(url, options) {
return Promise.race([ return Promise.race([
fetch(url, options).catch(err => { throw new Error(url + ' failed'); }), 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) { export async function getProfile(token) {
let headers = new Headers() let profile = await fetchWithTimeout('/profile?agent=' + token, { method: 'GET', timeout: FETCH_TIMEOUT });
headers.append('Authorization', 'Bearer ' + token);
let profile = await fetchWithTimeout('/profile', { method: 'GET', timeout: FETCH_TIMEOUT, headers: headers });
checkResponse(profile) checkResponse(profile)
return await profile.json() return await profile.json()
} }
export async function getGroups(token, revision) { export async function getGroups(token, revision) {
let headers = new Headers() let param = "?agent=" + token
headers.append('Authorization', 'Bearer ' + token); if (revision != null) {
let param = revision == null ? '' : '?revision=' + revision param += '&revision=' + revision
let groups = await fetchWithTimeout('/alias/groups' + param, { method: 'GET', timeout: FETCH_TIMEOUT, headers: headers }); }
let groups = await fetchWithTimeout('/alias/groups' + param, { method: 'GET', timeout: FETCH_TIMEOUT });
checkResponse(groups) checkResponse(groups)
return await groups.json() return await groups.json()
} }

View File

@ -1,5 +1,5 @@
import { useEffect, useState, useRef } from 'react'; 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) { async function updateProfile(token, updateData) {
let profile = await getProfile(token); let profile = await getProfile(token);
@ -70,7 +70,8 @@ export function useAppContext() {
logout: () => { logout: () => {
appLogout(updateState, clearWebsocket); appLogout(updateState, clearWebsocket);
resetData(); resetData();
} },
profileImageUrl: () => getProfileImageUrl(state.token)
} }
const adminActions = { const adminActions = {

View File

@ -37,7 +37,7 @@ export function useIdentity() {
updateState({ name: profile.name }); updateState({ name: profile.name });
} }
if (profile.image != null) { if (profile.image != null) {
updateState({ imageUrl: 'https://' + profile.node + '/profile/image?token=' + app.state.token }) updateState({ imageUrl: app.actions.profileImageUrl() })
} else { } else {
updateState({ imageUrl: '' }) updateState({ imageUrl: '' })
} }