Module: GrapeDeviseTokenAuth::AuthHelpers

Defined in:
lib/grape_devise_token_auth/auth_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(_base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grape_devise_token_auth/auth_helpers.rb', line 3

def self.included(_base)
  Devise.mappings.keys.each do |mapping|
    define_method("current_#{mapping}") do
      warden.user(mapping)
    end

    define_method("authenticate_#{mapping}") do
      authorizer_data  = AuthorizerData.from_env(env)
      devise_interface = DeviseInterface.new(authorizer_data)
      token_authorizer = TokenAuthorizer.new(authorizer_data,
                                             devise_interface)
      user = token_authorizer.authenticate_from_token(mapping)
      devise_interface.set_user_in_warden(mapping, user) if user
      user
    end

    define_method("authenticate_#{mapping}!") do
      user = send("authenticate_#{mapping}")
      fail Unauthorized unless user
      user
    end
  end
end

Instance Method Details

#authenticated?(scope = :user) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/grape_devise_token_auth/auth_helpers.rb', line 31

def authenticated?(scope = :user)
  user_type = "current_#{scope}"
  return false unless respond_to?(user_type)
  !!send(user_type)
end

#wardenObject



27
28
29
# File 'lib/grape_devise_token_auth/auth_helpers.rb', line 27

def warden
  @warden ||= env['warden']
end