Sha256: 5fdae6053ed365697b3060dbb90cbef3130ef89fb843c30a59973fabb5d680ce

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

class SessionsController < ApplicationController
  skip_before_action :authenticate, only: %i[ new create ]

  before_action :set_session, only: :destroy

  def index
    @sessions = Current.<%= singular_table_name %>.sessions.order(created_at: :desc)
  end

  def new
    @<%= singular_table_name %> = <%= class_name %>.new
  end

  def create
    <%= singular_table_name %> = <%= class_name %>.find_by(email: params[:email])

    if <%= singular_table_name %> && <%= singular_table_name %>.authenticate(params[:password])
      @session = <%= singular_table_name %>.sessions.create!(session_params)
      cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }

      redirect_to root_path, notice: "Signed in successfully"
    else
      redirect_to sign_in_path(email_hint: params[:email]), alert: "That email or password is incorrect"
    end
  end

  def destroy
    @session.destroy
    redirect_to sessions_path, notice: "That session has been logged out"
  end

  private
    def set_session
      @session = Current.<%= singular_table_name %>.sessions.find(params[:id])
    end

    def session_params
      { user_agent: request.user_agent, ip_address: request.remote_ip, sudo_at: Time.current }
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
authentication-zero-2.6.0 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.5.1 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.5.0 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.4.0 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt