Sha256: c65a3c67b7626e062d803771d43696b072acc585a7fbcca80e8010e3399e4e42

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

class SessionsController < ApplicationController
  before_action :set_session, only: %i[ show destroy ]

  skip_before_action :authenticate, only: :create

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

  def show
    render json: @session
  end

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

    if @<%= singular_table_name %>.try(:authenticate, params[:password])
      session = @<%= singular_table_name %>.sessions.create!(session_params)
      response.set_header("X-Session-Token", session.signed_id)

      render json: session, status: :created
    else
      render json: { error: "That email or password is incorrect" }, status: :unauthorized
    end
  end

  def destroy
    @session.destroy
  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 }
    end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
authentication-zero-2.2.7 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.6 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.5 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.4 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.3 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.2 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt
authentication-zero-2.2.1 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt