Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: continue with the verification flow after registration #156

Merged
merged 1 commit into from
Dec 19, 2023
Merged
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
19 changes: 18 additions & 1 deletion examples/react-spa/src/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,24 @@ export const Registration = () => {
flow: flow.id,
updateRegistrationFlowBody: body,
})
.then(() => {
.then(({ data }) => {
if ("continue_with" in data) {
for (const cw of data.continue_with ?? []) {
if (cw.action === "show_verification_ui") {
const search = new URLSearchParams()
search.set("flow", cw.flow.id)
navigate(
{
pathname: "/verification",
search: search.toString(),
},
{ replace: true },
)
return
}
}
}

// we successfully submitted the login flow, so lets redirect to the dashboard
navigate("/", { replace: true })
})
Expand Down
16 changes: 14 additions & 2 deletions examples/react-spa/src/Verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export const Verification = (): JSX.Element => {
const [flow, setFlow] = useState<VerificationFlow | null>(null)
const [searchParams, setSearchParams] = useSearchParams()

// The return_to is a query parameter is set by you when you would like to redirect
// the user back to a specific URL after registration is successful
// In most cases it is not necessary to set a return_to if the UI business logic is
// handled by the SPA.
// In OAuth flows this value might be ignored in favor of keeping the OAuth flow
// intact between multiple flows (e.g. Login -> Recovery -> Settings -> OAuth2 Consent)
// https://www.ory.sh/docs/oauth2-oidc/identity-provider-integration-settings
const returnTo = searchParams.get("return_to")

const flowId = searchParams.get("flow")

const navigate = useNavigate()

// Get the flow based on the flowId in the URL (.e.g redirect to this page after flow initialized)
Expand All @@ -27,7 +38,9 @@ export const Verification = (): JSX.Element => {
// create a new verification flow
const createFlow = () => {
sdk
.createBrowserVerificationFlow()
.createBrowserVerificationFlow({
...(returnTo && { returnTo: returnTo }),
})
// flow contains the form fields, error messages and csrf token
.then(({ data: flow }) => {
// Update URI query params to include flow id
Expand Down Expand Up @@ -57,7 +70,6 @@ export const Verification = (): JSX.Element => {

useEffect(() => {
// it could happen that we are redirected here with an existing flow
const flowId = searchParams.get("flow")
if (flowId) {
// if the flow failed to get since it could be expired or invalid, we create a new one
getFlow(flowId).catch(createFlow)
Expand Down