Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update api key now uses userId #239

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions services/web-app/src/app/demo/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";
import { useSession } from "next-auth/react";
import React, { useState } from "react";
import Loading from "@/components/loading/loading";
import CodeTextArea from "@/components/textFields/codeBox";
import ReviewedCode from "@/components/textFields/reviewedCode";
import BasicButton from "@/components/buttons/basicButton";

import BasicButton from "../../components/buttons/basicButton";
import Loading from "../../components/loading/loading";
import CodeTextArea from "../../components/textFields/codeBox";
import ReviewedCode from "../../components/textFields/reviewedCode";

export default function Demo(): JSX.Element {
const { status } = useSession();
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we move this image?

File renamed without changes
6 changes: 4 additions & 2 deletions services/web-app/src/app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";
import Image from "next/image";
import { User } from "next-auth";
import { useSession } from "next-auth/react";
import { useEffect, useState } from "react";

Expand All @@ -8,7 +9,6 @@ import UpdateAPIKey from "../../components/dialog/updateApiKey";
import Loading from "../../components/loading/loading";
import { RepoTable } from "../../components/tables/repoTable";
import useAxios from "../../lib/hooks/useAxios";
import { User } from "../../lib/types";

const containsUserDataFields = (input: object): boolean =>
"email" in input &&
Expand Down Expand Up @@ -79,9 +79,11 @@ export default async function Profile(): Promise<JSX.Element> {

const handleUpdateApiKey = async (newApiKey: string) => {
try {
console.log("user -> ", user);
console.log("session -> ", session);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove pls!

const response = await axiosInstance.post(`/updateUser`, {
userID: user.userId,
apiKey: newApiKey,
userId: user.userId,
});
console.log("API key updated successfully:", response.data);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

interface ReviewedCodeProps {
type ReviewedCodeProps = {
text: string;
isHidden: boolean;
}
Expand Down
15 changes: 8 additions & 7 deletions services/web-app/src/lib/hooks/useAxios.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { type AxiosInstance } from "axios";
import { Session } from "next-auth";
import { getSession } from "next-auth/react";

import { BASE_URL } from "../constants";
Expand All @@ -17,13 +18,13 @@ const useAxios = async (): Promise<{ axiosInstance: AxiosInstance }> => {
}

axiosInstance.interceptors.request.clear();
axiosInstance.interceptors.request.use((config) => {
//TODO: investigate this as the Session type returned from does not seem to contain 'token'
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
config.headers.Authorization = session.token;

return config;
});
axiosInstance.interceptors.request.use(
(config) => {
config.headers["Authorization"] = `Bearer ${(session as Session)?.token ?? ""}`;
return config;
}
);

return { axiosInstance };
};
Expand Down
2 changes: 1 addition & 1 deletion services/web-app/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const authOptions: NextAuthOptions = {
async session({ session, token }) {
if (session.user) {
session.user.id = token.sub;
session.token = token;
session.token = token.token;
}

return session;
Expand Down
5 changes: 3 additions & 2 deletions services/web-app/types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { JWT } from "next-auth/jwt";

declare module "next-auth" {
interface Session {
token?: JWT;
token?: string;
user?: User;
expires?: ISODateString;
}

interface User {
name?: string;
email?: string;
picture?: string;
pictureUrl?: string;
id?: string;
repos?: [string];
}
}

Expand Down