Skip to content

[Feature/Fix] Redirect users back to previous URL upon sign in #5

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

Merged
merged 4 commits into from
Mar 5, 2025

Conversation

joecampo
Copy link
Contributor

@joecampo joecampo commented Mar 4, 2025

tl;dr

  • Does not contain any breaking changes
  • Fixes inviting users from WorkOS directly (currently users would get a 403)
  • Allows user to specify a default redirect location and redirects them to the requested page

Fix

Currently the state parameter is being set to a random 20 character string upon being redirected to WorkOS for authentication. The state parameter is being used as an extra "check" to ensure the log in is valid with the ensureStateIsValid method. Per WorkOS, we don't need to use the state parameter to ensure any integrity and can use it to pass information to restore state:

Additionally, WorkOS can pass a state parameter back to your application that you may use to encode arbitrary information to restore your application state between the redirects.

Unfortunately, the current random string state breaks the invitation system:

  • If we invite a user to an app from WorkOS directly. They'll get an invitation via email from WorkOS to register.
  • Upon registering WorkOS redirects them as they have been authenticated.
  • The user gets a 403 as there is no state variable set from hitting the /login route.

This PR fixes this by not enforcing that the state variable is present. I'm building out a private application where there is no public registration and users will be invited through WorkOS.

Redirecting/Setting default route

This PR encode the previous URL upon hitting the /login route and will allow us to redirect users back. For example if an unauthenticated user goes to https://contoso.test/settings, they'll be redirected to WorkOS for auth, and then back to the settings page.

Currently the WorkOS scaffolding sets the default route to dashboard.

return tap(to_route('dashboard'), fn () => $request->authenticate());

With this PR the above still works fine, but if the user wants the scaffolding to redirect back to the previous URL they can update their login route to while also setting a default location:

return tap($request->redirect(default: '/dashboard'), fn () => $request->authenticate());

@taylorotwell taylorotwell merged commit a1586b4 into laravel:main Mar 5, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants