Skip to content

Commit

Permalink
registration
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Jul 20, 2024
1 parent 45d28cb commit d9a905c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

# GET|POST /users/auth/twitter/callback
def failure
# super
super
end

# protected
Expand Down
60 changes: 56 additions & 4 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
27 changes: 16 additions & 11 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="mt-20">

<h2 class="mt-6 text-3xl font-extrabold text-gray-900 dark:text-gray-100">
<%= t("users.sign_in") %>
<%= t("users.register") %>
</h2>
<p class="hidden mt-2 text-sm text-gray-600 dark:text-gray-400">
<%= t("users.or") %>
Expand Down Expand Up @@ -89,19 +89,24 @@
</div>
<div class="field">
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
<%= f.email_field :email, autofocus: true,
autocomplete: "email",
disabled: session["devise.omniauth_data"].present? %>
</div>

<div class="field mt-10">
<%= f.password_field :password,
autocomplete: "new-password",
hint: "#{@minimum_password_length} characters minimum"
%>
</div>

<div class="field">
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<% if session["devise.omniauth_data"].blank? %>
<div class="field mt-10">
<%= f.password_field :password,
autocomplete: "new-password",
hint: "#{@minimum_password_length} characters minimum"
%>
</div>

<div class="field">
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<% end %>

<div class="actions mt-2">
<%= f.submit "Sign up" %>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d9a905c

Please sign in to comment.