GithubHelp home page GithubHelp logo

rails_auth_intro's Introduction

#Authentication in Rails

##Create a new project rails new basic_auth -T

cd basic_auth

##Create a user model

We need to first start creating a user model that has a username/email field and a password_digest

rails g model user email password_digest

rake db:migrate

##Add some validations http://guides.rubyonrails.org/active_record_validations.html

  validates :email,
    presence: true,
    uniqueness: {case_sensitive: false}

##Add password encryption Add has_secure_password to your model

uncomment gem 'bcrypt' on your Gemfile

###Test out a user Now that we have has_secure_password it gives out a password setter.

###Add Validations for User

  validates_confirmation_of :password, on: :create
  validates_presence_of :password_confirmation

###Lets test a real user

User.find_by_email("[email protected]").try(:authenticate, "123")

This is nifty, but long, we can add a Class method that will return true or false

###Add a helper method to the class

  def self.authenticate email,password
    User.find_by_email(email).try(:authenticate, password)
  end

##Add the login pages Let's create a session controller to handle

touch app/controllers/sessions_controller.rb

add actions new, create, destroy

###Lets create some routes

  get "login" => "sessions#new"
  post "login" => "session#create"

Lets generate a form

  <h1>Login</h1>

  <%= form_for :user do |f| %>
    <%= f.text_field :email, placeholder: "Enter your email" %>
    <%= f.password_field :password, placeholder: "Enter your password"%>
    <%= f.submit "Login"%>
  <% end %>

###Authenticate Authenticate the user on sessions#create

  def create
    @user = User.authenticate(params[:user][:email], params[:user][:password])

    if @user
      session[:user_id] = @user.id
      flash[:success] = "User logged in!!"
      redirect_to root_path
    else
      flash[:error] = "Credentials Invalid!!"
      render :new
    end

  end

###Add current User capabilities

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def is_authenticated?
    redirect_to login_path unless current_user
  end

  def current_user
    @current_user ||= User.find_by_id(session[:user_id])
  end

end

###Protect a controller before_action :is_authenticated? on the controller you want to protect

@current_user is now visible to all pages

rails_auth_intro's People

Contributors

bridgpal avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.