Knock: login without email

Created on 14 Aug 2017  路  3Comments  路  Source: nsarno/knock

My application logs in with username, how can I generate the token with username instead of email?

Most helpful comment

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

All 3 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YMonnier picture YMonnier  路  3Comments

gingerhot picture gingerhot  路  6Comments

FNGR2911 picture FNGR2911  路  7Comments

ghost picture ghost  路  4Comments

saroar picture saroar  路  3Comments