-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* the callback route redirects to the home page on success, or an unauthorized page if no token is present or error page in error cases * creates error and unauthorized pages * adds middleware to implement the correct status codes on these redirects
- Loading branch information
1 parent
96f6b65
commit 65221c5
Showing
8 changed files
with
103 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Metadata } from "next"; | ||
|
||
import { useTranslations } from "next-intl"; | ||
import { getTranslations } from "next-intl/server"; | ||
import { GridContainer } from "@trussworks/react-uswds"; | ||
|
||
import ServerErrorAlert from "src/components/ServerErrorAlert"; | ||
|
||
export async function generateMetadata() { | ||
const t = await getTranslations(); | ||
const meta: Metadata = { | ||
title: t("ErrorPages.generic_error.page_title"), | ||
description: t("Index.meta_description"), | ||
}; | ||
return meta; | ||
} | ||
|
||
// not a NextJS error page - this is here to be redirected to manually in cases | ||
// where Next's error handling situation doesn't quite do what we need. | ||
const TopLevelError = () => { | ||
const t = useTranslations("Errors"); | ||
return ( | ||
<GridContainer> | ||
<ServerErrorAlert callToAction={t("try_again")} /> | ||
</GridContainer> | ||
); | ||
}; | ||
|
||
export default TopLevelError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Metadata } from "next"; | ||
|
||
import { useTranslations } from "next-intl"; | ||
import { getTranslations } from "next-intl/server"; | ||
import { Alert, GridContainer } from "@trussworks/react-uswds"; | ||
|
||
export async function generateMetadata() { | ||
const t = await getTranslations(); | ||
const meta: Metadata = { | ||
title: t("ErrorPages.unauthorized.page_title"), | ||
description: t("Index.meta_description"), | ||
}; | ||
return meta; | ||
} | ||
|
||
const Unauthorized = () => { | ||
const t = useTranslations("Errors"); | ||
return ( | ||
<GridContainer> | ||
<Alert type="error" heading={t("unauthorized")} headingLevel="h4"> | ||
{t("authorization_fail")} | ||
</Alert> | ||
</GridContainer> | ||
); | ||
}; | ||
|
||
export default Unauthorized; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,17 @@ | ||
import { createSession, getSession } from "src/services/auth/session"; | ||
import { createSession } from "src/services/auth/session"; | ||
|
||
import { redirect } from "next/navigation"; | ||
import { NextRequest } from "next/server"; | ||
|
||
const createSessionAndSetStatus = async ( | ||
token: string, | ||
successStatus: string, | ||
): Promise<string> => { | ||
try { | ||
await createSession(token); | ||
return successStatus; | ||
} catch (error) { | ||
console.error("error in creating session", error); | ||
return "error!"; | ||
} | ||
}; | ||
|
||
/* | ||
For now, we'll send them to generic success and error pages with cookie set on success | ||
message: str ("success" or "error") | ||
token: str | None | ||
is_user_new: bool | None | ||
error_description: str | None | ||
TODOS: | ||
- translating messages? | ||
- ... | ||
*/ | ||
export async function GET(request: NextRequest) { | ||
const currentSession = await getSession(); | ||
if (currentSession && currentSession.token) { | ||
const status = await createSessionAndSetStatus( | ||
currentSession.token, | ||
"already logged in", | ||
); | ||
return redirect(`/user?message=${status}`); | ||
} | ||
const token = request.nextUrl.searchParams.get("token"); | ||
if (!token) { | ||
return redirect("/user?message=no token provided"); | ||
return redirect("/unauthorized"); | ||
} | ||
try { | ||
await createSession(token); | ||
} catch (_e) { | ||
return redirect("/error"); | ||
} | ||
const status = await createSessionAndSetStatus(token, "created session"); | ||
return redirect(`/user?message=${status}`); | ||
return redirect("/"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters