Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import { UpdateSelf, getSelf, getUsers, RegisterSelf } from '@/app/lib/data';
import { redirectIfUnauthorized } from '@/app/lib/authGuard';
import {
RemoveMember,
InviteMember,
Expand Down Expand Up @@ -565,16 +566,7 @@ export default function Dashboard() {
try {
const data = await getSelf();

if (data?.error && typeof data.error === 'string') {
if (
data.error.includes('Something went wrong') ||
data.error.includes('not authenticated') ||
data.error.includes('Unauthorized')
) {
window.location.href = '/login';
return;
}
}
if (await redirectIfUnauthorized(data.statusCode)) return;

const points = await GetPoints();

Expand All @@ -589,7 +581,7 @@ export default function Dashboard() {
});

if (data.error != '') {
alert(data.error.message);
alert(data.error);
}

setUserData(data.response);
Expand Down
4 changes: 4 additions & 0 deletions app/dashboard/views/directorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getSelf, getUsers } from '@/app/lib/data';
import { generatePagination } from '@/app/lib/utils';
import { useState, useEffect } from 'react';
import { GetAllUsers } from '@/app/lib/actions';
import { redirectIfUnauthorized } from '@/app/lib/authGuard';
import { DeleteUser } from '@/app/lib/actions';
import { set } from 'zod';
import ConfirmDeleteModal from '@/app/ui/confirmDeleteModal';
Expand All @@ -36,6 +37,9 @@ function DirectorView(userData: any) {
const fetchUsers = async () => {
try {
const data = await GetAllUsers();

if (await redirectIfUnauthorized(data.statusCode)) return;

const users = data.response;
console.log(users);
setAllUsers(users);
Expand Down
3 changes: 3 additions & 0 deletions app/dashboard/views/organizerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import EventScan from './eventScan';
import { AttendEventScan, GetUser, SetUser } from '@/app/lib/actions';
import { handleSignOut } from '@/app/lib/actions';
import { getSelf } from '@/app/lib/data';
import { redirectIfUnauthorized } from '@/app/lib/authGuard';
import PopupDialog from '../components/dialog';
import { set } from 'zod';
import Page from '@/app/(pre-dashboard)/(landing)/page';
Expand Down Expand Up @@ -341,6 +342,8 @@ ${clues[5]}: ${clue5Done}`,
try {
const data = await getSelf();

if (await redirectIfUnauthorized(data.statusCode)) return;

const domain = data.response.email.slice(-11);
setIsSponsor(domain == 'sponsor.com');
if (domain == 'sponsor.com') {
Expand Down
10 changes: 7 additions & 3 deletions app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export async function SignUp(
}

export async function GetUser(email: string) {
let resp = {
let resp: { error: string; response: string; statusCode?: number } = {
error: '',
response: '',
};
Expand All @@ -301,14 +301,16 @@ export async function GetUser(email: string) {
if (res.status == 200) {
resp.response = res_json;
} else {
resp.error = res_json;
resp.error = res_json?.message || 'Unexpected Error';
resp.statusCode = res.status;
}
})
.catch((error) => {
resp.error = error + 'An error occured retrieving data';
});
} else {
resp.error = 'Please log in';
resp.statusCode = 401;
}
return resp;
}
Expand Down Expand Up @@ -1033,7 +1035,7 @@ export async function UserExists(email: string) {
}
export async function GetAllUsers() {
noStore();
let resp = {
let resp: { error: string; response: string; statusCode?: number } = {
error: '',
response: '',
};
Expand All @@ -1055,12 +1057,14 @@ export async function GetAllUsers() {
let resJSON = await res.json();
if (res.status !== 200) {
resp.error = 'Error Getting All Users';
resp.statusCode = res.status;
} else {
resp.response = resJSON;
}
});
} else {
resp.error = 'User not authenticated';
resp.statusCode = 401;
}
return resp;
}
Expand Down
18 changes: 18 additions & 0 deletions app/lib/authGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { handleSignOut } from './actions';

/**
* Backend calls return an inconsistent error shape, but a 401 always means the
* session's auth_token was rejected (expired or invalid). Call this right after
* any backend read that runs on page load; it clears the stale session and sends
* the user to /login instead of leaving them stuck on a half-loaded dashboard.
* Returns true if it redirected, so the caller can bail out of its effect.
*/
export async function redirectIfUnauthorized(
statusCode?: number,
): Promise<boolean> {
if (statusCode !== 401) return false;

await handleSignOut();
window.location.href = '/login';
return true;
}
3 changes: 3 additions & 0 deletions app/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export async function getLeaderboard() {
export async function getSelf(): Promise<{
error: any;
response: Record<string, any>;
statusCode?: number;
}> {
const session = await auth();

Expand All @@ -144,13 +145,15 @@ export async function getSelf(): Promise<{
return {
error: resp.error,
response: {},
statusCode: resp.statusCode,
};
}
}

return {
error: 'Something went wrong',
response: {},
statusCode: 401,
};
}

Expand Down
Loading