Skip to content

Commit

Permalink
Merge pull request #286 from kortirso/issue_285
Browse files Browse the repository at this point in the history
IS-285 added create users with only username
  • Loading branch information
kortirso authored Mar 29, 2024
2 parents a16af28 + 801f275 commit 9ad7d01
Show file tree
Hide file tree
Showing 87 changed files with 394 additions and 288 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- create users with only username

## [1.2.4] - 2024-03-28
### Added
- best_of elimination kind for cups pairs
Expand Down
21 changes: 17 additions & 4 deletions app/contracts/users/create_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ class CreateContract < ApplicationContract
config.messages.namespace = :user

params do
required(:email).filled(:string)
optional(:email)
optional(:username)
required(:password).filled(:string)
required(:password_confirmation).filled(:string)
end

rule(:email, :username) do
key(:login).failure(:blank) if values[:email].blank? && values[:username].blank?
end

rule(:email) do
unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
key.failure(:invalid)
if value
unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
key.failure(:invalid)
end

key.failure(I18n.t('dry_validation.errors.user.banned')) if BannedEmail.exists?(value: value)
end
end

key.failure(I18n.t('dry_validation.errors.user.banned')) if BannedEmail.exists?(value: value)
rule(:username) do
if value && !/[\w+\-\_]+/i.match?(value)
key.failure(:invalid)
end
end

rule(:password, :password_confirmation) do
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/api/v1/users/access_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module V1
module Users
class AccessTokensController < Api::V1Controller
skip_before_action :authenticate, only: %i[create]
skip_before_action :check_email_confirmation, only: %i[create]
skip_before_action :check_email_ban, only: %i[create]

before_action :find_user, only: %i[create]
Expand All @@ -24,7 +23,16 @@ def create
private

def find_user
@user = User.not_banned.find_by!(email: user_params[:email]&.strip&.downcase)
@user = find_by_email || find_by_username
page_not_found unless @user
end

def find_by_email
User.not_banned.find_by(email: user_params[:login]&.strip&.downcase)
end

def find_by_username
User.not_banned.find_by(username: user_params[:login])
end

def authenticate_user
Expand All @@ -38,7 +46,7 @@ def failed_sign_in
end

def user_params
params.require(:user).permit(:email, :password)
params.require(:user).permit(:login, :password)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/v1/users/me_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Api
module V1
module Users
class MeController < Api::V1Controller
skip_before_action :check_email_confirmation, only: %i[index]
skip_before_action :check_email_ban, only: %i[index]

SERIALIZER_FIELDS = %w[confirmed banned].freeze
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class UsersController < Api::V1Controller
SERIALIZER_FIELDS = %w[confirmed banned access_token].freeze

skip_before_action :authenticate, only: %i[create]
skip_before_action :check_email_confirmation, only: %i[create destroy]
skip_before_action :check_email_ban, only: %i[create destroy]

def create
# commento: users.email, users.username, users.password
case create_form.call(params: user_params.to_h.symbolize_keys)
in { errors: errors } then render json: { errors: errors }, status: :unprocessable_entity
in { result: result }
Expand Down Expand Up @@ -47,7 +47,7 @@ def destroy
private

def user_params
params_hash = params.require(:user).permit(:email, :password, :password_confirmation)
params_hash = params.require(:user).permit(:email, :username, :password, :password_confirmation)
params_hash[:email] = params_hash[:email].strip.downcase
params_hash
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/api/v1_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ def authentication_error
render json: { errors: [t('controllers.authentication.permission')] }, status: :unauthorized
end

def confirmation_error
render json: { errors: [t('controllers.confirmation.permission')] }, status: :forbidden
end

def ban_error
render json: { errors: [t('controllers.confirmation.ban')] }, status: :forbidden
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ApplicationController < ActionController::Base
# TODO: remember to skip redundant before actions in
# Api::Frontend::BaseController, Admin::BaseController, Api::V1::BaseController
before_action :authenticate, except: %i[page_not_found]
before_action :check_email_confirmation, except: %i[page_not_found]
before_action :check_email_ban, except: %i[page_not_found]

rescue_from ActiveRecord::RecordNotFound, with: :page_not_found
Expand Down
10 changes: 0 additions & 10 deletions app/controllers/concerns/confirmation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ module Confirmation

private

def check_email_confirmation
return if Current.user.nil? || Current.user.confirmed?

confirmation_error
end

def check_email_ban
return if Current.user.nil? || !Current.user.banned?

ban_error
end

def confirmation_error
redirect_to users_confirm_path, alert: t('controllers.confirmation.permission')
end

def ban_error
redirect_to root_path, alert: t('controllers.confirmation.ban')
end
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/fantasy_leagues/joins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ class JoinsController < ApplicationController
include Deps[join_fantasy_league: 'services.persisters.fantasy_teams.join_fantasy_league']

skip_before_action :authenticate
skip_before_action :check_email_confirmation
skip_before_action :check_email_ban
before_action :remember_invite_code
before_action :authenticate
before_action :check_email_confirmation
before_action :check_email_ban
before_action :find_fantasy_league
before_action :validate_invite_code
Expand Down
1 change: 0 additions & 1 deletion app/controllers/users/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class ConfirmationsController < ApplicationController
include Deps[update_service: 'services.persisters.users.update']

skip_before_action :authenticate
skip_before_action :check_email_confirmation
before_action :find_user
before_action :check_confirmation_token

Expand Down
1 change: 0 additions & 1 deletion app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class OmniauthCallbacksController < ApplicationController

skip_before_action :verify_authenticity_token
skip_before_action :authenticate, only: %i[create]
skip_before_action :check_email_confirmation, only: %i[create]
skip_before_action :check_email_ban, only: %i[create]
before_action :validate_provider, only: %i[create]
before_action :validate_auth, only: %i[create]
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ class RegistrationsController < ApplicationController
]

skip_before_action :authenticate
skip_before_action :check_email_confirmation
before_action :check_recaptcha, only: %i[create]

def new
@user = User.new
end

def new_username
@user = User.new
end

def create
# commento: users.email, users.username, users.password, users.password_confirmation
case create_form.call(params: user_params.to_h.symbolize_keys)
in { errors: errors } then failed_create_response(errors)
in { result: result } then success_create_response(result)
Expand Down Expand Up @@ -44,12 +48,12 @@ def failed_create_response(errors)
end

def after_registration_path
users_confirm_path
draft_players_path
end

def user_params
params_hash = params.require(:user).permit(:email, :password, :password_confirmation)
params_hash[:email] = params_hash[:email].strip.downcase
params_hash = params.require(:user).permit(:email, :username, :password, :password_confirmation)
params_hash[:email] = params_hash[:email].strip.downcase if params_hash[:email].present?
params_hash
end
end
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ class SessionsController < ApplicationController
include Deps[generate_token: 'services.auth.generate_token']

skip_before_action :authenticate
skip_before_action :check_email_confirmation
skip_before_action :check_email_ban
before_action :find_user, only: %i[create]
before_action :authenticate_user, only: %i[create]
before_action :check_email_confirmation, only: %i[create]

def new; end

Expand All @@ -30,22 +28,24 @@ def destroy
private

def find_user
@user = User.not_banned.find_by(email: user_params[:email]&.strip&.downcase)
@user = find_by_email || find_by_username
return if @user.present?

failed_sign_in
end

def authenticate_user
return if @user.authenticate(user_params[:password])
def find_by_email
User.not_banned.find_by(email: user_params[:login]&.strip&.downcase)
end

failed_sign_in
def find_by_username
User.not_banned.find_by(username: user_params[:login])
end

def check_email_confirmation
return if @user.confirmed?
def authenticate_user
return if @user.authenticate(user_params[:password])

redirect_to users_confirm_path
failed_sign_in
end

def failed_sign_in
Expand All @@ -61,7 +61,7 @@ def after_logout_path
end

def user_params
params.require(:user).permit(:email, :password)
params.require(:user).permit(:login, :password)
end
end
end
1 change: 0 additions & 1 deletion app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class WelcomeController < ApplicationController
skip_before_action :authenticate
skip_before_action :check_email_confirmation
skip_before_action :check_email_ban

def index; end
Expand Down
5 changes: 2 additions & 3 deletions app/forms/users/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ def call(params:, with_send_confirmation: true)
error = validate_user(user)
return { errors: [error] } if error.present?

# commento: users.email, users.password, users.password_confirmation
send_email_confirmation(user) if user.save && with_send_confirmation
send_email_confirmation(user) if user.save && user.email.present? && with_send_confirmation

