# Omitir el modal de creación de cuenta al usar OAuth2

**URL:** https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123
**Category:** Support
**Created:** [26 Febrero, 2019 13:23 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123 "2019-02-26T13:23:56Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [26 Febrero, 2019 13:23 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/1 "2019-02-26T13:23:56Z")

</div>

When logging in via OAuth2 (custom server) for the first time, the user is welcomed by this account creation modal.

 ![41](https://global.discourse-cdn.com/meta/original/3X/b/e/be914c2d326c158dde2462001d8d879ad72839db.png)

Why popping it if all the infos are valid? Why not create the account automatically?

**Is there any way to bypass it?**

In my case, it adds confusion to the user because he’s supposed to have a single account (the OAuth2 one) for several apps. Nevertheless, I think it it should pop if the infos are invalid (conflict or such).

The other solution for me would be to create the user via the API beforehand (when its OAuth2 account is created), then the window doesn’t appear at first login, but it forces me to _enable local logins_ and I’d prefer not to.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [26 Febrero, 2019 13:57 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/2 "2019-02-26T13:57:40Z")

</div>

You really want SSO. That way the other server controls everything. With oauth the user can still choose a username.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [26 Febrero, 2019 14:22 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/3 "2019-02-26T14:22:19Z")

</div>

Take a look at this [https://meta.discourse.org/t/disable-create-account-screen-for-cas-logins/11334/4](https://meta.discourse.org/t/disable-create-account-screen-for-cas-logins/11334/4) . You should be able to do something similar. I have no idea why this is not a default feature.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [26 Febrero, 2019 14:28 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/4 "2019-02-26T14:28:24Z")

</div>

> [@ryancey](#):
>
> create the account automatically

We do exactly this in the official SAML plugin.

> [@RGJ](#):
>
> I have no idea why this is not a default feature.

The reason we show the modal is so that people can change their Name/Username if the “Social login provider” doesn’t have the best information available. For things like facebook/twitter this makes a lot of sense, but it makes less sense when corporate SAML/OAuth/OIDC is being used.

I think a core site setting for “Automatically create accounts” would be useful. But we would need to think about whether it should work when registration is disabled.

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [1 Marzo, 2019 10:40 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/5 "2019-03-01T10:40:57Z")

</div>

> [@RGJ](#):
>
> Take a look at this [Disable “create account” screen for CAS logins](https://meta.discourse.org/t/disable-create-account-screen-for-cas-logins/11334/4) . You should be able to do something similar. I have no idea why this is not a default feature.

I modified `discourse-oauth2-plugin` to create the user account in the `after_authenticate` hook but the modal still pops and the suggested username is a variation of the OAuth2 username (because, eh, it’s already taken at this point).

I’m not familiar enough with Discourse (and Rails) to dig this, so if anyone could tell me if there is an “easy” way to dismiss this modal, other than forking `discourse/discourse` if possible…

> [@pfaffman](#):
>
> You really want SSO. That way the other server controls everything. With oauth the user can still choose a username.

SSO would make me implement another auth endpoint on my OAuth2 server and I’d prefer not to, to keep consistency with the other apps using OAuth2 in the stack.

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [1 Marzo, 2019 11:41 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/6 "2019-03-01T11:41:32Z")

</div>

The ugly workaround I found for the time being:

- Enable local logins

- Create users beforehand via the API

- Create a new Component in the theme

In a nutshell, I allow local logins but hides the sign up button, and when the log in modal shows (wether it’s coming from a click on the log in button, the reply button…) the modal is hidden and it simulates a click on the OAuth2 login method.

You can throw stones at me until I figure out a cleaner, lower-level way to do this 🙂

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [1 Marzo, 2019 11:44 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/7 "2019-03-01T11:44:12Z")

</div>

> [@ryancey](#):
>
> I modified `discourse-oauth2-plugin` to create the user account in the `after_authenticate` hook but the modal still pops

Would you mind sharing your code?

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [1 Marzo, 2019 11:46 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/8 "2019-03-01T11:46:48Z")

</div>

Added code is between the comments (taken from [https://meta.discourse.org/t/disable-create-account-screen-for-cas-logins/11334/6?u=ryancey](https://meta.discourse.org/t/disable-create-account-screen-for-cas-logins/11334/6)).

```ruby
  def after_authenticate(auth)
    log("after_authenticate response: \n\ncreds: #{auth['credentials'].to_hash}\ninfo: #{auth['info'].to_hash}\nextra: #{auth['extra'].to_hash}")

    result = Auth::Result.new
    token = auth['credentials']['token']
    user_details = fetch_user_details(token, auth['info'][:id])

    result.name = user_details[:name]
    result.username = user_details[:username]
    result.email = user_details[:email]
    result.email_valid = result.email.present? && SiteSetting.oauth2_email_verified?
    avatar_url = user_details[:avatar]

    current_info = ::PluginStore.get("oauth2_basic", "oauth2_basic_user_#{user_details[:user_id]}")
    if current_info
      result.user = User.where(id: current_info[:user_id]).first
    elsif SiteSetting.oauth2_email_verified?
      result.user = User.find_by_email(result.email)
      if result.user && user_details[:user_id]
        ::PluginStore.set("oauth2_basic", "oauth2_basic_user_#{user_details[:user_id]}", user_id: result.user.id)
      end
    end

    download_avatar(result.user, avatar_url)

    result.extra_data = { oauth2_basic_user_id: user_details[:user_id], avatar_url: avatar_url }

    # --------------
    if User.find_by_email(user_details[:email]).nil?
      user = User.create(name: user_details[:name], email: user_details[:email], username: user_details[:username])
      log("created user account")
    end
    # --------------

    result
  end

```

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [1 Marzo, 2019 11:55 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/9 "2019-03-01T11:55:04Z")

</div>

Try moving that code directly underneath

```plaintext
elsif SiteSetting.oauth2_email_verified?

```

and change `user` to `result.user`

```plaintext
  def after_authenticate(auth)
    log("after_authenticate response: \n\ncreds: #{auth['credentials'].to_hash}\ninfo: #{auth['info'].to_hash}\nextra: #{auth['extra'].to_hash}")

    result = Auth::Result.new
    token = auth['credentials']['token']
    user_details = fetch_user_details(token, auth['info'][:id])

    result.name = user_details[:name]
    result.username = user_details[:username]
    result.email = user_details[:email]
    result.email_valid = result.email.present? && SiteSetting.oauth2_email_verified?
    avatar_url = user_details[:avatar]

    current_info = ::PluginStore.get("oauth2_basic", "oauth2_basic_user_#{user_details[:user_id]}")
    if current_info
      result.user = User.where(id: current_info[:user_id]).first
    elsif SiteSetting.oauth2_email_verified?
      # --------------
      if User.find_by_email(user_details[:email]).nil?
        result.user = User.create(name: user_details[:name], email: user_details[:email], username: user_details[:username])
        log("created user account")
      end
      # --------------

      result.user = User.find_by_email(result.email)
      if result.user && user_details[:user_id]
        ::PluginStore.set("oauth2_basic", "oauth2_basic_user_#{user_details[:user_id]}", user_id: result.user.id)
      end
    end

    download_avatar(result.user, avatar_url)

    result.extra_data = { oauth2_basic_user_id: user_details[:user_id], avatar_url: avatar_url }

    result
  end

```

Note that modifying code in this way isn’t great, because it will break if we update the method later. This will almost certainly require maintenance in the future.

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [1 Marzo, 2019 13:41 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/10 "2019-03-01T13:41:36Z")

</div>

Thanks @david it works with your version.

Nevertheless, I’m now welcomed by this modal (button label redacted)

 ![29](https://global.discourse-cdn.com/meta/original/3X/e/1/e1f898598859944a85f620e5a80c22a4042f6d22.png)

`oauth2 email verified` setting is enabled.  
Clicking again logs me in, but this is still an extra step for the user.

> [@david](#):
>
> Note that modifying code in this way isn’t great, because it will break if we update the method later. This will almost certainly require maintenance in the future.

This method is in my `discourse-oauth2-plugin` fork, shouldn’t it stay stable as long as I don’t update the major version?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [1 Marzo, 2019 13:55 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/11 "2019-03-01T13:55:15Z")

</div>

> [@ryancey](#):
>
> I’m now welcomed by this modal

Update your `User.create` line to include `active: true`

> [@ryancey](#):
>
> This method is in my `discourse-oauth2-plugin` fork, shouldn’t it stay stable

In theory, yes

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [1 Marzo, 2019 15:27 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/12 "2019-03-01T15:27:39Z")

</div>

> [@david](#):
>
> Update your `User.create` line to include `active: true`

Err I could have guessed it (and I actually do it with the API client), thanks 🙂

---

<div class="post-metadata">

### Author: ![Akrem\_Abayed](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/akrem_abayed/32/244459_2.png) [@Akrem\_Abayed](https://meta.discourse.org/u/Akrem_Abayed)
#### Post date: [16 Marzo, 2022 13:56 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/13 "2022-03-16T13:56:24Z")

</div>

¿Hay una solución actualizada para manejar este problema?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [16 Marzo, 2022 13:57 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/14 "2022-03-16T13:57:27Z")

</div>

Sí, ahora tenemos una configuración de ‘omitir creación de confirmación de autenticación’

 ![Screenshot 2022-03-16 at 13.57.15](https://global.discourse-cdn.com/meta/original/3X/d/9/d93d7b172abe2a25ed971eb6b3144e5681f27a9c.png)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [23 Marzo, 2022 08:00 UTC](https://meta.discourse.org/t/bypass-the-account-creation-modal-when-using-oauth2/110123/15 "2022-03-23T08:00:52Z")

</div>

Este tema se cerró automáticamente después de 20 horas. Ya no se permiten nuevas respuestas.
