Module: Locomotive::Concerns::Account::ApiKey::ClassMethods

Defined in:
app/models/locomotive/concerns/account/api_key.rb

Instance Method Summary collapse

Instance Method Details

#create_api_token(email, password, api_key) ⇒ String

Create the API token which will be passed to all the requests to the Locomotive API. It requires the credentials of an account with admin role OR the API key of the site. If an error occurs (invalid account, …etc), this method raises an exception that has to be caught somewhere.

Parameters:

  • email (String)

    The email of the account

  • password (String)

    The password of the account

  • api_key (String)

    The API key of the site.

Returns:


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/locomotive/concerns/account/api_key.rb', line 56

def create_api_token(email, password, api_key)
  if api_key.present?
     = self.where(api_key: api_key).first

    raise 'The API key is invalid.' if .nil?
  elsif email.present? && password.present?
     = self.where(email: email.downcase).first

    raise 'Invalid email or password.' if .nil? || !.valid_password?(password)
  else
    raise 'The request must contain either the user email and password OR the API key.'
  end

  .ensure_authentication_token
  .save

  .authentication_token
end

#invalidate_api_token(token) ⇒ String

Logout the user responding to the token passed in parameter from the API. An exception is raised if no account corresponds to the token.

Parameters:

  • token (String)

    The API token created by the create_api_token method.

Returns:


82
83
84
85
86
87
88
89
90
# File 'app/models/locomotive/concerns/account/api_key.rb', line 82

def invalidate_api_token(token)
   = self.where(authentication_token: token).first

  raise 'Invalid token.' if .nil?

  .reset_authentication_token!

  token
end