Skip to content

Commit

Permalink
feat: Move emails and protect dashboard sub-routes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
RudraPatel2003 authored Oct 14, 2024
1 parent 7400d9e commit 1fdf79f
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ jobs:
- name: Enforce formatting rules
run: pnpm run check-format

- name: Create .env file
run: |
touch .env
echo "MONGODB_URI=${{ secrets.MONGODB_URI }}" >> .env
echo "NEXTAUTH_URL=${{ secrets.NEXTAUTH_URL }}" >> .env
echo "NEXTAUTH_SECRET=${{ secrets.NEXTAUTH_SECRET }}" >> .env
echo "SCF_GMAIL=${{ secrets.SCF_GMAIL }}" >> .env
echo "SCF_GMAIL_APP_PASSWORD=${{ secrets.SCF_GMAIL_APP_PASSWORD }}" >> .env
- name: Check that project builds
run: pnpm run build

- name: Check that PR title follows conventional commits using regex
env:
TITLE: ${{ github.event.pull_request.title }}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"check-lint": "next lint",
"format": "prettier --write src",
"check-format": "prettier --check src",
"email-dev": "email dev --dir ./src/emails",
"email-build": "email build --dir ./src/emails",
"email-export": "email export --dir ./src/emails"
"email-dev": "email dev --dir ./src/components/emails",
"email-build": "email build --dir ./src/components/emails",
"email-export": "email export --dir ./src/components/emails"
},
"dependencies": {
"@emotion/cache": "^11.13.1",
Expand Down
10 changes: 10 additions & 0 deletions src/app/dashboard/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { redirect } from "next/navigation";

import getUserSession from "@/utils/getUserSession";

export default async function AdminDashboardPage() {
const session = await getUserSession();

if (!session || session.user.role !== "admin") {
return redirect("/dashboard");
}

return <p>Admin dashboard page</p>;
}
10 changes: 10 additions & 0 deletions src/app/dashboard/client/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { redirect } from "next/navigation";

import getUserSession from "@/utils/getUserSession";

export default async function ClientDashboardPage() {
const session = await getUserSession();

if (!session || session.user.role !== "client") {
return redirect("/dashboard");
}

return <p>Client dashboard page</p>;
}
8 changes: 5 additions & 3 deletions src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Typography } from "@mui/material";
import { redirect } from "next/navigation";

import getSession from "@/utils/getSession";
import getUserSession from "@/utils/getUserSession";

export default async function DashboardPage() {
const session = await getSession();
const session = await getUserSession();

if (!session) {
return <Typography>You must be logged in to view this page</Typography>;
throw new Error(
"The dashboard page should be protected by a session middleware, so session should never be null",
);
}

const role = session.user.role;
Expand Down
19 changes: 18 additions & 1 deletion src/app/test/client/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
"use client";

import { signOut } from "next-auth/react";

export default function ClientComponentTestPage() {
return <p>Client component test page</p>;
const handleSignOut = () => {
signOut()
.then(() => {
alert("Signed out");
})
.catch(() => {
alert("Error signing out");
});
};

return (
<>
<p>Client component test page</p>
<button onClick={handleSignOut}>Sign out</button>
</>
);
}
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default } from "next-auth/middleware";

export const config = { matcher: ["/dashboard"] };
export const config = { matcher: ["/dashboard", "/dashboard/:path*"] };
2 changes: 1 addition & 1 deletion src/utils/getSession.ts → src/utils/getUserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getServerSession } from "next-auth";
import authOptions from "@/app/api/auth/[...nextauth]/options";

// get session in server components
export default async function getSession() {
export default async function getUserSession() {
const session = await getServerSession(authOptions);
return session;
}

0 comments on commit 1fdf79f

Please sign in to comment.