Skip to content

Commit 066810e

Browse files
victoriavo256dyluriYP66yydsraisedadeadutsab
authored
fix page redirects across all pages (#515)
* page redirects fixed Co-authored by: Dylan Uribe <[email protected]> Co-authored by: Ann Pan <[email protected]> * Revert "page redirects fixed" This reverts commit 8339fea. * page redirects fixed Co-authored-by: Dylan Uribe <[email protected]> Co-authored-by: Ann Pan <[email protected]> * fixed minor errors Co-authored-by: Dylan Uribe <[email protected]> Co-authored-by: Ann Pan <[email protected]> * removed faulty redirectUser test * chore: delete workflow * Fixing file path for the redirect function --------- Co-authored-by: Dylan Uribe <[email protected]> Co-authored-by: Ann Pan <[email protected]> Co-authored-by: Mrugesh Mohapatra <[email protected]> Co-authored-by: Utsab Saha <[email protected]>
1 parent b9e5a4e commit 066810e

File tree

7 files changed

+36
-45
lines changed

7 files changed

+36
-45
lines changed

pages/admin/actions/[id].js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { getSession } from 'next-auth/react';
22
import Navbar from '../../../components/navbar';
33
import UpdateUserForm from '../../../components/updateUserForm';
44
import prisma from '../../../prisma/prisma';
5+
import redirectUser from '../../util/redirectUser.js';
56

67
export async function getServerSideProps(context) {
78
const userSession = await getSession(context);
89
if (!userSession) {
9-
context.res.writeHead(302, { Location: '/' });
10-
context.res.end();
11-
return {};
10+
return redirectUser('/error');
1211
}
1312

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

2423
if (user.role != 'ADMIN') {
25-
context.res.writeHead(302, { Location: '/error' });
26-
context.res.end();
27-
return {};
24+
return redirectUser('/error');
2825
}
2926

3027
const userInfo = await prisma.User.findUnique({

pages/admin/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import Link from 'next/link';
55
import { getSession } from 'next-auth/react';
66
import prisma from '../../prisma/prisma';
77
import dynamic from 'next/dynamic';
8+
import redirectUser from '../../util/redirectUser.js';
89

910
export async function getServerSideProps(ctx) {
1011
const userSession = await getSession(ctx);
1112
if (!userSession) {
12-
ctx.res.writeHead(302, { Location: '/' });
13-
ctx.res.end();
14-
return {};
13+
return redirectUser('/error');
1514
}
1615

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

2726
if (user.role != 'ADMIN') {
28-
ctx.res.writeHead(302, { Location: '/error' });
29-
ctx.res.end();
30-
return {};
27+
return redirectUser('/error');
3128
}
3229

3330
const users = await prisma.User.findMany({

pages/classes/index.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import Modal from '../../components/modal';
88
import { ToastContainer } from 'react-toastify';
99
import 'react-toastify/dist/ReactToastify.css';
1010
import { useState } from 'react';
11+
import redirectUser from '../../util/redirectUser.js';
1112

1213
export async function getServerSideProps(ctx) {
1314
const userSession = await getSession(ctx);
1415
if (!userSession) {
15-
ctx.res.writeHead(302, { Location: '/' });
16-
ctx.res.end();
17-
return {};
16+
return redirectUser('/error');
1817
}
1918

2019
const userInfo = await prisma.User.findMany({
@@ -23,19 +22,9 @@ export async function getServerSideProps(ctx) {
2322
}
2423
});
2524
if (userInfo[0].role == 'ADMIN') {
26-
ctx.res.writeHead(302, { Location: '/admin' });
27-
ctx.res.end();
28-
// This prevents us from returning undefined prop obj which throws an error.
29-
return {
30-
props: {}
31-
};
25+
return redirectUser('/admin');
3226
} else if (userInfo[0].role != 'TEACHER') {
33-
ctx.res.writeHead(302, { Location: '/error' });
34-
ctx.res.end();
35-
// This prevents us from returning undefined prop obj which throws an error.
36-
return {
37-
props: {}
38-
};
27+
return redirectUser('/error');
3928
}
4029

4130
const classrooms = await prisma.Classroom.findMany({

pages/dashboard/[id].js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import {
1212
getNonDashedNamesURLs,
1313
getSuperBlockJsons
1414
} from '../../util/api_proccesor';
15+
import redirectUser from '../../util/redirectUser.js';
1516

1617
export async function getServerSideProps(context) {
1718
//making sure User is the teacher of this classsroom's dashboard
1819
const userSession = await getSession(context);
1920
if (!userSession) {
20-
context.res.writeHead(302, { Location: '/' });
21-
context.res.end();
22-
return {};
21+
return redirectUser('/error');
2322
}
2423
const userEmail = await prisma.User.findMany({
2524
where: {
@@ -37,9 +36,7 @@ export async function getServerSideProps(context) {
3736
});
3837

3938
if (userEmail[0].id !== classroomTeacherId['classroomTeacherId']) {
40-
context.res.writeHead(302, { Location: '/classes' });
41-
context.res.end();
42-
return {};
39+
return redirectUser('/classes');
4340
}
4441

4542
const certificationNumbers = await prisma.classroom.findUnique({

pages/dashboard/v2/[id].js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import {
1414
fetchStudentData,
1515
checkIfStudentHasProgressDataForSuperblocksSelectedByTeacher
1616
} from '../../../util/api_proccesor';
17+
import redirectUser from '../../../util/redirectUser.js';
1718

1819
export async function getServerSideProps(context) {
1920
//making sure User is the teacher of this classsroom's dashboard
2021
const userSession = await getSession(context);
2122
if (!userSession) {
22-
context.res.writeHead(302, { Location: '/' });
23-
context.res.end();
24-
return {};
23+
return redirectUser('/error');
2524
}
2625

2726
const userEmail = await prisma.User.findMany({
@@ -44,9 +43,7 @@ export async function getServerSideProps(context) {
4443
userEmail[0].id == null ||
4544
userEmail[0].id !== classroomTeacherId['classroomTeacherId']
4645
) {
47-
context.res.writeHead(302, { Location: '/classes' });
48-
context.res.end();
49-
return {};
46+
return redirectUser('/classes');
5047
}
5148

5249
const certificationNumbers = await prisma.classroom.findUnique({

pages/dashboard/v2/details/[id]/[studentEmail].js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getIndividualStudentData
1313
} from '../../../../../util/api_proccesor';
1414
import React from 'react';
15+
import redirectUser from '../../../../../util/redirectUser.js';
1516
import styles from '../../../../../components/DetailsCSS.module.css';
1617
import DetailsDashboard from '../../../../../components/DetailsDashboard';
1718

@@ -21,9 +22,7 @@ export async function getServerSideProps(context) {
2122

2223
const studentEmail = context.params.studentEmail;
2324
if (!userSession) {
24-
context.res.writeHead(302, { Location: '/' });
25-
context.res.end();
26-
return {};
25+
return redirectUser('/error');
2726
}
2827

2928
const userEmail = await prisma.User.findMany({
@@ -55,9 +54,7 @@ export async function getServerSideProps(context) {
5554
userEmail[0].id == null ||
5655
userEmail[0].id !== classroomTeacherId['classroomTeacherId']
5756
) {
58-
context.res.writeHead(302, { Location: '/classes' });
59-
context.res.end();
60-
return {};
57+
return redirectUser('/classes');
6158
}
6259

6360
const certificationNumbers = await prisma.classroom.findUnique({

util/redirectUser.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Function to redirect user, within getServerSideProps functions.
3+
* Leaving it blank will error out
4+
* @param {string} arg1 The intended link to access. examples: '/error', '/admin', '/class', '/'.
5+
* @return {[type]} HTTP request to redirected link
6+
*/
7+
8+
const redirectUser = destination => {
9+
return {
10+
redirect: {
11+
destination: destination,
12+
permanent: false
13+
}
14+
};
15+
};
16+
17+
export default redirectUser;

0 commit comments

Comments
 (0)