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 all 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
2 changes: 1 addition & 1 deletion services/web-app/functions/add-user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const postEmail = async (email: string, name: string) => {
};

// eslint-disable-next-line complexity
export const main = async (event: DynamoDBStreamEvent): Promise<void> => {
export const main = async (event: DynamoDBStreamEvent): Promise<object> => {
if (event.Records.length === 0) {
return Promise.resolve({
statusCode: 400,
Expand Down
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
File renamed without changes
10 changes: 6 additions & 4 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 @@ -37,12 +37,13 @@ export default async function Profile(): Promise<JSX.Element> {
if (
session === null ||
session.user === undefined ||
session.user.id === undefined
session.user.userId === undefined
) {
throw new Error("Session data not fetched correctly.");
}
const response = await axiosInstance.get(
`/getUser?userId=${session.user.id}`
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`/getUser?userId=${session.user.userId}`
);
//We need response.data to be of type string so that it can be parsed into user data
if (typeof response.data !== "string") {
Expand Down Expand Up @@ -80,8 +81,9 @@ export default async function Profile(): Promise<JSX.Element> {
const handleUpdateApiKey = async (newApiKey: string) => {
try {
const response = await axiosInstance.post(`/updateUser`, {
userID: user.userId,
apiKey: newApiKey,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
userId: user.userId,
});
console.log("API key updated successfully:", response.data);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion services/web-app/src/components/buttons/basicButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface ButtonProps {
const BasicButton: React.FC<ButtonProps> = ({ text, onClick, styling }) => {
return (
<button
className={`bg-black text-white sm:font-light font-extralight py-2 px-4 rounded ${styling}`}
className={`bg-black text-white sm:font-light font-extralight py-2 px-4 rounded ${styling ?? ""}`}
onClick={onClick}
>
{text}
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
14 changes: 7 additions & 7 deletions services/web-app/src/lib/hooks/useAxios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,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.token ?? ""}`;
return config;
}
);

return { axiosInstance };
};
Expand Down
4 changes: 2 additions & 2 deletions services/web-app/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const authOptions: NextAuthOptions = {
// eslint-disable-next-line @typescript-eslint/require-await
async session({ session, token }) {
if (session.user) {
session.user.id = token.sub;
session.token = token;
session.user.userId = token.sub;
session.token = token.token;
}

return session;
Expand Down
7 changes: 4 additions & 3 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;
id?: string;
pictureUrl?: string;
userId?: string;
repos?: [string];
}
}

Expand Down