Skip to content

Commit

Permalink
Merge pull request ItzyBitzySpider#11 from samuzora/admin
Browse files Browse the repository at this point in the history
Admin
  • Loading branch information
beanbeah authored Oct 26, 2022
2 parents 5dc1fb2 + 4f42400 commit 6302ad9
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 63 deletions.
34 changes: 0 additions & 34 deletions package-lock.json

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

12 changes: 7 additions & 5 deletions pages/admin.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { getSession, useSession } from 'next-auth/react';
import { useSession } from 'next-auth/react';
import { getAllSubmissions } from '../server/challengeFunctions';
import { getAllLogs } from '../server/logging';
import styles from '../styles/admin.module.scss';
import dayjs from 'dayjs';
import { useEffect, useState } from 'react';
import { Col, Row, Form, Button, Toast } from 'react-bootstrap';
import { useState } from 'react';
import { Col, Row, Form, Button } from 'react-bootstrap';
import Log from '../components/log';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { unstable_getServerSession } from 'next-auth';
import { authOptions } from './api/auth/[...nextauth]';

export default function Admin({ submissions, logs }) {
const { data: session, status } = useSession();
Expand Down Expand Up @@ -146,8 +148,8 @@ export default function Admin({ submissions, logs }) {
}

export async function getServerSideProps(context) {
const session = await getSession(context);
if (!session) return { props: { submissions: [], logs: [] } };
const session = await unstable_getServerSession(context.req, context.res, authOptions);
if (session?.user.role !== "ADMIN") return { notFound: true };
let submissions = await getAllSubmissions();
if (!submissions) submissions = [];
let logs = await getAllLogs();
Expand Down
5 changes: 3 additions & 2 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import GithubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';
import DiscordProvider from 'next-auth/providers/discord';

export default NextAuth({
export const authOptions = {
adapter: PrismaAdapter(prisma),
providers: [
EmailProvider({
Expand Down Expand Up @@ -69,5 +69,6 @@ export default NextAuth({
},
},
secret: process.env.JWT_SECRET,
});
};

export default NextAuth(authOptions);
7 changes: 4 additions & 3 deletions pages/api/changeUsername.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { changeUsername } from '../../server/userFunctions';
import { authOptions } from './auth/[...nextauth]';

export default async function submitUsername(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand All @@ -24,7 +25,7 @@ export default async function submitUsername(req, res) {
}

//Change Username
let result = await changeUsername(userId, username);
const result = await changeUsername(userId, username);
res.status(200).json({ result: result });
} else {
// Not Signed in
Expand Down
9 changes: 5 additions & 4 deletions pages/api/deleteLogs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { clearAllLogs } from '../../server/logging';
import { authOptions } from './auth/[...nextauth]';

export default async function deleteLogs(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);

if (session && session.user.role !== 'USER') {
if (session && session.user.role !== 'ADMIN') {
res.status(401).json({ error: 'Unauthorized' });
return;
}

//Delete user
let result = await clearAllLogs();
const result = await clearAllLogs();
res.status(200).json({ result: 'success' });;
return;
} else {
Expand Down
5 changes: 3 additions & 2 deletions pages/api/deleteUser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { deleteAccount } from '../../server/userFunctions';
import { authOptions } from './auth/[...nextauth]';

export default async function deleteUser(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/extendTimeInstance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { userEnabled } from '../../server/userFunctions';
import { authOptions } from './auth/[...nextauth]';

export default async function extendTimeInstance(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/getTimeLeft.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { userEnabled } from '../../server/userFunctions';
import { authOptions } from './auth/[...nextauth]';

export default async function getTimeLeft(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
3 changes: 2 additions & 1 deletion pages/api/startInstance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { unstable_getServerSession } from 'next-auth';
import { getSession } from 'next-auth/react';
import { validateChallengeHash } from '../../server/challengeFunctions';
import { userEnabled } from '../../server/userFunctions';

export default async function startInstance(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/stopInstance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSession } from 'next-auth/react';
import { unstable_getServerSession } from 'next-auth';
import { userEnabled } from '../../server/userFunctions';
import { authOptions } from './auth/[...nextauth]';

export default async function stopInstance(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req });
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/submitFlag.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getSession } from "next-auth/react"
import { unstable_getServerSession } from "next-auth";
import { getChallengeById, getLastSubmission, submitFlag } from '../../server/challengeFunctions';
import { userEnabled } from "../../server/userFunctions";
import { authOptions } from "./auth/[...nextauth]";

export default async function submit(req, res) {
if (req.method === 'POST') {
const session = await getSession({ req })
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
// Signed in
let userId = session.user.id;
Expand Down
6 changes: 4 additions & 2 deletions pages/challenges.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getSession, useSession } from 'next-auth/react';
import { authOptions } from "./api/auth/[...nextauth]"
import { useSession } from 'next-auth/react';
import { useState } from 'react';
import { Col, Row } from 'react-bootstrap';
import Challenge from '../components/challenge';
Expand All @@ -9,6 +10,7 @@ import {
} from '../server/challengeFunctions';
import Filter from '../components/multiSelect';
import Unauthorized from '../components/unauthorized';
import { unstable_getServerSession } from 'next-auth';

export default function Challenges({
challengeData,
Expand Down Expand Up @@ -136,7 +138,7 @@ export default function Challenges({

// Get challenges
export async function getServerSideProps(context) {
const session = await getSession(context);
const session = await unstable_getServerSession(context.req, context.res, authOptions);
if (!session) return { props: { challengeData: [], solvedIDs: [] } };
const challengeData = await getAllChallenges();
const userSolved = await getChallengesSolved(session.user.id);
Expand Down
6 changes: 4 additions & 2 deletions pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSession, signOut, useSession } from 'next-auth/react';
import { signOut, useSession } from 'next-auth/react';
import { useState } from 'react';
import { Button, Row, Col } from 'react-bootstrap';
import ModalForm from '../components/modalForm';
Expand All @@ -9,6 +9,8 @@ import styles from '../styles/profile.module.scss';
import Router from 'next/router';
import { getChallengesSolved } from '../server/challengeFunctions';
import Unauthorized from '../components/unauthorized';
import { unstable_getServerSession } from 'next-auth';
import { authOptions } from './api/auth/[...nextauth]';

export default function Profile({ challengeSolved }) {
const { data: session, status } = useSession();
Expand Down Expand Up @@ -157,7 +159,7 @@ export default function Profile({ challengeSolved }) {
}

export async function getServerSideProps(context) {
const session = await getSession(context);
const session = await unstable_getServerSession(context.req, context.res, authOptions);
if (!session) return { props: { challengeSolved: [] } };
let challengeSolved = await getChallengesSolved(session.user.id);
if (!challengeSolved) {
Expand Down

0 comments on commit 6302ad9

Please sign in to comment.