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

Feat/add jwt to authjs #215

Merged
merged 12 commits into from
Aug 30, 2023
6 changes: 3 additions & 3 deletions services/core/entities/UserEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Table } from "./Table";
export const UserEntity = new Entity({
name: "userEntity",
attributes: {
PK: { partitionKey: true, hidden: true, prefix: "EMAIL#" },
PK: { partitionKey: true, hidden: true, prefix: "USERID#" },
SK: { sortKey: true, hidden: true, default: "ROOT" },
email: ["PK", 0, { type: "string", required: true }],
userId: { type: "string" },
userId: ["PK", 0, { type: "string", required: true }],
email: { type: "string" },
apiKey: { type: "binary" },
name: { type: "string" },
pictureUrl: { type: "string" },
Expand Down
8 changes: 4 additions & 4 deletions services/core/functions/get-user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { formatResponse } from "../../helpers/format-response";

export const main = async (event: APIGatewayProxyEvent) => {
try {
const email = event.queryStringParameters?.email;
const userId = event.queryStringParameters?.userId;

if (email === undefined) {
return formatResponse("Please provide the email of the user you wish to get.", 400)
if (userId === undefined) {
return formatResponse("Please provide the userId of the user you wish to get.", 400)
}

const response = await UserEntity.get({
email: email,
userId: userId,
});

console.log(response);
Expand Down
10 changes: 5 additions & 5 deletions services/core/functions/update-user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { formatResponse } from "../../helpers/format-response";

interface UpdateUserLambdaInput {
apiKey: string;
email: string;
userId: string;
}

export const main = async (event: APIGatewayProxyEvent) => {
Expand All @@ -20,9 +20,9 @@ export const main = async (event: APIGatewayProxyEvent) => {
try {
const inputBody = JSON.parse(event.body) as UpdateUserLambdaInput;
const apiKey = inputBody.apiKey;
const email = inputBody.email;
const userId = inputBody.userId;

if (apiKey === undefined || email === undefined) {
if (apiKey === undefined || userId === undefined) {
return formatResponse(
"The request body does not contain the expected data.",
400
Expand All @@ -33,10 +33,10 @@ export const main = async (event: APIGatewayProxyEvent) => {

await UserEntity.update(
{
email: email,
userId: userId,
apiKey: encryptedApiKey,
},
{ conditions: { attr: "email", exists: true } }
{ conditions: { attr: "userId", exists: true } }
);

return formatResponse("User updated successfully.");
Expand Down
2 changes: 1 addition & 1 deletion services/core/resources/constructs/review-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ReviewBucket extends Bucket {
removalPolicy: isProduction()
? RemovalPolicy.RETAIN
: RemovalPolicy.DESTROY,
autoDeleteObjects: !isProduction(),
// autoDeleteObjects: !isProduction(),
Copy link
Contributor

Choose a reason for hiding this comment

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

@mattzcarey Seb and I chatted about this. Adding this autoDeleteObjects thing is a bit broken, and it doesn't allow you to tear down your stack (you will see a couple of stacks already have DeleteFailed). So we have both commented this out for now. Might be a good things to investigate!

Copy link
Owner

Choose a reason for hiding this comment

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

Sounds good!

});
}
}
1 change: 1 addition & 0 deletions services/web-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For setting them in SST you would need to use the following commands:
```bash
npx sst secrets set GITHUB_ID sk_test_abc123
npx sst secrets set GITHUB_SECRET sk_test_abc123
npx sst secrets set NEXTAUTH_SECRET sk_test_abc123
```

To run the development server locally:
Expand Down
6 changes: 2 additions & 4 deletions services/web-app/src/app/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ export default function Profile(): JSX.Element {
const fetchData = async () => {
setLoading(true);
try {
const response = await axiosInstance.get(
`/getUser?email=${session?.user?.email}`
);
const response = await axiosInstance.get(`/getUser?userId=${session?.user?.id}`);
setData(response.data);
} catch (err: any) {
console.log("Failed to getUser");
console.log("Failed to getUser, due to the following error ", err);
} finally {
setLoading(false);
}
Expand Down
2 changes: 1 addition & 1 deletion services/web-app/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const BASE_URL = "https://mylf3hxjs8.execute-api.eu-west-2.amazonaws.com/prod";
export const BASE_URL = "https://s75mgjbsnb.execute-api.eu-west-2.amazonaws.com/prod";
export const InstallationInstructions = `
## We give engineers their weekends back

Expand Down
11 changes: 10 additions & 1 deletion services/web-app/src/lib/hooks/useAxios.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import axios, { AxiosInstance } from "axios";
import axios, { AxiosError, AxiosInstance } from "axios";
import { BASE_URL } from "@/lib/constants";
import { Session, getSession } from "next-auth/react";

const axiosInstance = axios.create({
baseURL: `${BASE_URL}`,
});

const useAxios = (): { axiosInstance: AxiosInstance } => {
const session = getSession();
axiosInstance.interceptors.request.clear();
axiosInstance.interceptors.request.use(
(config) => {
config.headers.Authorization = (session as Session).token;
Copy link
Owner

Choose a reason for hiding this comment

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

Love it

return config;
}
);
return { axiosInstance };
};

Expand Down
13 changes: 13 additions & 0 deletions services/web-app/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ const authOptions: NextAuthOptions = {
adapter: DynamoDBAdapter(dynamoClient, {
tableName: Table.auth.tableName,
}),
session: {
strategy: "jwt",
},
callbacks: {
async session({ session, token }) {
if (session.user) {
session.user.id = token.sub;
session.token = token;
}

return session;
},
},
};

export default NextAuth(authOptions);
Expand Down