How to Trigger a Custom Redirect URL on Successful Login Using Custom OAuth Provider?

Hi,
cc: @revant_one

I’m using a custom OAuth provider for user authentication in my Frappe application. I want to set up a custom redirect URL that gets triggered immediately after a successful login.

I found that in the get_context method of login.py, the redirect-to parameter is fetched from frappe.local.request.args, but it’s None, so the redirect URL in the callback state is passed as null.

Here’s the relevant code snippet for reference:

def get_context(context):
	redirect_to = frappe.local.request.args.get("redirect-to")
	redirect_to = sanitize_redirect(redirect_to)

	if frappe.session.user != "Guest":
		if not redirect_to:
			if frappe.session.data.user_type == "Website User":
				redirect_to = get_home_page()
			else:
				redirect_to = "/app"

		if redirect_to != "login":
			frappe.local.flags.redirect_location = redirect_to
			raise frappe.Redirect

How can I ensure that the redirect-to parameter is correctly set, or is there an alternative way to trigger the custom redirect URL upon successful login?

Any insights or suggestions would be greatly appreciated!

Frappe Framework Version 15

Please tag anyone who can help on this.

Thank You.

Are you using frappe as OAuth2 provider? OAuth Client doctype. In this case use the state param to pass the data and get it back after login.

Or are you using third party provider and added Social Login Key entry in frappe? In this case visiting the link /login?redirect-to=https:%2F%2Ffrappe.io will redirect you to https://frappe.io after successful social login.

1 Like

Thank you for the response. :pray:

We are using a third party provider and added it as an entry in Social Login Key dt.
We managed to learn that passing the redirect-to query param in the url will redirect the user to the desired page.

Most the times the users would use just the hostname to visit the site
e.g. hostname.com
Hence wondering if there is a means to auto set the redirect-to query param or some other means to set the custom redirect url

post login the following method is triggered to redirect users either to /app/* or /me

def redirect_post_login(desk_user: bool, redirect_to: str | None = None, provider: str | None = None):
	frappe.local.response["type"] = "redirect"

	if not redirect_to:
		desk_uri = "/app/workspace" if provider == "facebook" else "/app"
		redirect_to = frappe.utils.get_url(desk_uri if desk_user else "/me")

	frappe.local.response["location"] = redirect_to

location: frappe/frappe/utils/oauth.py

We resolved this issue by using Redirects options in Website Settings. Attached the screenshot for reference.