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
# File 'app/commands/authenticate_user.rb', line 9

def initialize(*args)
    if !args.email.blank? && !args.password.blank?
        @email = args.email
        @password = args.password
    elsif !args.access_token.blank?
        @access_token = args.access_token
    end
end

Instance Method Details

#callObject



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

def call
    if !api_user.blank? && result = JsonWebToken.encode(user_id: api_user.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(valid: false)
        return result
    end
    nil
end