Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
add Registrations (sign ups)
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Jul 21, 2024
1 parent b94b002 commit ca6f270
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
24 changes: 24 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RegistrationsController < ApplicationController
allow_unauthenticated_access(only: %i[new create])
before_action :resume_session, only: [:new]

Check failure on line 3 in app/controllers/registrations_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 3 in app/controllers/registrations_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }

def new
redirect_to root_url if authenticated?
end

def create
user = User.new(params.permit(:email_address, :password))
if user.save
start_new_session_for user
redirect_to after_authentication_url, notice: "Signed up."
else
redirect_to new_session_url, alert: user.errors.full_messages.to_sentence
end
end

def destroy
terminate_session
redirect_to new_session_url, notice: "Signed out."
end
end
1 change: 1 addition & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SessionsController < ApplicationController
allow_unauthenticated_access(only: %i[new create])
before_action :resume_session, only: [:new]

Check failure on line 3 in app/controllers/sessions_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 3 in app/controllers/sessions_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }

def new
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ApplicationRecord
has_secure_password validations: false
has_many :sessions, dependent: :destroy

validates :email_address, uniqueness: true, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
end
11 changes: 9 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
<%#= Current.session %>
<%= Current.user.email_address %>
<%= button_to 'Sign out', session_path, method: :delete %>
<% else %>
<%= link_to 'Sign in', new_session_path %>
<%= link_to 'Sign up', new_registration_path %>
<% end %>
<hr>
<%= notice %>
<%= alert %>
<% if notice = flash[:notice] %>
<div style="color:green"><%= notice %></div>
<% end %>
<% if alert = flash[:alert] %>
<div style="color:red"><%= alert %></div>
<% end %>
<hr>
<%= yield %>
</body>
Expand Down
13 changes: 13 additions & 0 deletions app/views/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Create an account</h1>

<%= form_with url: registration_url do |form| %>
<div>
<%= form.email_field :email_address, required: true, autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email_address] %>
</div>

<div>
<%= form.password_field :password, required: true, autocomplete: "current-password", placeholder: "Enter your password", maxlength: 72 %>
</div>

<%= form.submit "Sign up" %>
<% end %>
4 changes: 1 addition & 3 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<% if alert = flash[:alert] %>
<div style="color:red"><%= alert %></div>
<% end %>
<h1>Sign in</h1>

<%= form_with url: session_url do |form| %>
<div>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
resource :session
resource :session, only: [:new, :create, :destroy]

Check failure on line 2 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 2 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
resource :registration, only: [:new, :create]
get "dashboard", to: "home#dashboard"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down

0 comments on commit ca6f270

Please sign in to comment.