refactor auth to run it per request rather than carry it around the app.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -11,6 +11,8 @@ import type Stat from "$lib/models/stat";
|
||||
import type { StatInput } from "$lib/models/stat";
|
||||
import type User from "$lib/models/user";
|
||||
import type { AuthResult } from "$lib/models/user";
|
||||
import { Auth } from "aws-amplify";
|
||||
import { getContext, setContext } from "svelte";
|
||||
|
||||
type FetchImpl = (info: RequestInfo, init?: RequestInit) => Promise<Response>;
|
||||
|
||||
@@ -158,9 +160,12 @@ export default class SL3APIClient {
|
||||
bodyData = JSON.stringify(body);
|
||||
}
|
||||
|
||||
if (this.tokenPromise != null) {
|
||||
headers["Authorization"] = `Bearer ${await this.tokenPromise}`;
|
||||
}
|
||||
try {
|
||||
const sess = await Auth.currentSession();
|
||||
const idToken = sess?.getIdToken()?.getJwtToken();
|
||||
|
||||
headers["Authorization"] = `Bearer ${idToken}`;
|
||||
} catch(_) {}
|
||||
|
||||
const res = await this.fetcher(`${this.root}/api/v1/${path}`, {
|
||||
method, headers,
|
||||
@@ -180,6 +185,6 @@ export default class SL3APIClient {
|
||||
}
|
||||
}
|
||||
|
||||
export function sl3(fetcher: FetchImpl, idToken?: string) {
|
||||
return new SL3APIClient(fetcher, idToken ? Promise.resolve(idToken) : null);
|
||||
export function sl3(fetcher: FetchImpl) {
|
||||
return new SL3APIClient(fetcher);
|
||||
}
|
||||
@@ -51,7 +51,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await sl3(fetch, $page.stuff.idToken).listItems($scope.id, filter, true, {});
|
||||
const res = await sl3(fetch).listItems($scope.id, filter, true, {});
|
||||
itemsWritable.set(res.items);
|
||||
groupsWritable.set(res.groups);
|
||||
} catch(_) {}
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
loading = {...loading, [key]: true};
|
||||
|
||||
try {
|
||||
const items = await sl3(fetch, $page.stuff.idToken).listItems(global ? "ALL" : $scope.id, filter)
|
||||
const items = await sl3(fetch).listItems(global ? "ALL" : $scope.id, filter)
|
||||
listsWritable.update(l => ({...l, [key.replace("Filter", "Items")]: items}));
|
||||
} catch(_err) {
|
||||
// TODO: Use err
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const newProject = await sl3(fetch, $page.stuff.idToken).findProject($scope.id, project.id)
|
||||
const newProject = await sl3(fetch).findProject($scope.id, project.id)
|
||||
projectWritable.set(newProject);
|
||||
} catch(_) {}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const newProjects = await sl3(fetch, $page.stuff.idToken).listProjects($scope.id)
|
||||
const newProjects = await sl3(fetch).listProjects($scope.id)
|
||||
projectWritable.set(newProjects);
|
||||
} catch(_) {}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const newScopes = await sl3(fetch, $page.stuff.idToken).listScopes()
|
||||
const newScopes = await sl3(fetch).listScopes()
|
||||
scopesWritable.set(newScopes);
|
||||
} catch(_) {}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const newSprints = await sl3(fetch, $page.stuff.idToken).listSprints(global ? "ALL" : $scope.id, parseInterval(intervalString, $now));
|
||||
const newSprints = await sl3(fetch).listSprints(global ? "ALL" : $scope.id, parseInterval(intervalString, $now));
|
||||
sprintsWritable.set(newSprints);
|
||||
} catch(_) {}
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ import { getStores } from "$app/stores";
|
||||
loading = true;
|
||||
|
||||
try {
|
||||
const res = await sl3(fetch, $page.stuff.idToken).fetch("DELETE", endpoint);
|
||||
const res = await sl3(fetch).fetch("DELETE", endpoint);
|
||||
|
||||
if (navigate) {
|
||||
if (noun === "Project") {
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
loading = true;
|
||||
|
||||
try {
|
||||
await sl3(fetch, $page.stuff.idToken).updateItem(scopeId, itemId, {
|
||||
await sl3(fetch).updateItem(scopeId, itemId, {
|
||||
...item,
|
||||
acquiredTime: new Date(item.acquiredTime).toISOString(),
|
||||
});
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
await sl3(fetch, $page.stuff.idToken).createItem(scopeId, submission);
|
||||
await sl3(fetch).createItem(scopeId, submission);
|
||||
break;
|
||||
case "Edit":
|
||||
if (!submission.acquiredTime) {
|
||||
@@ -148,7 +148,7 @@
|
||||
}
|
||||
|
||||
submission.stats = statDiff(currentStats, submission.stats)
|
||||
await sl3(fetch, $page.stuff.idToken).updateItem(scopeId, itemId, submission);
|
||||
await sl3(fetch).updateItem(scopeId, itemId, submission);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -76,11 +76,11 @@
|
||||
try {
|
||||
switch (op) {
|
||||
case "Create":
|
||||
const newProject = await sl3(fetch, $page.stuff.idToken).createProject($scope.id, project);
|
||||
const newProject = await sl3(fetch).createProject($scope.id, project);
|
||||
projectId = newProject.id
|
||||
break;
|
||||
case "Edit":
|
||||
await sl3(fetch, $page.stuff.idToken).updateProject($scope.id, projectId, project);
|
||||
await sl3(fetch).updateProject($scope.id, projectId, project);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@
|
||||
try {
|
||||
switch (op) {
|
||||
case "Create":
|
||||
await sl3(fetch, $page.stuff.idToken).createRequirement($scope.id, projectId, requirement);
|
||||
await sl3(fetch).createRequirement($scope.id, projectId, requirement);
|
||||
break;
|
||||
case "Edit":
|
||||
const submission: Partial<RequirementInput> = {
|
||||
@@ -98,7 +98,7 @@
|
||||
stats: statDiff(oldStats, requirement.stats, -1)
|
||||
};
|
||||
|
||||
await sl3(fetch, $page.stuff.idToken).updateRequirement($scope.id, projectId, requirementId, submission);
|
||||
await sl3(fetch).updateRequirement($scope.id, projectId, requirementId, submission);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,11 +69,11 @@
|
||||
try {
|
||||
switch (op) {
|
||||
case "Create":
|
||||
await sl3(fetch, $page.stuff.idToken).createScope(scope);
|
||||
await sl3(fetch).createScope(scope);
|
||||
await reloadScopeList();
|
||||
break;
|
||||
case "Edit":
|
||||
const res = await sl3(fetch, $page.stuff.idToken).updateScope(scopeId, scope);
|
||||
const res = await sl3(fetch).updateScope(scopeId, scope);
|
||||
updateScope(res);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -112,16 +112,16 @@
|
||||
try {
|
||||
switch (op) {
|
||||
case "Create":
|
||||
await sl3(fetch, $page.stuff.idToken).createSprint(scopeId, submission);
|
||||
await sl3(fetch).createSprint(scopeId, submission);
|
||||
break;
|
||||
case "Edit":
|
||||
await sl3(fetch, $page.stuff.idToken).updateSprint(scopeId, sprintId, submission);
|
||||
await sl3(fetch).updateSprint(scopeId, sprintId, submission);
|
||||
const {added, removed} = partsDiff(oldParts, submission.parts)
|
||||
for (const part of added) {
|
||||
await sl3(fetch, $page.stuff.idToken).upsertSprintPart(scopeId, sprintId, part).catch(() => {});
|
||||
await sl3(fetch).upsertSprintPart(scopeId, sprintId, part).catch(() => {});
|
||||
}
|
||||
for (const part of removed) {
|
||||
await sl3(fetch, $page.stuff.idToken).deleteSprintPart(scopeId, sprintId, part).catch(() => {});
|
||||
await sl3(fetch).deleteSprintPart(scopeId, sprintId, part).catch(() => {});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
@@ -77,11 +77,11 @@
|
||||
try {
|
||||
switch (op) {
|
||||
case "Create":
|
||||
const createdStat = await sl3(fetch, $page.stuff.idToken).createStat($scope.id, stat);
|
||||
const createdStat = await sl3(fetch).createStat($scope.id, stat);
|
||||
upsertStat(createdStat);
|
||||
break;
|
||||
case "Edit":
|
||||
const editedStat = await sl3(fetch, $page.stuff.idToken).updateStat($scope.id, statId, {
|
||||
const editedStat = await sl3(fetch).updateStat($scope.id, statId, {
|
||||
...stat,
|
||||
allowedAmounts: void(0),
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
export const load: Load = async({ fetch, params, stuff }) => {
|
||||
const scopeId = parseInt(params.scope.split("-")[0]);
|
||||
const client = sl3(fetch, stuff.idToken);
|
||||
const client = sl3(fetch);
|
||||
|
||||
const [scope, projects] = await Promise.all([
|
||||
client.findScope(scopeId),
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
acquiredTime: interval,
|
||||
scheduledDate: datesOf(interval),
|
||||
};
|
||||
const {items, groups} = await sl3(fetch, stuff.idToken).listItems(scopeId, filter, true, session.groupOptions);
|
||||
const {items, groups} = await sl3(fetch).listItems(scopeId, filter, true, session.groupOptions);
|
||||
|
||||
return {
|
||||
stuff: { title: "History" },
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
export const load: Load = async({params, stuff, fetch}) => {
|
||||
const scopeId = parseInt(params.scope.split("-")[0]);
|
||||
|
||||
const client = sl3(fetch, stuff.idToken);
|
||||
const client = sl3(fetch);
|
||||
|
||||
const {acquiredFilter, looseFilter, scheduledFilter} = generateItemFilters(new Date());
|
||||
const [scheduledItems, acquiredItems, looseItems, sprints] = await Promise.all([
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
import type Project from "$lib/models/project";
|
||||
import type { Load } from "@sveltejs/kit/types/internal";
|
||||
|
||||
export const load: Load = async({params, fetch, stuff}) => {
|
||||
export const load: Load = async({params, fetch}) => {
|
||||
const scopeId = parseInt(params.scope.split("-")[0]);
|
||||
const projectId = parseInt(params.project.split("-")[0]);
|
||||
|
||||
const project = await sl3(fetch, stuff.idToken).findProject(scopeId, projectId);
|
||||
const project = await sl3(fetch).findProject(scopeId, projectId);
|
||||
|
||||
return {
|
||||
stuff: { title: project.name },
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
interval = {min: new Date("2000-01-01T00:00:00Z"), max: new Date("2187-01-01T00:00:00Z")}
|
||||
}
|
||||
|
||||
const sprints = await sl3(fetch, stuff.idToken).listSprints(scopeId, interval);
|
||||
const sprints = await sl3(fetch).listSprints(scopeId, interval);
|
||||
|
||||
return {
|
||||
stuff: {
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
export const load: Load = async({ fetch, stuff }) => {
|
||||
const client = sl3(fetch, stuff.idToken);
|
||||
export const load: Load = async({ fetch }) => {
|
||||
const client = sl3(fetch);
|
||||
|
||||
const scopes = await client.listScopes();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user