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

API for session restore using magic links #923

Closed
henry2man opened this issue May 10, 2024 · 5 comments
Closed

API for session restore using magic links #923

henry2man opened this issue May 10, 2024 · 5 comments
Labels
auth This issue or pull request is related to authentication enhancement New feature or request

Comments

@henry2man
Copy link

Is your feature request related to a problem? Please describe.
I'm using Flutter Web. Supabase Auth is able to perform passwordless logins. You can setup a redirect URL. I'm able to read Supabase session from URL hash query parameters (access_token, refresh_token and so on), but I don't see any way to inject this parameters into current goTrue auth client in order to restore the session from these parameters.

Describe the solution you'd like
A clean, maybe automatic way to read URL Parameters and/or automatic session restoring during startup and/or via explicit method. Also error handling should be done.

Sample URL: http://127.0.0.1:3000/#access_token=aaa.bbbbbbbbb.ccccc&expires_at=1715302651&expires_in=3600&refresh_token=abcabcabc&token_type=bearer&type=recovery

Describe alternatives you've considered
I've tried the refreshSession(newRefreshToken) during app bootstrap but sadly it looks that a proper session is needed before.

Additional context
I'm using current latest version (2.5.2)

@henry2man henry2man added the enhancement New feature or request label May 10, 2024
@dshukertjr dshukertjr added the auth This issue or pull request is related to authentication label May 10, 2024
@grdsdev
Copy link

grdsdev commented May 10, 2024

Hi @henry2man there is the getSessionFromUrl method.

Let me know if that solves your use case.

Thanks.

@henry2man
Copy link
Author

Hi @henry2man there is the getSessionFromUrl method.

Let me know if that solves your use case.

Thanks.

I missed this one. Probably it will work, I'll confirm ASAP.

Maybe is this an opportunity to improve docs?

@dshukertjr
Copy link
Member

@henry2man The supabase_flutter SDK will obtain the session without you having to do anything when your user clicks on the magic link and opens your Flutter app. You do not need to call neither getSessionFromUrl() or the refreshSession() method.

Sample URL: http://127.0.0.1:3000/#access_token=aaa.bbbbbbbbb.ccccc&expires_at=1715302651&expires_in=3600&refresh_token=abcabcabc&token_type=bearer&type=recovery

So the sample URL that you provided uses an implicit auth flow, which is not the default auth flow for supabase_flutter. How are you generating this magic link? Are you perhaps generating it on a non-Flutter app using supabase-js, and then opening the magic link on an Flutter app or something?

Whatever the situation is, you can probably fix this by setting the auth flow to implicit like this:

  await Supabase.initialize(
    url: supabaseUrl,
    anonKey: supabaseKey,
    authOptions: const FlutterAuthClientOptions(
      authFlowType: AuthFlowType.implicit,
    ),
  );

@henry2man
Copy link
Author

My UI is incomplete. This link is generated via (local) studio UI > Authentication > User > Send Magic Link.

Thanks for these useful insights, I'll review them ASAP

@henry2man
Copy link
Author

    authOptions: const FlutterAuthClientOptions(
      authFlowType: AuthFlowType.implicit,
    ),

This is working like a charm.

there is the getSessionFromUrl method

getSessionFromURL only works if implicit AuthFlowType is pre-configured

Closing this, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth This issue or pull request is related to authentication enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants