Skip to content

Commit

Permalink
Merge pull request #7 from ecss-soton/restrict-signins
Browse files Browse the repository at this point in the history
Restrict signins
  • Loading branch information
Ortovoxx authored Nov 27, 2023
2 parents 3985057 + 1d24830 commit 093f347
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
25 changes: 12 additions & 13 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
pages: {
signIn: '/signin',
signOut: '/',
signOut: '/signout'
},
providers: [
AzureADProvider({
Expand All @@ -34,20 +34,27 @@ export const authOptions: NextAuthOptions = {
};
session.id = user.id;
return session
},
async signIn({ user, account, profile, email, credentials }) {
const holder = await prisma.ticketHolders.findFirst({
where: {
sotonId: user.sotonId || undefined,
}
})

return !!holder;
}
}
};

async function getPlusOnes(sotonId: string): Promise<{ error?: string, plusOnes: string[] }> {
async function getPlusOnes(sotonId: string): Promise<{ plusOnes: string[] }> {
const holder = await prisma.ticketHolders.findFirst({
where: {
sotonId: sotonId
}
})

if (!holder) return {error: 'Could not find ticket holder, have you bought a ticket?', plusOnes: []};

return { plusOnes: holder.plusOnes }
return { plusOnes: holder?.plusOnes || [] }
}

export default async function auth(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -74,8 +81,6 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {

const plusOne = await getPlusOnes(data.sotonId)

if (plusOne.error) return res.status(403).json({error: plusOne.error})

return {
id: data.sotonId,
firstName: data.firstName,
Expand All @@ -98,8 +103,6 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {

const plusOne = await getPlusOnes(sotonData.mail.split('@')[0])

if (plusOne.error) return res.status(403).json({error: plusOne.error})

return {
id: sotonData.mail.split('@')[0],
firstName: sotonData.givenName,
Expand All @@ -109,10 +112,6 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
plusOnes: plusOne.plusOnes,
}



// res.redirect(`https://sotonverify.link?callback=${process.env.NEXTAUTH_URL}`);

}

}
Expand Down
1 change: 0 additions & 1 deletion pages/scanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default function Qr() {
})

const res2 = await res.json()
console.log(res2)
if (res2.success) {
setData(res2);
} else {
Expand Down
2 changes: 0 additions & 2 deletions pages/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default function SignIn() {

const {data: session} = useSession();

console.log(session)

const { colorScheme, toggleColorScheme } = useMantineColorScheme();
const dark = colorScheme === 'dark';

Expand Down
72 changes: 72 additions & 0 deletions pages/signout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {unstable_getServerSession} from "next-auth";
import {LoginButton} from "@/components/LoginButton";
import {IncomingMessage, ServerResponse} from "http";
import {NextApiRequestCookies} from "next/dist/server/api-utils";
import {NextApiRequest, NextApiResponse} from "next";
import {authOptions} from "./api/auth/[...nextauth]";
import {Text, useMantineColorScheme} from "@mantine/core";
import {signIn, signOut, useSession} from "next-auth/react";
import {useRouter} from "next/router";
import Tables from "./seat-selection";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faMicrosoft} from "@fortawesome/free-brands-svg-icons";
import React from "react";

// @ts-ignore
export default function SignOut() {

const router = useRouter();

const {data: session} = useSession();

const { colorScheme, toggleColorScheme } = useMantineColorScheme();
const dark = colorScheme === 'dark';

return (
<div className="flex flex-col items-center justify-center min-h-screen py-2">

<main className="flex flex-col items-center justify-center w-full flex-1 px-5 text-center">

<div className='flex flex-row justify-center'>
{dark ? <img
className='max-h-72'
src="./WinterBallGF_1.png"
alt="ECSS Winter ball logo"
/> : <img
className='max-h-72'
src="./WinterBallGF_1.png"
alt="ECSS Winter ball logo"
/>}

</div>

<div onClick={() => signOut()}
className="cursor-pointer h-11 w-64 text-white max-w-300 bg-[#005C85] hover:bg-[#024460] rounded-md flex p-3 flex-row items-center justify-center m-2">
<div className='flex-none pr-2'>
<FontAwesomeIcon icon={faMicrosoft} className='text-white text-lg h-4 w-5'/>
</div>
Sign out
</div>

</main>
</div>
);
}

export async function getServerSideProps(context: { req: (IncomingMessage & { cookies: NextApiRequestCookies; }) | NextApiRequest; res: ServerResponse | NextApiResponse<any>; }) {

const session = await unstable_getServerSession(context.req, context.res, authOptions)

if (!session) {
return {
redirect: {
destination: '/signin',
permanent: false,
},
}
}

return {props: {}}
}

SignOut.auth = true;

0 comments on commit 093f347

Please sign in to comment.