Skip to content

auth.signIn() with magic link creates users in Auth.users table #1176

Closed

Description

@suilillo

Bug report

Describe the bug

Using magic link feature to login creates the user if the email provided doesn't exist on Auth.users table.

To Reproduce

  1. Use this snippet to login with magic lik

const { user, session, error } = await supabase.auth.signIn({ email: '[email protected]' })

  1. Use an email that doesn't exist on Auth.users table
  2. Check Auth.users table

Expected behavior

If the email provided is not in Auth.users table it shouldn't be created and an error should be thrown.

Activity

  1. added
    bug
    on Apr 16, 2021
  2. edgarasben commented on Apr 20, 2021

    @edgarasben

    I think this could be an option. Some apps might want a magic sign-up/log-in while others might only want a magic log-in.

  3. kiwicopple commented on Apr 20, 2021

    @kiwicopple
    Member

    If the email provided is not in Auth.users table it shouldn't be created and an error should be thrown.

    Yeah, as @edgarasben points out, there are plenty of apps that use "magic signin" - i.e. the user never creates a password. So this is expected behaviour. I'll keep this open for a few days if there are comments or suggested improvements

  4. suilillo commented on Apr 20, 2021

    @suilillo
    Author

    I would suggest to explain this in documentation to avoid confusion.

    Thanks!

  5. added
    documentation
    help wanted
    and removed
    bug
    on Apr 20, 2021
  6. edgarasben commented on May 14, 2021

    @edgarasben

    I think there could be an option here.

    If you see the way magic.link works, they have both - log in and sign up pages.

    image

    If you input email that does not exist yet, you get an error:
    image

  7. pichichi91 commented on May 17, 2021

    @pichichi91

    Just a spontanous thought - but how about defining the behaviour of the magic link based on, if signup is enabled/disabled in the backend?

    • if signup is disabled: The admin probably wants to create/invite user on their own, so when an unknown email is entered the request could be ignored
    • if signup is enabled: The admin probably wants to let users create their account on their own so when an unknown email is entered it could be created in the backend

    I think there could be an option here.
    If you input email that does not exist yet, you get an error:
    image

    This looks nice, but maybe on a security perspective, you don't want to show the message that the account couldn't be found - so attackers could somehow pinpoint which emails have already been registered?. Personally, I'd just display a message like "An email has been sent to your inbox"

  8. edgarasben commented on May 17, 2021

    @edgarasben

    Disabling the registration and inviting people manually as you say could be a nice option too. (Log in only).

    I was talking more about having some way to separate sign up and log in, while still relying on magic link tech technique. I expect some people might be confused about what's happening with signin only. Am I creating an account? Am I logging in?

    But good point about the security. You could add "If your account exists, an email will be sent to your inbox".

  9. nicholaschiang commented on May 26, 2021

    @nicholaschiang
    Contributor

    The current behavior mimics the behavior of Firebase Authentication which has both seamless sign-up (when a user doesn't exist) and login via email links. From the Firebase Authentication docs (see also this magic link login/signup guide):

    Unlike password reset and email verification, the email used does not necessarily need to belong to an existing user, as this operation can be used to sign up new users into your app via email link.

    I think that Supabase should continue to mimic that ☝️ behavior to make it easier and more intuitive for users (like me) who are migrating from Firebase to Supabase.

    I do agree with @pichichi91 that manual invites might be a common enough use-case. But I think a better solution (instead of changing the current signup/login behavior of the magic link) would be a "test mode" similar to Google OAuth2 where you have to manually specify "test user email addresses" that are allowed to login or signup at all:

    Screenshot 2021-05-26 9 57 01 AM

  10. rsdavis commented on Jun 21, 2021

    @rsdavis

    Since this is left open for opinions, I'll add mine.

    I think it's awesome that magic links has streamlined the signup/login paths into a single flow. It is also in-line with how the OAuth providers work. It's much cleaner and simpler.

    I can see how some users might be a bit perplexed, but if there is only one "sign in" page, seems like they will get it soon enough :)

    Also, I noticed that using a magic link for a new user signup will send the "confirmation" email template, while using a magic link for an existing user will send the "magic link" template. So, you can customize these email templates to help the user understand whether they have created an account or just logged in.

  11. CR1AT0RS commented on Jul 21, 2021

    @CR1AT0RS

    I think we can just add an option called "Disable SignUps" with magic link. I personally like the automatic magic link signup.

  12. oliviermtl commented on Jul 30, 2021

    @oliviermtl

    Automatic signup with magic link is nice, but it should only be available via supabase.auth.signUp()
    If there is a typo in the email entered by the user when signin in, he still receives a confirmation message, but will not receive the magic link since it has been sent to an invalid email.

  13. richcorbs commented on Sep 2, 2021

    @richcorbs

    Just a spontanous thought - but how about defining the behaviour of the magic link based on, if signup is enabled/disabled in the backend?

    I like the idea but in practice how would we ever get new users in the system (auth.users) if there wasn't a way to sign them up?

  14. 18 remaining items

  15. kristjanmar commented on Jan 26, 2022

    @kristjanmar

    I wanted to implement this but I am getting a typescript error when I try to add the "create_user" flag to my signIn function:

    "Argument of type '{ email: string; create_user: boolean; }' is not assignable to parameter of type 'UserCredentials'.
    Object literal may only specify known properties, and 'create_user' does not exist in type 'UserCredentials'.ts(2345)"

    Here's my code:

    async function signIn(email: string) {
      const { error } = await supabase.auth.signIn({ email, create_user: false })
      return { error }
    }
    
  16. kangmingtay commented on Jan 26, 2022

    @kangmingtay
    Member

    hey @kristjanmar, we haven't added it to gotrue-js yet but will be looking to do so soon!

    hope this new param helps everyone on this thread to build better magiclink login flows 💪🏻

  17. kristjanmar commented on Jan 26, 2022

    @kristjanmar

    Thanks @kangmingtay -- I managed to get rid of the type error by adding // @ts-ignore above the line.

    However, the "create_user: false" flag is not working. When I type an email that is not signed up in the login form, it creates the user in Supabase.

    I already updated to the latest version of supabase-js and tested this several times. The user is always created, just like before.

  18. kangmingtay commented on Jan 26, 2022

    @kangmingtay
    Member

    yeah this is because the gotrue version that your supabase project is currently on is not updated to the latest one yet, we'll be rolling out the updates to all existing projects soon!

  19. kristjanmar commented on Jan 26, 2022

    @kristjanmar

    That makes sense, thanks for clarifying.

  20. kristjanmar commented on Feb 11, 2022

    @kristjanmar

    I see that supabase/gotrue-js has been updated. Does this mean that the create_user: false flag should work?

  21. kristjanmar commented on Feb 16, 2022

    @kristjanmar

    yeah this is because the gotrue version that your supabase project is currently on is not updated to the latest one yet, we'll be rolling out the updates to all existing projects soon!

    Any updates on this?

  22. mirkonasato commented on Feb 20, 2022

    @mirkonasato

    Creating the user is fine in my case, but how can we tell if it's a new user and therefore the magic link has type=signup, or if it's an existing user and the link has type=magiclink?

    The email message says to click the link or "Alternatively, enter the code", so I guess I also need to show an input box for the user to manually enter the code if they so choose, calling GET /verify that requires the right type.

  23. kangmingtay commented on Feb 24, 2022

    @kangmingtay
    Member

    hey @mirkonasato,

    Creating the user is fine in my case, but how can we tell if it's a new user and therefore the magic link has type=signup, or if it's an existing user and the link has type=magiclink?

    Good point, I've created a PR to address this

    The email message says to click the link or "Alternatively, enter the code", so I guess I also need to show an input box for the user to manually enter the code if they so choose, calling GET /verify that requires the right type.

    Hmm let me check on this. We recently merged a PR that would send both the confirmation link and token to the user's email. But you can also modify the email template to only send the email link.

  24. mirkonasato commented on Feb 24, 2022

    @mirkonasato

    Thanks @kangmingtay. It is possible to modify the magic link template email so it's not a big deal. I could also call generateLink() and send the email myself. From what I can see generateLink returns an object that will have an email_confirmed_at field only if the user already exists.

  25. kristjanmar commented on Mar 4, 2022

    @kristjanmar

    This is still not working, unfortunately. The user gets added when typing an email into the login form even when the flag is activated.

    async function signIn(email: string) {
    	const { error } = await supabase.auth.signIn({
    		email,
    		shouldCreateUser: false
    	})
    	return { error }
    }

    I've tried shouldCreateUser, create_user and noSignup as the flag, since it is unclear which one it's supposed to be. But none of them work.

  26. chrisb2244 commented on Apr 4, 2022

    @chrisb2244
    Contributor

    @kristjanmar I think you need to put the shouldCreateUser in the second parameter, that is:

    const { user, error, session } = await supabase.auth.signIn({email}, {shouldCreateUser: false})
    
  27. ChronSyn commented on Apr 13, 2022

    @ChronSyn
    Contributor

    I've just been trying this with phone login, and unfortunately, when set to true, this returns the error 'signups not allowed for otp' even for an account which already exists (and preventing the login from proceeding):

    supabase.auth.signIn(
      { phone: `+${phone number for existing account}` },
      { shouldCreateUser: false },
    )

    If I set it to shouldCreateUser to true and make no other changes, the login process continues as normal. Unfortunately, this also means that accounts are still created (if they don't exist) when using signIn.

    This is when using the library "@supabase/supabase-js": "^1.33.3".

  28. kangmingtay commented on Apr 16, 2022

    @kangmingtay
    Member

    Hey @ChronSyn, thanks for reporting this with the repro steps. I'll take a look at this soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions