Sha256: 268a1c4b64eac388cbf15f9258afcf310373bda8e6d1d36f655f2278696e639d

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

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

  before_action :set_session, only: :destroy

  def index
    @sessions = Current.user.sessions.order(created_at: :desc)
  end

  def new
    @user = User.new
  end

  def create
    user = User.find_by(email: params[:email])

    if user && user.authenticate(params[:password])
      <%- if two_factor? -%>
      if user.otp_required_for_sign_in?
        session[:challenge_token] = user.signed_id(purpose: :authentication_challenge, expires_in: 20.minutes)
        redirect_to new_two_factor_authentication_challenge_totp_path
      else
        @session = user.sessions.create!
        cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }

        redirect_to root_path, notice: "Signed in successfully"
      end
      <%- else -%>
      @session = user.sessions.create!
      cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }

      redirect_to root_path, notice: "Signed in successfully"
      <%- end -%>
    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.user.sessions.find(params[:id])
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
authentication-zero-2.16.29 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.16.28 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.16.27 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.16.26 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.16.25 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt
authentication-zero-2.16.24 lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt