File size: 949 Bytes
5010ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bc028a
 
5010ad7
9bc028a
 
5010ad7
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Api
  module V1
    module Auth
      class SessionsController < Devise::SessionsController
        respond_to :json

        private

        def respond_with(resource, _opts = {})
          if resource.persisted?
            render json: {
              user: {
                id: resource.id,
                email: resource.email,
                full_name: resource.full_name,
                role: resource.role
              }
            }, status: :ok
          else
            render json: { error: "Invalid email or password" }, status: :unauthorized
          end
        end

        def respond_to_on_destroy(non_navigational_status: :no_content)
          if non_navigational_status == :unauthorized
            render json: { error: "No active session" }, status: :unauthorized
          else
            render json: { message: "Logged out successfully" }, status: :ok
          end
        end
      end
    end
  end
end