I have an existing web application developed with Ruby on Rails and I would like to create a simple Flask API to make request over the existing tables and authenticate the API with the preexisting users. What would be the best approach?
I ended up creating an api generator module and requiring it in the user model, setting a before_validation so when a new user signs up the api key is generated. Then the user can authenticate against the flask api with the api key.
# lib/apy_key.rb
module ApiKey
def self.generator
SecureRandom.base64(40)
end
end
# models/user.rb
class User < ActiveRecord::Base
...
require './lib/api_key.rb'
validates_presence_of :api_key
validates_uniqueness_of :api_key
before_validation :set_api_key
private
def set_api_key
self.api_key = ApiKey.generator
end
end