Skip to content

Commit ffa692f

Browse files
committed
fix(portal): user would be redirected to incorrect pages depending on state
1 parent 2c69486 commit ffa692f

File tree

5 files changed

+33
-51
lines changed

5 files changed

+33
-51
lines changed

apps/portal/app/routes/_index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ export const loader: LoaderFunction = async ({ request }) => {
2929
return redirect('/profile')
3030
}
3131

32+
const forms = await fetch(`${API_URL}/api/form-submissions?where[submittedBy][equals]=${user.id}`, {
33+
method: 'GET',
34+
headers: { Cookie: cookie || '' },
35+
})
36+
37+
const userForms = await forms.json()
38+
if (!userForms.docs.length) {
39+
return redirect('/registration')
40+
}
41+
3242
return redirect('/dashboard')
3343
}
44+
3445
catch (error) {
3546
return redirect('/login')
3647
}

apps/portal/app/routes/dashboard.tsx

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
1-
/* eslint-disable unused-imports/no-unused-vars */
2-
import type { User } from '@cuhacking/portal/types/user'
1+
import type { UserDetails } from '@cuhacking/portal/types/user'
32
import type { LoaderFunction } from '@remix-run/node'
43
import process from 'node:process'
54
import { Home } from '@cuhacking/portal/pages/index/index'
65
import { json, redirect } from '@remix-run/node'
76
import { useLoaderData } from '@remix-run/react'
87

9-
/* export const loader: LoaderFunction = async () => {
10-
* const snapshot = userFlowActor.getSnapshot()
11-
* if (!snapshot) {
12-
* return redirect('/login')
13-
* }
14-
*
15-
* const currentState = snapshot.value
16-
* const error = snapshot.context.error
17-
*
18-
* if (error) {
19-
* return redirect('/error')
20-
* }
21-
*
22-
* switch (currentState) {
23-
* case 'unauthenticated':
24-
* return redirect('/login')
25-
* case 'legal':
26-
* return redirect('/terms')
27-
* case 'profile_incomplete':
28-
* return redirect('/profile')
29-
* case 'dashboard':
30-
* case 'registered':
31-
* return null
32-
* default:
33-
* return redirect('/login')
34-
* }
35-
* } */
36-
378
export const loader: LoaderFunction = async ({ request }) => {
389
const cookie = request.headers.get('Cookie')
3910

4011
try {
41-
const API_URL = `${process.env.NODE_ENV} === 'development' ? http://localhost:8000 : https://axiom.cuhacking.ca`
12+
const API_URL = process.env.NODE_ENV === 'development' ? 'http://localhost:8000' : 'https://axiom.cuhacking.ca'
4213
const res = await fetch(`${API_URL}/api/users/me`, {
4314
headers: { Cookie: cookie || '' },
4415
})
@@ -50,36 +21,36 @@ export const loader: LoaderFunction = async ({ request }) => {
5021
const { user } = await res.json()
5122

5223
if (!user) {
53-
return redirect('/login')
24+
return redirect('/')
5425
}
5526

5627
if (!user.agreedToTerms) {
57-
return redirect('/terms')
28+
return redirect('/')
5829
}
5930

6031
if (!user.emergencyContactFullName) {
61-
return redirect('/profile')
32+
return redirect('/')
6233
}
6334

64-
/* const forms = await fetch(`${API_URL}/api/form-submissions?where[submittedBy][equals]=${user.id}`, {
65-
* method: 'GET',
66-
* headers: { Cookie: cookie || '' },
67-
* })
35+
const forms = await fetch(`${API_URL}/api/form-submissions?where[submittedBy][equals]=${user.id}`, {
36+
method: 'GET',
37+
headers: { Cookie: cookie || '' },
38+
})
6839

69-
* const userForms = await forms.json()
70-
* if (!userForms.docs.length) {
71-
* return redirect('/registration')
72-
* } */
40+
const userForms = await forms.json()
41+
if (!userForms.docs.length) {
42+
return redirect('/registration')
43+
}
7344

7445
return json(user)
7546
}
76-
catch (error) {
77-
return redirect('/login')
47+
catch {
48+
return redirect('/')
7849
}
7950
}
8051

8152
export default function Dashboard() {
82-
const user = useLoaderData<User>()
53+
const user = useLoaderData<UserDetails>()
8354

8455
return <Home user={user} />
8556
}

apps/portal/app/routes/profile.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ export const loader: LoaderFunction = async ({ request }) => {
2626
const { user } = await res.json()
2727

2828
if (!user) {
29-
return redirect('/login')
29+
return redirect('/')
3030
}
3131

3232
return json({ user, cookie, API_URL })
3333
}
3434
catch {
35-
return redirect('/login')
35+
return redirect('/')
3636
}
3737
}
3838

apps/portal/app/routes/registration.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const loader: LoaderFunction = async ({ request }) => {
2424
const { user } = await me.json()
2525

2626
if (!user) {
27-
return redirect('/login')
27+
return redirect('/')
2828
}
2929

3030
const forms = await fetch(`${API_URL}/api/form-submissions?where[submittedBy][equals]=${user.id}`, {
@@ -33,12 +33,12 @@ export const loader: LoaderFunction = async ({ request }) => {
3333
})
3434
const userForms = await forms.json()
3535
if (userForms.docs.length) {
36-
return redirect('/dashboard')
36+
return redirect('/')
3737
}
3838
return json({ user, cookie, API_URL })
3939
}
4040
catch {
41-
return redirect('/login')
41+
return redirect('/')
4242
}
4343
}
4444

apps/portal/app/routes/terms.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const action: ActionFunction = async ({ request }) => {
3838
const userId = session.get('userId')
3939

4040
if (!userId) {
41-
return redirect('/login')
41+
return redirect('/')
4242
}
4343
return await updateTerms(userId, cookie)
4444
}

0 commit comments

Comments
 (0)