Skip to content
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
3 changes: 2 additions & 1 deletion studio/app/projects/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@adobe/react-spectrum";
import { withPageAuthRequired } from "@auth0/nextjs-auth0/client";
import { ContentContainer } from "@core/components/ContentContainer";
import { withUserEnabled } from "@core/utils/withUserEnabled";
import Header from "@features/projects/components/Header";
import { useCreateProjects } from "@features/projects/hooks/useCreateProject";

Expand Down Expand Up @@ -53,4 +54,4 @@ function SecretPage() {
);
}

export default withPageAuthRequired(SecretPage);
export default withPageAuthRequired(withUserEnabled(SecretPage));
12 changes: 3 additions & 9 deletions studio/app/projects/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
"use client";

import {
ActionButton,
Button,
Flex,
Item,
ListView,
Text,
} from "@adobe/react-spectrum";
import { Button, Flex, Item, ListView, Text } from "@adobe/react-spectrum";
import { withPageAuthRequired } from "@auth0/nextjs-auth0/client";
import { ContentContainer } from "@core/components/ContentContainer";
import { NoData } from "@core/components/Empty";
import { withUserEnabled } from "@core/utils/withUserEnabled";
import Header from "@features/projects/components/Header";
import { useOwnProjects } from "@features/projects/hooks/useOwnProjects";
import File from "@spectrum-icons/illustrations/File";
Expand Down Expand Up @@ -60,7 +54,7 @@ function SecretPage() {
);
}

export default withPageAuthRequired(SecretPage);
export default withPageAuthRequired(withUserEnabled(SecretPage));

/*

Expand Down
21 changes: 21 additions & 0 deletions studio/core/utils/withUserEnabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useQuery } from "@core/hooks/useQuery";
import { hasAccess } from "@features/auth/hasAccess";
import { redirect } from "next/navigation";
import { useEffect } from "react";

export const withUserEnabled = (WrappedComponent: React.FC) => {
// eslint-disable-next-line react/display-name
return (props: any) => {
const { data: enabled } = useQuery({
queryFn: hasAccess,
defaultValue: undefined,
});

useEffect(() => {
if (enabled === false) redirect("/");
}, [enabled]);

if (!enabled) return null;
else return <WrappedComponent {...props} />;
};
};
26 changes: 26 additions & 0 deletions studio/features/auth/hasAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use server";

import { getUserToken } from "./user";
import { injectRepository } from "@features/db/helpers";
import { User } from "@features/db/entities/user";

export async function hasAccess() {
const user = await getUserToken();
const userRepository = await injectRepository(User);

const userModel = await userRepository.findOne({
where: { idAuth0: user.id },
});

if (!userModel) {
await userRepository.save({
id: user.id,
email: user.email,
picture: user.picture,
idAuth0: user.id,
});
return false;
} else {
return userModel.enabled;
}
}
6 changes: 5 additions & 1 deletion studio/features/db/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity()
@Entity({
name: "users",
})
export class User {
@PrimaryColumn() id!: string;

@Column() email!: string;
@Column({ nullable: true }) picture?: string;
@Column({ default: false }) enabled!: boolean;
@Column() idAuth0!: string;
}
105 changes: 105 additions & 0 deletions studio/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion studio/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions studio/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ beforeAll(async () => {
email: "test@test",
picture: "https://test.com/test.jpg",
projects: [],
idAuth0: "test",
enabled: true,
});

vi.mock("@auth0/nextjs-auth0", () => ({
Expand All @@ -19,6 +21,8 @@ beforeAll(async () => {
sub: "test",
email: "test@test",
picture: "https://example.com/picture.png",
idAuth0: "test",
enabled: true,
},
}),
}));
Expand Down