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) {
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()
}

View File

@ -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 = {

View File

@ -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: '' })
}