-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve idp integration using server action
- Loading branch information
Showing
6 changed files
with
363 additions
and
314 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
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
41 changes: 41 additions & 0 deletions
41
apps/login/src/components/sign-in-with-idp/server/action.ts
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,41 @@ | ||
"use server"; | ||
|
||
import { startIDPFlow } from "@/lib/server/idp"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export type RedictToIdpState = { error?: string | null } | undefined; | ||
|
||
export async function redictToIdpAuction( | ||
prevState: RedictToIdpState, | ||
formData: FormData, | ||
): Promise<RedictToIdpState> { | ||
const params = new URLSearchParams(); | ||
|
||
const linkOnly = formData.get("linkOnly") === "true"; | ||
const authRequestId = formData.get("authRequestId") as string; | ||
const organization = formData.get("organization") as string; | ||
const idpId = formData.get("id") as string; | ||
const provider = formData.get("provider") as string; | ||
|
||
if (linkOnly) params.set("link", "true"); | ||
if (authRequestId) params.set("authRequestId", authRequestId); | ||
if (organization) params.set("organization", organization); | ||
|
||
try { | ||
const response = await startIDPFlow({ | ||
idpId, | ||
successUrl: `/idp/${provider}/success?` + params.toString(), | ||
failureUrl: `/idp/${provider}/failure?` + params.toString(), | ||
}); | ||
|
||
if (response && "error" in response && response?.error) { | ||
return { error: response.error }; | ||
} | ||
|
||
if (response && "redirect" in response && response?.redirect) { | ||
redirect(response.redirect); | ||
} | ||
} catch { | ||
return { error: "Could not start IDP flow" }; | ||
} | ||
} |
Oops, something went wrong.