My application logs in with username, how can I generate the token with username instead of email?
You can modify the default behavior implementing within your entity model a class method from_token_request that takes the request in argument.
class User < ActiveRecord::Base
def self.from_token_request(request)
username = request.params["auth"] && request.params["auth"]["username"]
self.find_by(username: username)
end
end
More info: https://github.com/nsarno/knock#via-the-entity-model
You might also need to override strong parameters:
app/controllers/user_token_controller.rb
class UserTokenController < Knock::AuthTokenController
private
def auth_params
params.require(:auth).permit(:username, :password)
end
end
app/models/user.rb
class User < ActiveRecord::Base
def self.from_token_request(request)
username = request.params["auth"] && request.params["auth"]["username"]
self.find_by(username: username)
end
end
I鈥檓 also using a username and no emails. What about making this configurable?
Most helpful comment
You might also need to override strong parameters:
app/controllers/user_token_controller.rbapp/models/user.rb