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

Pass back the 'return_to' url when errors are thrown so that we can redirect with the return_to to get a new flow #2275

Closed
4 of 6 tasks
MilesNorton opened this issue Mar 2, 2022 · 2 comments
Labels
feat New feature or request.
Milestone

Comments

@MilesNorton
Copy link

MilesNorton commented Mar 2, 2022

Preflight checklist

Describe your problem

When using Nextjs (react) to get or submit self service browser flows and you receive an error, it would be nice for the error message to return the redirect_to with the original return_to in the url.

So initially when I called the page I passed in the return_to and then I reload the page with the Flow Id which renders everything. But if that flow id were to expire say, it would spit out an error and we would then need to call 'http://localhost:4433/self-service/login/browse to initialize a new flow, at this point we don't have any notion of the return_to that was originally used.

Note, we use the return_to to render the page differently for different applications.

Here is an example of the error message return with the self_service_flow_expired error:

{
  error: {
    id: 'self_service_flow_expired',
    code: 410,
    status: 'Gone',
    reason: 'The login flow has expired. Redirect the user to the login flow init endpoint to initialize a new login flow.',
    details: { redirect_to: 'http://localhost:4433/self-service/login/browser' },
    message: 'The requested resource is no longer available because it has expired or is otherwise invalid.'
  }
}

Below is an example of the login screen, notice where I've commented SOLVE: which is what I'm having an issue with;

type LoginProps = {
	flow: SelfServiceLoginFlow;
};

const Login: React.FC<LoginProps> = ({ flow }) => {
	const submitFlow = async (flowId: string, values: SubmitSelfServiceLoginFlowBody) => {
		await ory.submitSelfServiceLoginFlow(flowId, undefined, values)
            .catch(handleRedirectFlowError({
			    redirectCallback: clientRedirectHelper,
			    route: "login",
			    return_to: flow.return_to
            }))
            .then(() => router.push(flow.return_to || "/"));
	};

	return (
		<MasterLayout returnTo={flow.return_to}>
			<LoginFormFlow flow={flow} submitFlow={submitFlow} />
		</MasterLayout>
	);
};


const getLoginServerSideProps: GetServerSideProps<LoginProps | any> = async ({ query, req }) => {
	if (!query?.flow) {
		return {
			redirect: {
				destination:
					"/api/.ory/self-service/login/browser" +
					"?" +
					new URLSearchParams(query as Record<string, string>).toString(), // passes in the query parameters if there are any
				statusCode: 302,
			},
		};
	}

	const result = await ory.getSelfServiceLoginFlow(query?.flow as string, req?.headers["cookie"]).catch(
		handleRedirectFlowError({
			redirectCallback: serverRedirectHelper,
			route: "login",
			// return_to: , SOLVE: there is no way of getting the return to at this point
		}),
	);

	if (result && "data" in result) {
		return {
			props: {
				flow: result.data,
			},
		};
	} else {
		return result as { redirect: Redirect };
	}
};

export const getServerSideProps = withNoAuthentication(getLoginServerSideProps);

export default Login;

Describe your ideal solution

It would be nice to have the error object return the redirect_url with the initial request_url that was used to create the flow.
Otherwise another endpoint to get the configuration of the flow regardless of errors.

Workarounds or alternatives

Currently, I will be having my own database that will log the flow configuration. Upon an error I will request from this database to get the config and find the return_to url

Version

oryd/kratos:v0.8.2

Additional Context

No response

@MilesNorton
Copy link
Author

MilesNorton commented Mar 4, 2022

The workaround in this ticket also helped: #2285

@aeneasr aeneasr added this to the Stable Release milestone Mar 7, 2022
@spietras
Copy link

This would also enable us to redirect to the requested URL if the user is already logged in (because requesting a new flow returns an error in that case).

harnash pushed a commit to Wikia/kratos that referenced this issue Mar 28, 2022
Closes ory#2275
Closes ory#2279
Closes ory#2285

Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
peturgeorgievv pushed a commit to senteca/kratos-fork that referenced this issue Jun 30, 2023
Closes ory#2275
Closes ory#2279
Closes ory#2285

Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request.
Projects
None yet
Development

No branches or pull requests

3 participants