Module: Locomotive::Concerns::Account::ApiKey::ClassMethods
- Defined in:
- app/models/locomotive/concerns/account/api_key.rb
Instance Method Summary collapse
-
#create_api_token(email, password, api_key) ⇒ String
Create the API token which will be passed to all the requests to the Locomotive API.
-
#invalidate_api_token(token) ⇒ String
Logout the user responding to the token passed in parameter from the API.
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.
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? account = self.where(api_key: api_key).first raise 'The API key is invalid.' if account.nil? elsif email.present? && password.present? account = self.where(email: email.downcase).first raise 'Invalid email or password.' if account.nil? || !account.valid_password?(password) else raise 'The request must contain either the user email and password OR the API key.' end account.ensure_authentication_token account.save account.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.
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) account = self.where(authentication_token: token).first raise 'Invalid token.' if account.nil? account.reset_authentication_token! token end |