Skip to content

Commit 58d92a9

Browse files
committed
Most of the functionality implemented, some bugs to fix
1 parent 26505d2 commit 58d92a9

File tree

24 files changed

+101
-224
lines changed

24 files changed

+101
-224
lines changed

app/(api)/_datalib/auth/login.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use server';
22
import { NextResponse } from 'next/server';
3-
import bcrypt from 'bcryptjs';
43
import { signIn } from 'auth';
54

65
import { HttpError, NotAuthenticatedError } from '@utils/response/Errors';
@@ -17,24 +16,13 @@ export async function Login(body: { email: string; password: string }) {
1716
throw new NotAuthenticatedError('Judge not found');
1817
}
1918

20-
const judge = data.body[0];
21-
22-
const isPasswordValid = await bcrypt.compare(
23-
password as string,
24-
judge.password
25-
);
26-
27-
if (!isPasswordValid) {
28-
throw new NotAuthenticatedError('Email or Password do not match');
29-
}
30-
3119
const response = await signIn('credentials', {
32-
email: judge.email,
33-
password: judge.password,
20+
email: email,
21+
password: password,
3422
redirect: false,
3523
});
3624

37-
if (!response?.ok) {
25+
if (!response.ok) {
3826
throw new NotAuthenticatedError('Invalid login credentials');
3927
}
4028

app/(api)/_datalib/auth/register.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ export async function Register(body: JudgeInt) {
3333
throw new HttpError('Failed to create judge');
3434
}
3535

36-
const judge = data.body;
37-
3836
// Sign In
3937
const response = await signIn('credentials', {
40-
email: judge.email,
41-
password: judge.password,
38+
email: email,
39+
password: password,
4240
redirect: false,
4341
});
4442

app/(api)/_datalib/auth/resetPassword.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import bcrypt from 'bcryptjs';
55
import { GetManyJudges } from '@datalib/judges/getJudge';
66
import { UpdateJudge } from '@datalib/judges/updateJudge';
77
import { HttpError } from '@utils/response/Errors';
8+
import { signOut } from 'auth';
89

910
export async function ResetPassword(body: { email: string; password: string }) {
1011
try {
@@ -27,6 +28,8 @@ export async function ResetPassword(body: { email: string; password: string }) {
2728

2829
const updateData = await updateRes.json();
2930

31+
await signOut();
32+
3033
return NextResponse.json(
3134
{ ok: true, body: updateData, error: null },
3235
{ status: 200 }

app/(pages)/(index-page)/_components/JudgingHub/HubHero.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import JudgeInt from '@typeDefs/judges';
2-
import styles from './HubHero.module.scss';
1+
import { useSession } from 'next-auth/react';
2+
33
import Image from 'next/image';
44
import judgeHeroes from '/public/judges/hub/judgingheroes.svg';
5+
import styles from './HubHero.module.scss';
6+
7+
export default function HubHero() {
8+
const { data: _, status } = useSession();
59

6-
export default function HubHero({
7-
loading,
8-
}: {
9-
user: JudgeInt;
10-
loading: boolean;
11-
members: string[];
12-
}) {
13-
if (loading) {
10+
if (status === 'loading') {
1411
return 'loading...';
1512
}
1613

app/(pages)/(index-page)/_components/JudgingHub/JudgingHub.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import { useAuth } from '@hooks/useAuth';
2-
import styles from './JudgingHub.module.scss';
31
import HubHero from './HubHero';
42
import TableLocations from './TableLocations';
53
import ViewProjects from './ViewProjects';
6-
import { useJudgeGroup } from '@hooks/useJudgeGroup';
4+
import styles from './JudgingHub.module.scss';
75

8-
export default function JudgingHub() {
9-
const { user, loading } = useAuth();
10-
const { members } = useJudgeGroup();
6+
export default async function JudgingHub() {
117
return (
128
<div className={styles.container}>
139
<ViewProjects />
14-
<HubHero user={user} loading={loading} members={members} />
10+
<HubHero />
1511
{/* <JudgingList projects={unjudgedTeams} /> */}
1612
<TableLocations />
1713
</div>

app/(pages)/(index-page)/_components/LoginPage/LoginForm.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { useEffect, useState, ChangeEvent } from 'react';
22
import { useFormState } from 'react-dom';
3-
import { useAuth } from '@hooks/useAuth';
4-
import LoginAction from '@actions/auth/login';
3+
import { useRouter } from 'next/navigation';
4+
import Image from 'next/image';
5+
// import Link from 'next/link';
56

7+
import LoginAction from '@actions/auth/login';
68
import styles from './LoginForm.module.scss';
7-
// import Link from 'next/link';
8-
import Image from 'next/image';
9-
import AuthTokenInt from '@typeDefs/authToken';
109

1110
export default function LoginForm() {
11+
const router = useRouter();
12+
1213
const [email, setEmail] = useState('');
1314
const [password, setPassword] = useState('');
1415
const [isValid, setIsValid] = useState(false);
@@ -19,7 +20,6 @@ export default function LoginForm() {
1920
body: null,
2021
error: null,
2122
});
22-
const { login } = useAuth();
2323

2424
const handleEmailChange = (event: ChangeEvent<HTMLInputElement>) => {
2525
setEmail(event.target.value);
@@ -41,13 +41,13 @@ export default function LoginForm() {
4141

4242
useEffect(() => {
4343
if (loginState.ok === true) {
44-
const user = loginState.body as AuthTokenInt;
45-
login(user);
44+
setError('');
45+
router.push('/');
4646
} else {
4747
const err = loginState.error as string;
4848
setError(err);
4949
}
50-
}, [loginState, login]);
50+
}, [loginState, router]);
5151

5252
useEffect(() => {
5353
validateForm(email, password);

app/(pages)/(index-page)/_components/LogoutButton/LogoutButton.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useAuth } from '@hooks/useAuth';
3+
import { signOut } from 'auth';
44

55
export default function LogoutButton({
66
children,
@@ -9,9 +9,14 @@ export default function LogoutButton({
99
children?: React.ReactNode;
1010
style?: object;
1111
}) {
12-
const { logout } = useAuth();
1312
return (
14-
<button style={style} onClick={logout}>
13+
<button
14+
style={style}
15+
onClick={async () => {
16+
'use server';
17+
await signOut();
18+
}}
19+
>
1520
{children}
1621
</button>
1722
);

app/(pages)/(index-page)/_components/RegisterForm/RegisterForm.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { useEffect, useState, ChangeEvent } from 'react';
22
import { useFormState } from 'react-dom';
3-
import { useAuth } from '@hooks/useAuth';
4-
import Register from '@actions/auth/register';
5-
6-
import styles from './RegisterForm.module.scss';
3+
import { useRouter } from 'next/navigation';
74
import Link from 'next/link';
8-
import AuthTokenInt from '@typeDefs/authToken';
5+
6+
import Register from '@actions/auth/register';
97
import { useInvite } from '@hooks/useInvite';
10-
import { useRouter } from 'next/navigation';
8+
import styles from './RegisterForm.module.scss';
119

1210
export default function RegisterForm() {
1311
const router = useRouter();
@@ -26,7 +24,6 @@ export default function RegisterForm() {
2624
body: null,
2725
error: null,
2826
});
29-
const { login } = useAuth();
3027

3128
const handleEmailChange = (event: ChangeEvent<HTMLInputElement>) => {
3229
setEmail(event.target.value);
@@ -74,15 +71,13 @@ export default function RegisterForm() {
7471

7572
useEffect(() => {
7673
if (registerState.ok === true) {
77-
const user = registerState.body as AuthTokenInt;
7874
setError('');
79-
login(user);
8075
router.push('/');
8176
} else {
8277
const err = registerState.error as string;
8378
setError(err);
8479
}
85-
}, [registerState, login, router]);
80+
}, [registerState, router]);
8681

8782
useEffect(() => {
8883
validateForm(email, password, passwordDupe);

app/(pages)/(index-page)/_components/ResetPasswordForm/ResetPasswordForm.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useState, ChangeEvent } from 'react';
22
import { useFormState } from 'react-dom';
3-
import { useAuth } from '@hooks/useAuth';
43
import ResetPassword from '@actions/auth/resetPassword';
54
import styles from './ResetPasswordForm.module.scss';
65
import Link from 'next/link';
@@ -22,7 +21,6 @@ export default function ResetPasswordForm() {
2221
body: null,
2322
error: null,
2423
});
25-
const { logout } = useAuth();
2624

2725
const handlePasswordChange = (event: ChangeEvent<HTMLInputElement>) => {
2826
setPassword(event.target.value);
@@ -57,13 +55,12 @@ export default function ResetPasswordForm() {
5755
useEffect(() => {
5856
if (resetState.ok === true) {
5957
setError('');
60-
logout();
6158
router.push('/judges');
6259
} else {
6360
const err = resetState.error as string;
6461
setError(err);
6562
}
66-
}, [resetState, logout, router]);
63+
}, [resetState, router]);
6764

6865
useEffect(() => {
6966
validateForm(password, passwordDupe);
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import AdminProtected from '@components/AdminProtected/AdminProtected';
1+
import ProtectedDisplay from '@components/ProtectedDisplay/ProtectedDisplay';
22
import { Metadata } from 'next';
33

44
export const metadata: Metadata = {
@@ -11,11 +11,8 @@ export default function AdminLayout({
1111
children: React.ReactNode;
1212
}) {
1313
return (
14-
<AdminProtected
15-
loadingDisplay={'loading...'}
16-
failDisplay={"you aren't the admin 😡"}
17-
>
14+
<ProtectedDisplay allowedRoles="admin" failRedirectPath="/">
1815
{children}
19-
</AdminProtected>
16+
</ProtectedDisplay>
2017
);
2118
}

app/(pages)/(index-page)/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AuthProvider } from '../_contexts/AuthContext';
21
import { Metadata } from 'next';
2+
import { SessionProvider } from 'next-auth/react';
33

44
type Props = {
55
children: React.ReactNode;
@@ -10,5 +10,5 @@ export const metadata: Metadata = {
1010
};
1111

1212
export default function JudgesLayout({ children }: Props) {
13-
return <AuthProvider>{children}</AuthProvider>;
13+
return <SessionProvider>{children}</SessionProvider>;
1414
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use client';
2+
import { Suspense } from 'react';
3+
import LoginPage from '../_components/LoginPage/LoginPage';
4+
5+
export default function Login() {
6+
return (
7+
<Suspense>
8+
<LoginPage />
9+
</Suspense>
10+
);
11+
}

app/(pages)/(index-page)/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use client';
22
import JudgingHub from './_components/JudgingHub/JudgingHub';
33
import ProtectedDisplay from '@components/ProtectedDisplay/ProtectedDisplay';
4-
import LoginPage from './_components/LoginPage/LoginPage';
54

65
export default function Judges() {
76
return (
8-
<ProtectedDisplay loadingDisplay={'loading...'} failDisplay={<LoginPage />}>
7+
<ProtectedDisplay allowedRoles="admin judge" failRedirectPath="/login">
98
<JudgingHub />
109
</ProtectedDisplay>
1110
);

app/(pages)/(index-page)/projects/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import { useState } from 'react';
44
import ProjectsHeader from './_components/ProjectsHeader';
55
import ProjectsList from './_components/ProjectsList';
66
import ProtectedDisplay from '@components/ProtectedDisplay/ProtectedDisplay';
7-
import LoginPage from '../_components/LoginPage/LoginPage';
87
// import SearchBar from './_components/SearchBar';
98

109
export default function Judges() {
1110
const [activeTab, setActiveTab] = useState(0);
1211
return (
13-
<ProtectedDisplay loadingDisplay={'loading...'} failDisplay={<LoginPage />}>
12+
<ProtectedDisplay allowedRoles="admin judge" failRedirectPath="/login">
1413
<div>
1514
<ProjectsHeader activeTab={activeTab} setActiveTab={setActiveTab} />
1615
{/* <SearchBar /> */}

app/(pages)/(index-page)/scoring/layout.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use client';
22

33
import ProtectedDisplay from '@components/ProtectedDisplay/ProtectedDisplay';
4-
import LoginPage from '../_components/LoginPage/LoginPage';
54

65
type Props = {
76
children: React.ReactNode;
87
};
98

109
export default function ScoringLayout({ children }: Props) {
1110
return (
12-
<ProtectedDisplay loadingDisplay={'loading...'} failDisplay={<LoginPage />}>
11+
<ProtectedDisplay allowedRoles="admin judge" failRedirectPath="/login">
1312
{children}
1413
</ProtectedDisplay>
1514
);

app/(pages)/_components/AdminProtected/AdminProtected.tsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)