{ result: user }
rescue ActiveRecord::RecordNotUnique
{ errors: [I18n.t('services.users.create.email_exists')] }
{ errors: [I18n.t('services.users.create.not_unique')] }
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/views/controllers/users/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<%= t('views.users.registrations.new.have_account') %>
<%= link_to t('components.page_wrappers.users_component.login'), users_login_path, class: 'underline text-blue-600' %>
</div>
<div class="mb-2 flex items-center">
<span><%= t('views.users.registrations.new.no_email') %><span>
<%= link_to t('components.page_wrappers.users_component.sign_up'), users_sign_up_name_path, class: 'underline text-blue-600' %>
</div>
<div class="form-field">
<%= recaptcha_tags %>
</div>
Expand Down
34 changes: 34 additions & 0 deletions app/views/controllers/users/registrations/new_username.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%= render PageWrappers::GuestComponent.new do %>
<div class="flex-1 flex justify-center items-center">
<%= form_with model: @user, url: users_sign_up_path, method: :post, class: 'p-4 bg-white rounded border border-stone-300' do |form| %>
<h2><%= t('views.users.registrations.new.sign_up') %></h2>
<div class="form-field">
<%= form.label :username, 'Username', class: 'form-label' %>
<%= form.text_field :username, class: 'form-value w-full' %>
</div>
<div class="form-field">
<%= form.label :password, t('views.users.registrations.new.password', value: Rails.application.config.minimum_password_length), class: 'form-label' %>
<%= form.password_field :password, class: 'form-value w-full' %>
</div>
<div class="form-field">
<%= form.label :password_confirmation, t('views.users.registrations.new.password_confirmation'), class: 'form-label' %>
<%= form.password_field :password_confirmation, class: 'form-value w-full' %>
</div>
<div class="mb-2 flex flex-row">
<%= link_to image_tag('google.svg', width: 20, height: 20, alt: 'google'), omniauth_link(:google), 'aria-label': 'Login with Google', class: 'flex justify-center mr-4' %>
</div>
<div class="mb-2">
<%= t('views.users.registrations.new.have_account') %>
<%= link_to t('components.page_wrappers.users_component.login'), users_login_path, class: 'underline text-blue-600' %>
</div>
<div class="mb-2 flex items-center">
<span><%= t('views.users.registrations.new.with_email') %><span>
<%= link_to t('components.page_wrappers.users_component.sign_up'), users_sign_up_path, class: 'underline text-blue-600' %>
</div>
<div class="form-field">
<%= recaptcha_tags %>
</div>
<%= form.submit t('views.users.registrations.new.create'), class: 'btn-primary' %>
<% end %>
</div>
<% end %>
4 changes: 2 additions & 2 deletions app/views/controllers/users/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<%= form_with model: User.new, url: users_login_path, method: :post, class: 'p-4 bg-white rounded border border-stone-300' do |form| %>
<h2><%= t('views.users.sessions.new.login') %></h2>
<div class="form-field">
<%= form.label :email, 'Email', class: 'form-label' %>
<%= form.text_field :email, class: 'form-value' %>
<%= form.label :login, t('views.users.sessions.new.email_username'), class: 'form-label' %>
<%= form.text_field :login, class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :password, t('views.users.sessions.new.password'), class: 'form-label' %>
Expand Down
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ en:
record_is_not_exists: Week is not exist
users:
create:
email_exists: Email is already used
not_unique: Credentials is already used
invalid: Credentials are invalid
seasons:
create:
Expand Down Expand Up @@ -411,13 +411,16 @@ en:
password_confirmation: Password confirmation
create: Sign up
have_account: Already have account?
no_email: No email?
with_email: Already have email?
confirm:
title: Thank you for registration
description: You need to check your mailbox to verify your email and gaining access to Fantasy Sports
sessions:
new:
login: Login
sign_in: Sign in
email_username: Email or Username
password: Password
no_account: Don't have account?
forgot: Forgot password?
Expand Down
5 changes: 5 additions & 0 deletions config/locales/errors.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ en:
status: Status
notification_type: Notification type
required_wins: Required wins
login: Email or Username
errors:
cups_pair:
rules:
Expand All @@ -34,6 +35,10 @@ en:
filled?: can't be blank
passwords:
different: must be equal
login:
blank: must be present
username:
invalid: has invalid format
fantasy_team:
rules:
name:
Expand Down
Loading

0 comments on commit 9ad7d01

Please sign in to comment.