Skip to content

Commit

Permalink
feat: Get JWT from store, not cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jan 5, 2024
1 parent ecb270a commit 44a0418
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions editor.planx.uk/src/api/upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { RawAxiosRequestHeaders } from "axios";
import { getCookie } from "lib/cookie";
import { useStore } from "pages/FlowEditor/lib/store";

export { uploadPrivateFile, uploadPublicFile };

Expand All @@ -9,7 +9,7 @@ async function uploadPublicFile(
file: any,
{ onProgress }: { onProgress?: (p: any) => void } = {},
) {
const token = getCookie("jwt");
const token = useStore.getState().jwt;
const authRequestHeader = { Authorization: `Bearer ${token}` };
const { data } = await handleUpload(
file,
Expand Down
5 changes: 3 additions & 2 deletions editor.planx.uk/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { getCookie } from "lib/cookie";
import { useStore } from "pages/FlowEditor/lib/store";

/**
* core doesn't expose a graphql interface like the graphql/hasura clients do
* instead, it encapsulates query and business logic to only expose declarative interfaces
Expand All @@ -10,5 +11,5 @@ export const _public = new CoreDomainClient({

export const _client = new CoreDomainClient({
targetURL: process.env.REACT_APP_HASURA_URL!,
auth: { jwt: getCookie("jwt") || "" },
auth: { jwt: useStore.getState().jwt || "" },
});
4 changes: 1 addition & 3 deletions editor.planx.uk/src/lib/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { logger } from "airbrake";
import { useStore } from "pages/FlowEditor/lib/store";
import { toast } from "react-toastify";

import { getCookie } from "./cookie";

const toastId = "error_toast";

// function used to verify response status
Expand All @@ -39,7 +37,7 @@ const authHttpLink = createHttpLink({
uri: process.env.REACT_APP_HASURA_URL,
fetch: customFetch,
headers: {
authorization: `Bearer ${getCookie("jwt")}`,
authorization: `Bearer ${useStore.getState().jwt}`,
},
});

Expand Down
11 changes: 5 additions & 6 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
update,
} from "@planx/graph";
import axios from "axios";
import { getCookie } from "lib/cookie";
import { client } from "lib/graphql";
import debounce from "lodash/debounce";
import isEmpty from "lodash/isEmpty";
Expand All @@ -21,7 +20,7 @@ import type { StateCreator } from "zustand";

import { FlowLayout } from "../../components/Flow";
import { connectToDB, getConnection } from "./../sharedb";
import type { Store } from ".";
import { type Store } from ".";
import type { SharedStore } from "./shared";
import { UserStore } from "./user";

Expand Down Expand Up @@ -141,7 +140,7 @@ export const editorStore: StateCreator<
},

copyFlow: async (flowId: string) => {
const token = getCookie("jwt");
const token = get().jwt;

// when copying a flow, we make nodeIds unique by replacing part of the original nodeId string.
// the onboarding script will often provide a meaningful string reflecting the team name (eg "LAM"),
Expand Down Expand Up @@ -237,7 +236,7 @@ export const editorStore: StateCreator<
},

validateAndDiffFlow(flowId: string) {
const token = getCookie("jwt");
const token = get().jwt;

return axios.post(
`${process.env.REACT_APP_API_URL}/flows/${flowId}/diff`,
Expand Down Expand Up @@ -340,7 +339,7 @@ export const editorStore: StateCreator<
return Promise.resolve();
}

const token = getCookie("jwt");
const token = get().jwt;

return axios
.post(
Expand Down Expand Up @@ -390,7 +389,7 @@ export const editorStore: StateCreator<
},

publishFlow(flowId: string, summary?: string) {
const token = getCookie("jwt");
const token = get().jwt;

const urlWithParams = (url: string, params: any) =>
[url, new URLSearchParams(omitBy(params, isEmpty))]
Expand Down
3 changes: 1 addition & 2 deletions editor.planx.uk/src/routes/views/authenticated.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getCookie } from "lib/cookie";
import { redirect } from "navi";
import { useStore } from "pages/FlowEditor/lib/store";
import React from "react";
Expand All @@ -11,7 +10,7 @@ import AuthenticatedLayout from "../../pages/layout/AuthenticatedLayout";
* Initialises user store
*/
export const authenticatedView = async () => {
const jwt = getCookie("jwt");
const jwt = useStore.getState().jwt;
if (!jwt) return redirect("/login");

await useStore.getState().initUserStore();
Expand Down

0 comments on commit 44a0418

Please sign in to comment.