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 page redirects across all pages #515

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 3 additions & 6 deletions pages/admin/actions/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { getSession } from 'next-auth/react';
import Navbar from '../../../components/navbar';
import UpdateUserForm from '../../../components/updateUserForm';
import prisma from '../../../prisma/prisma';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(context) {
const userSession = await getSession(context);
if (!userSession) {
context.res.writeHead(302, { Location: '/' });
context.res.end();
return {};
return redirectUser('/error');
}

const user = await prisma.User.findUnique({
Expand All @@ -22,9 +21,7 @@ export async function getServerSideProps(context) {
});

if (user.role != 'ADMIN') {
context.res.writeHead(302, { Location: '/error' });
context.res.end();
return {};
return redirectUser('/error');
}

const userInfo = await prisma.User.findUnique({
Expand Down
9 changes: 3 additions & 6 deletions pages/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import Link from 'next/link';
import { getSession } from 'next-auth/react';
import prisma from '../../prisma/prisma';
import dynamic from 'next/dynamic';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(ctx) {
const userSession = await getSession(ctx);
if (!userSession) {
ctx.res.writeHead(302, { Location: '/' });
ctx.res.end();
return {};
return redirectUser('/error');
}

const user = await prisma.User.findUnique({
Expand All @@ -25,9 +24,7 @@ export async function getServerSideProps(ctx) {
});

if (user.role != 'ADMIN') {
ctx.res.writeHead(302, { Location: '/error' });
ctx.res.end();
return {};
return redirectUser('/error');
}

const users = await prisma.User.findMany({
Expand Down
19 changes: 4 additions & 15 deletions pages/classes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import Modal from '../../components/modal';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useState } from 'react';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(ctx) {
const userSession = await getSession(ctx);
if (!userSession) {
ctx.res.writeHead(302, { Location: '/' });
ctx.res.end();
return {};
return redirectUser('/error');
}

const userInfo = await prisma.User.findMany({
Expand All @@ -23,19 +22,9 @@ export async function getServerSideProps(ctx) {
}
});
if (userInfo[0].role == 'ADMIN') {
ctx.res.writeHead(302, { Location: '/admin' });
ctx.res.end();
// This prevents us from returning undefined prop obj which throws an error.
return {
props: {}
};
return redirectUser('/admin');
} else if (userInfo[0].role != 'TEACHER') {
ctx.res.writeHead(302, { Location: '/error' });
ctx.res.end();
// This prevents us from returning undefined prop obj which throws an error.
return {
props: {}
};
return redirectUser('/error');
}

const classrooms = await prisma.Classroom.findMany({
Expand Down
9 changes: 3 additions & 6 deletions pages/dashboard/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import {
getNonDashedNamesURLs,
getSuperBlockJsons
} from '../../util/api_proccesor';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(context) {
//making sure User is the teacher of this classsroom's dashboard
const userSession = await getSession(context);
if (!userSession) {
context.res.writeHead(302, { Location: '/' });
context.res.end();
return {};
return redirectUser('/error');
}
const userEmail = await prisma.User.findMany({
where: {
Expand All @@ -37,9 +36,7 @@ export async function getServerSideProps(context) {
});

if (userEmail[0].id !== classroomTeacherId['classroomTeacherId']) {
context.res.writeHead(302, { Location: '/classes' });
context.res.end();
return {};
return redirectUser('/classes');
}

const certificationNumbers = await prisma.classroom.findUnique({
Expand Down
9 changes: 3 additions & 6 deletions pages/dashboard/v2/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import {
formattedStudentData,
getCompletionTimestamps
} from '../../../util/api_proccesor';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(context) {
//making sure User is the teacher of this classsroom's dashboard
const userSession = await getSession(context);
if (!userSession) {
context.res.writeHead(302, { Location: '/' });
context.res.end();
return {};
return redirectUser('/error');
}

const userEmail = await prisma.User.findMany({
Expand All @@ -44,9 +43,7 @@ export async function getServerSideProps(context) {
userEmail[0].id == null ||
userEmail[0].id !== classroomTeacherId['classroomTeacherId']
) {
context.res.writeHead(302, { Location: '/classes' });
context.res.end();
return {};
return redirectUser('/classes');
}

const certificationNumbers = await prisma.classroom.findUnique({
Expand Down
9 changes: 3 additions & 6 deletions pages/dashboard/v2/details/[id]/[studentEmail].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import prisma from '../../../../../prisma/prisma';
import Navbar from '../../../../../components/navbar';
import { getSession } from 'next-auth/react';
import React from 'react';
import redirectUser from '../../util/redirectUser.js';

export async function getServerSideProps(context) {
//making sure User is the teacher of this classsroom's dashboard
const userSession = await getSession(context);

const studentEmail = context.params.studentEmail;
if (!userSession) {
context.res.writeHead(302, { Location: '/' });
context.res.end();
return {};
return redirectUser('/error');
}

const userEmail = await prisma.User.findMany({
Expand Down Expand Up @@ -46,9 +45,7 @@ export async function getServerSideProps(context) {
userEmail[0].id == null ||
userEmail[0].id !== classroomTeacherId['classroomTeacherId']
) {
context.res.writeHead(302, { Location: '/classes' });
context.res.end();
return {};
return redirectUser('/classes');
}

return {
Expand Down
17 changes: 17 additions & 0 deletions util/redirectUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Function to redirect user, within getServerSideProps functions.
* Leaving it blank will error out
* @param {string} arg1 The intended link to access. examples: '/error', '/admin', '/class', '/'.
* @return {[type]} HTTP request to redirected link
*/

const redirectUser = destination => {
return {
redirect: {
destination: destination,
permanent: false
}
};
};

export default redirectUser;
Copy link
Contributor

Choose a reason for hiding this comment

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

Tested the PR by doing the following (all checks resulted as expected):

  1. Initially signing in as user ‘NONE’ which is the default user when first signing on to the app -> this correctly showed the ‘Access Denied’ UI as intended
Screenshot 2024-02-10 at 6 33 54 PM
  1. Initially signing in as user ‘STUDENT’ -> this correctly showed the ‘Access Denied’ UI as intended
Screenshot 2024-02-10 at 6 33 54 PM
  1. Initially signing in as user ‘TEACHER’ -> this correctly shows the class creation UI as intended
Screenshot 2024-02-10 at 6 33 02 PM
  1. Initially signing in as user ‘ADMIN’ -> this correctly displays the admin dashboard
Screenshot 2024-02-10 at 6 38 55 PM

Edge case to consider:
When updating the user privileges in Prisma, the UI does not update immediately as the link is still set to https://{CODESPACES}.app.github.dev/error. This could mean that logic may need to be added which would update the URL (‘error’, ‘admin’, ‘teacher’, ‘student’) once a user privilege is updated.

Mentioning @utsab for further view.