From d9a905c9e8743a259090d5cdbe5c3235cb994a85 Mon Sep 17 00:00:00 2001 From: Miguel Michelson Date: Sat, 20 Jul 2024 19:46:24 -0400 Subject: [PATCH] registration --- .../stylesheets/application.tailwind.css | 4 ++ .../users/omniauth_callbacks_controller.rb | 2 +- .../users/registrations_controller.rb | 60 +++++++++++++++++-- app/models/user.rb | 2 +- app/views/devise/registrations/new.html.erb | 27 +++++---- config/locales/es.yml | 2 +- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css index 5146490..aedcbfb 100644 --- a/app/assets/stylesheets/application.tailwind.css +++ b/app/assets/stylesheets/application.tailwind.css @@ -248,6 +248,10 @@ @apply cursor-pointer rounded-md bg-brand-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-brand-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-600; } + .input { + @apply disabled:bg-muted; + } + .link { @apply text-brand-600 hover:text-brand-900; } diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 2d4aec9..6ed07e1 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -18,7 +18,7 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController # GET|POST /users/auth/twitter/callback def failure - # super + super end # protected diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 7b95f97..2766ea2 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -9,10 +9,48 @@ class Users::RegistrationsController < Devise::RegistrationsController # super # end - # POST /resource - # def create - # super - # end + def new + build_resource(sign_up_params) + + # Check if we have OmniAuth data in the session + if session["devise.omniauth_data"] + # Prefill email from OmniAuth data + resource.email = session["devise.omniauth_data"]["info"]["email"] + resource.username = resource.email.split("@").first.parameterize + end + + yield resource if block_given? + respond_with resource + end + + def create + build_resource(sign_up_params) + + if session["devise.omniauth_data"] + resource.email = session["devise.omniauth_data"]["info"]["email"] if resource.email.blank? + + if resource.save + # Create OauthCredential and Identity + create_oauth_credential_and_identity(resource) + + if resource.active_for_authentication? + set_flash_message! :notice, :signed_up + sign_up(resource_name, resource) + respond_with resource, location: after_sign_up_path_for(resource) + else + set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}" + expire_data_after_sign_in! + respond_with resource, location: after_inactive_sign_up_path_for(resource) + end + else + clean_up_passwords resource + set_minimum_password_length + respond_with resource + end + else + super + end + end # GET /resource/edit # def edit @@ -50,6 +88,20 @@ def configure_account_update_params devise_parameter_sanitizer.permit(:account_update, keys: [:username]) end + def create_oauth_credential_and_identity(user) + auth = session["devise.omniauth_data"] + provider = auth["provider"] + uid = auth["uid"] + + user.identities.create!(provider: provider, + uid: uid, + token: auth["credentials"]["token"], + secret: auth["credentials"]["secret"]) + + # Clear the session data + session["devise.omniauth_data"] = nil + end + # The path used after sign up. # def after_sign_up_path_for(resource) # super(resource) diff --git a/app/models/user.rb b/app/models/user.rb index 1b35850..7e3c705 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,8 +42,8 @@ class User < ApplicationRecord acts_as_liker acts_as_mentionable - normalizes :username, with: -> username { username.parameterize } + validates :username, uniqueness: { message: "Username already exists" } validates_presence_of :username, on: :update, message: "can't be blank" diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index dbd91ee..a801bdb 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -6,7 +6,7 @@

- <%= t("users.sign_in") %> + <%= t("users.register") %>

- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autofocus: true, + autocomplete: "email", + disabled: session["devise.omniauth_data"].present? %>
-
- <%= f.password_field :password, - autocomplete: "new-password", - hint: "#{@minimum_password_length} characters minimum" - %> -
-
- <%= f.password_field :password_confirmation, autocomplete: "new-password" %> -
+ <% if session["devise.omniauth_data"].blank? %> +
+ <%= f.password_field :password, + autocomplete: "new-password", + hint: "#{@minimum_password_length} characters minimum" + %> +
+ +
+ <%= f.password_field :password_confirmation, autocomplete: "new-password" %> +
+ <% end %>
<%= f.submit "Sign up" %> diff --git a/config/locales/es.yml b/config/locales/es.yml index 224d2a9..f0452aa 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -133,7 +133,7 @@ es: users: sign_in: Iniciar sesiĆ³n en tu cuenta - register: Registrar + register: Registrar nueva cuenta or: O continuar con follow: Seguir unfollow: Dejar de seguir