From 029e3ff7ebc2eba57361f6f1d5983bb7f686027c Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Mon, 9 Sep 2024 22:00:11 -0600 Subject: [PATCH] adding headers on the backend --- nextjs/next.config.mjs | 12 +++++++--- nextjs/src/services/axiosUtils.ts | 18 +++++++++++++-- .../src/services/canvas/canvasPageService.ts | 1 + nextjs/src/services/canvas/webRequestor.ts | 22 ++----------------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/nextjs/next.config.mjs b/nextjs/next.config.mjs index 63626cf..7c7e451 100644 --- a/nextjs/next.config.mjs +++ b/nextjs/next.config.mjs @@ -1,12 +1,18 @@ /** @type {import('next').NextConfig} */ + +const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN; +if (!token) { + throw new Error("CANVAS_TOKEN not in environment"); +} + const nextConfig = { async rewrites() { return [ { - source: '/api/canvas/:rest*', - destination: 'https://snow.instructure.com/api/v1/:rest*', + source: "/api/canvas/:rest*", + destination: "https://snow.instructure.com/api/v1/:rest*", }, - ] + ]; }, }; diff --git a/nextjs/src/services/axiosUtils.ts b/nextjs/src/services/axiosUtils.ts index 21a3d3a..b6abd37 100644 --- a/nextjs/src/services/axiosUtils.ts +++ b/nextjs/src/services/axiosUtils.ts @@ -1,11 +1,15 @@ import { isServer } from "@tanstack/react-query"; -import axios, { AxiosInstance, AxiosError } from "axios"; +import axios, { AxiosInstance, AxiosError, AxiosHeaders } from "axios"; import toast from "react-hot-toast"; +const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN; +if (!token) { + throw new Error("NEXT_PUBLIC_CANVAS_TOKEN not in environment"); +} + export const axiosClient: AxiosInstance = axios.create(); if (!isServer) { - console.log("not on the server, setting up interceptor"); axiosClient.interceptors.request.use((config) => { if ( config.url && @@ -21,6 +25,16 @@ if (!isServer) { }); } +axiosClient.interceptors.request.use((config) => { + if ( + config.url && + config.url.startsWith("https://snow.instructure.com/api/v1/") + ) { + config.headers.set("Authorization", `Bearer ${token}`); + } + return config; +}); + axiosClient.interceptors.response.use( (response) => response, (error: AxiosError) => { diff --git a/nextjs/src/services/canvas/canvasPageService.ts b/nextjs/src/services/canvas/canvasPageService.ts index cb38ec0..5939a46 100644 --- a/nextjs/src/services/canvas/canvasPageService.ts +++ b/nextjs/src/services/canvas/canvasPageService.ts @@ -7,6 +7,7 @@ const baseCanvasUrl = "https://snow.instructure.com/api/v1"; export const canvasPageService = { async getAll(courseId: number): Promise { + console.log("requesting pages"); const url = `${baseCanvasUrl}/courses/${courseId}/pages`; const pages = await canvasServiceUtils.paginatedRequest({ url, diff --git a/nextjs/src/services/canvas/webRequestor.ts b/nextjs/src/services/canvas/webRequestor.ts index b20559a..e6b7f82 100644 --- a/nextjs/src/services/canvas/webRequestor.ts +++ b/nextjs/src/services/canvas/webRequestor.ts @@ -2,11 +2,6 @@ import { axiosClient } from "../axiosUtils"; type FetchOptions = Omit; -const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN; -if (!token) { - throw new Error("CANVAS_TOKEN not in environment"); -} - const rateLimitRetryCount = 6; const rateLimitSleepInterval = 1000; @@ -44,7 +39,6 @@ const rateLimitAwarePost = async ( method: "POST", body: JSON.stringify(body), headers: { - Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, }); @@ -81,8 +75,6 @@ const recursiveDelete = async ( ...options, method: "DELETE", headers: { - ...options.headers, - Authorization: `Bearer ${token}`, }, }); @@ -113,20 +105,12 @@ const recursiveDelete = async ( }; export const webRequestor = { getMany: async (url: string, options: FetchOptions = {}) => { - const response = await axiosClient.get(url, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const response = await axiosClient.get(url); return { data: response.data, response }; }, get: async (url: string) => { - const response = await axiosClient.get(url, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const response = await axiosClient.get(url); return { data: response.data, response }; }, @@ -144,7 +128,6 @@ export const webRequestor = { method: "PUT", body: JSON.stringify(body), headers: { - Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, }); @@ -156,7 +139,6 @@ export const webRequestor = { body: JSON.stringify(body), method: "PUT", headers: { - Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, });