Skip to content

Commit

Permalink
feat: use unstable_getServerSession for the other pages as well
Browse files Browse the repository at this point in the history
  • Loading branch information
samuzora committed Oct 26, 2022
1 parent 7736038 commit 4f42400
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 23 deletions.
10 changes: 6 additions & 4 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,7 +148,7 @@ export default function Admin({ submissions, logs }) {
}

export async function getServerSideProps(context) {
const session = await getSession(context);
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 = [];
Expand Down
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
7 changes: 4 additions & 3 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 !== '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
2 changes: 1 addition & 1 deletion pages/challenges.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { authOptions } from "./api/auth/[...nextauth]"
import { getSession, useSession } from 'next-auth/react';
import { useSession } from 'next-auth/react';
import { useState } from 'react';
import { Col, Row } from 'react-bootstrap';
import Challenge from '../components/challenge';
Expand Down
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit 4f42400

Please sign in to comment.