generated from chingu-voyages/voyage-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from chingu-voyages/user-roles
added User model, user route & checkRole controller. Added middleware function.
- Loading branch information
Showing
10 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
import { Suspense } from "react"; | ||
import { CircularProgress, Typography } from "@mui/material"; | ||
import ReservationTable from "../../components/admin_dashboard/ReservationTable"; | ||
import Map from "../../components/admin_dashboard/Map"; | ||
import { getServerSession } from "next-auth"; | ||
import { authOptions } from "@/auth"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export default function AdminDashboardView() { | ||
return ( | ||
<div> | ||
<Typography variant="h1">Reservations:</Typography> | ||
<Suspense fallback={<CircularProgress />}> | ||
<ReservationTable /> | ||
<Map /> | ||
</Suspense> | ||
</div> | ||
); | ||
export default async function AdminDashboardView() { | ||
const session = await getServerSession(authOptions); | ||
if (!session) { | ||
return <p>You must be an admin to view this page.</p>; | ||
} | ||
if (session.user.role === "admin") { | ||
return ( | ||
<div> | ||
<Typography variant="h1">Reservations:</Typography> | ||
<Suspense fallback={<CircularProgress />}> | ||
<ReservationTable /> | ||
</Suspense> | ||
</div> | ||
); | ||
} else { | ||
return redirect("/form"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NextResponse } from "next/server"; | ||
import { withAuth } from "next-auth/middleware"; | ||
|
||
export default withAuth( | ||
// `withAuth` augments your `Request` with the user's token. | ||
function middleware(req) { | ||
if (req.nextauth.token.role === "admin"){ | ||
return NextResponse.redirect(new URL("/admin-dashboard", req.nextUrl)); | ||
} | ||
else { | ||
return NextResponse.redirect(new URL("/form", req.nextUrl)); | ||
} | ||
}, | ||
{ | ||
callbacks: { | ||
authorized: ({ token }) => token.id !== null, | ||
}, | ||
} | ||
); | ||
|
||
export const config = { matcher: ["/landingPage"] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import User from "../models/user.model.js"; | ||
|
||
export async function checkRole(req, res) { | ||
// grab currently logged in user's profile data from results | ||
console.log(JSON.stringify(req.headers)) | ||
const email = req.headers["x-user-email"]; | ||
console.log("email:", email) | ||
const user = await User.findOne({ | ||
email: email, | ||
}); | ||
if (user) { | ||
if (user.role === "admin") { | ||
res.json(user.role); | ||
} | ||
} | ||
else { | ||
res.json({ role: "user" }) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const userSchema = new mongoose.Schema({ | ||
email: String, | ||
role: String, | ||
}); | ||
|
||
const User = mongoose.model("User", userSchema, "users"); | ||
|
||
export default User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from "express"; | ||
import { checkRole } from "../controllers/user.controller.js"; | ||
|
||
const router = Router(); | ||
|
||
router.get("/", checkRole); | ||
|
||
export default router; |