Basic sign up and sign in logics in Rails

Configured postgres database and added bcrypt for password secure:

gem 'pg'
gem 'bcrypt-ruby'
gem 'bcrypt'

Run the following command to install the gems:

bundle install

Create a user table migration with the respective columns and their type:

bundle exec rails g model user name:string email:string password_digest:string auth_token:string

Run the following command to migrate to database:

bundle exec rake db:migrate

Create a controller users and add a new and create action for sign up action

bundle exec rails g controller users

Create routes for the users by adding following in config/routes.rb

resources :users

Add a new action and create a object in the users controller

def new
	@user = User.new
end

Create a new folder users under apps/views & add a new.html.erb template for form

<div class="container">
  <div class="sign_up" style="margin-left: 200px;margin-top: 100px;">
    <h3>Sign up form</h3>
      <%= form_for @user, :url => users_path, :method => "POST" do |f| %>
	     <%= f.label "Name"%>
       <%= f.text_field :name, :class =>"form-control", :required => :true %>
       <br>
       <%= f.label "Email"%>
       <%= f.text_field :email, :class =>"form-control", :required => :true %>
       <br>
       <%= f.label "Password"%>
       <%= f.password_field :password, :class =>"form-control", :required => :true %>
       <br>
       <%= f.label "Confirm Password"%>
       <%= f.password_field :password_confirmation, :class =>"form-control", :required => :true %>
       <br>
       <%= f.submit "Sign Up", :class=>"btn btn-primary"  %>
      <% end %>
  </div>
</div>

Add a bcrypt password secure method in User model

has_secure_password

Create a server side validation for User columns in User model.

validates :name, presence: true, :length =>{ :minimum => 2, :maximum => 16 }
validates :email, :presence => true, :length => {:minimum => 3, :maximum => 254}
validates_uniqueness_of :email
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates :password, presence: true, :length =>{ :minimum => 6, :maximum => 16 }
validates :password_confirmation, presence: true, confirmation: true

Added a create action to users controller

def create
	@user = User.new(user_params)
	if @user.valid?
		@user.save!
		flash[:success] = "User signed up succssfully!"
		session[:user_id] = @user.id
		redirect_to users_path
	else
		flash[:error] = @user.errors.full_messages
		render :new
	end
end

Added a filter for index action in users controller

before_filter :require_session, :only => [:index]

Added filter action in application_contoller.rb

def require_session
  	if session[:user_id].present?
  	  @current_user = User.find(session[:user_id])
  	else
  		flash[:error] = "Need to login"
  		redirect_to new_users_path
  	end
 end

Create a session controller for sign in

Create a session routes only for new, create & destroy in config/routes.rb

resources :sessions, :only => [:new, :create, :destroy]

Added a new, create & destroy action in sessions controller

Create a sign in form in sessions new template app/views/sessions/new.html.erb

<div class="container">
	<div class="sign_up" style="margin-left: 200px;margin-top: 100px;">
    <h3>Sign up form</h3>
    <%= form_for :user, url: '/sessions' do |f| %>
      <br>
      <%= f.label "Email"%>
      <%= f.text_field :email, :class =>"form-control", :required => :true %>
      <br>
      <%= f.label "Password"%>
      <%= f.password_field :password, :class =>"form-control", :required => :true %>
      <br>
      <%= f.submit "Sign In", :class=>"btn btn-primary"  %>&nbsp&nbsp&nbsp&nbsp
      <%= link_to "Sign Up", new_user_path, :class=>"btn btn-primary"  %>
    <% end %>
  </div>
</div>

Make the sign in path as the root

root :to => "sessions#new"

Write a session create logic inside a sessions/create action

def create
    user = User.find_by_email(user_params[:email])
  	if user && user.authenticate(user_params[:password])
      session[:user_id] = user.id
      redirect_to users_path, :notice => "Logged in successfully"
    else
      flash.now[:alert] = "Invalid login/password combination"
      render :action => 'new'
    end
end

Write a logout logic in sessions/destroy action

def destroy
   reset_session
   redirect_to root_path, notice: 'Logged out'
end

The main logics are

Sign In logics are in sessions controller and views

Sign Up logics are in users controller and views

Filter in application controller

#Contributor

Facebook

Twitter

Linkedin

About Me