Class: AuthenticateUser

Inherits:
Object
  • Object
show all
Includes:
SimpleCommand
Defined in:
app/commands/authenticate_user.rb

Defined Under Namespace

Classes: AccessDenied

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AuthenticateUser

Returns a new instance of AuthenticateUser.



9
10
11
12
13
14
15
16
17
# File 'app/commands/authenticate_user.rb', line 9

def initialize(*args)
    first_arg = args.first
    if !first_arg[:email].blank? && !first_arg[:password].blank?
        @email = first_arg[:email]
        @password = first_arg[:password]
    elsif !first_arg[:access_token].blank?
        @access_token = first_arg[:access_token]
    end
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/commands/authenticate_user.rb', line 19

def call
    current_u = api_user
    if !current_u.blank? && result = JsonWebToken.encode(user_id: current_u.id)
        # The token is created and the api_user exists => Invalidating all the previous tokens
        # Since this is a new login and I don't care from where it comes, new logins always
        # Invalidate older tokens
        UsedToken.where(user_id: api_user.id).update(is_valid: false) if ENV["ALLOW_MULTISESSIONS"] == "false"
        return result
    end
    nil
end