Skip to content

Commit

Permalink
fix csrf and login redirect to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jun 13, 2024
1 parent 030cf2d commit 6dc4434
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const api = createApi({
baseQuery: async (args, api, extraOptions) => {
if (api.type === "mutation" && getCsrfCookie() === undefined) {
// Get the CSRF token.
const { error } = await fetch({ url: "", method: "GET" }, api, {})
const { error } = await fetch(
{ url: "/csrf/cookie", method: "GET" },
api,
{},
)

// Validate we got the CSRF token.
if (error !== undefined) {
Expand Down
25 changes: 23 additions & 2 deletions src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { FC } from "react"
import { useEffect, type FC } from "react"
import * as yup from "yup"

import * as page from "codeforlife/components/page"
import { useSearchParamEntries } from "codeforlife/hooks"
import {
useNavigate,
useSearchParamEntries,
useSessionMetadata,
} from "codeforlife/hooks"
import { tryValidateSync } from "codeforlife/utils/schema"

import { paths } from "../../router"
import IndyForm from "./IndyForm"
import * as studentForms from "./studentForms"
import * as teacherForms from "./teacherForms"
Expand All @@ -20,13 +25,29 @@ export interface LoginProps {
}

const Login: FC<LoginProps> = ({ form }) => {
const sessionMetadata = useSessionMetadata()
const navigate = useNavigate()

const searchParams = tryValidateSync(
useSearchParamEntries(),
yup.object({
verifyEmail: yup.boolean().default(false),
}),
)

useEffect(() => {
if (sessionMetadata && !sessionMetadata.auth_factors.length) {
navigate(
{
teacher: paths.teacher.dashboard.school._,
student: paths.student.dashboard._,
indy: paths.indy.dashboard._,
}[sessionMetadata.user_type],
{ replace: true },
)
}
}, [sessionMetadata, navigate])

return (
<page.Page>
{searchParams?.verifyEmail && (
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,7 @@ clsx@^2.1.0:

"codeforlife@github:ocadotechnology/codeforlife-package-javascript#auth_flow":
version "2.0.0"
resolved "https://codeload.github.com/ocadotechnology/codeforlife-package-javascript/tar.gz/182d467560528a7d64108e8a7d04ac76efedb2ba"
resolved "https://codeload.github.com/ocadotechnology/codeforlife-package-javascript/tar.gz/73814fa45fd15f8ea8f92a063163a8166980c549"
dependencies:
"@emotion/react" "^11.10.6"
"@emotion/styled" "^11.10.6"
Expand Down

0 comments on commit 6dc4434

Please sign in to comment.