Sha256: 63dbd0cd06c96f9a8fc1ea806d2f6bcccbf747adce4b5bb4662bce2e3dc15386

Contents?: true

Size: 1011 Bytes

Versions: 1

Compression:

Stored size: 1011 Bytes

Contents

class SessionsController < ApplicationController
  skip_before_action :authenticate, only: :create

  before_action :set_session, only: %i[ show destroy ]

  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: "Invalid email or password" }, status: :unauthorized
    end
  end

  def destroy
    @session.destroy
  end

  private
    def set_session
      @session = Current.user.sessions.find(params[:id])
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authentication-zero-2.0.0 lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt