When a user signs up i have three fields: name, email, password
For some users i would like the same email address but then it gives a notification thats already in use.
But i do want devise to authentic my user with name and password...
Do i need to turn off authentication for email?
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Related
Try installing administrate(thoughtbot) and Madmin(GoRails). But in both cases when I change the password of a user in /admin or /madmin and try to login again with the new password, I get the following error.
raise Errors::InvalidHash.new("invalid hash")
I check that user.rb has :database_authenticatable
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Is just standar rails new 7 + devise, bcrypt is commented
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
The users can edit his own password but i need to close the new signups and only create new users manually in /admin
I have a RoR application, running the Devise Gem for authentification. For my new API I've implemented the Gem "devise-token-auth": https://github.com/lynndylanhurley/devise_token_auth
Since I want Devise to run for the website and API authentication, I followed the extra tips in the following instruction as well (and als the FAQ of the Gem's Git): http://www.developingandrails.com/2015/02/api-authentication-with-devisetokenauth.html
I've deactivated Devise's :confirmable, but activated :omniauthable. Everything seems to be in place, but I'm getting the following error:
/Users/sebastianplasschaert/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/devise-3.5.2/lib/devise/rails/routes.rb:240:in `block in devise_for': Mapping omniauth_callbacks on a resource that is not omniauthable (ArgumentError)
Please add `devise :omniauthable` to the `User` model
And in my User model I start with the following code:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
include DeviseTokenAuth::Concerns::User
So, :omniauthable seems to be there. When I deactivate:
include DeviseTokenAuth::Concerns::User
then everything works again (but I need it for my API authentication).
Any ideas on what I'm doing wrong?
for some reason include DeviseTokenAuth::Concerns::User in removing the omniauthable.
I fixed the issue adding it back:
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
include DeviseTokenAuth::Concerns::User
devise :omniauthable
So currently validatable validates the presence of email and password. It can also validate an emails format. However, my user model requires more than just an email and password. I am also requiring a first, last and user name. So in order for me to validate the presence of these attributes I have to use rails validates as shown:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true
validates :last_name, presence: true
validates :user_name, presence: true
end
I was wondering if there was a way to add first, last and user name to the validatable action. I have checked the devise.rb file and found the validatable config for a password_length and email_regexp but don't quite know how I can add additional attributes to the validatable function. Obviously this isn't a huge deal, but would be nice for cleaning up code in my User's model. Thank you for any responses to my questions.
While you could potentially monkeypatch Devise::Models::Validatable at runtime it would be rather foolish. Its going to take 5 times more code and potentially break upgrades.
The whole point of the module is to provide models with the basic validations needed for Devise to work out of the box.
What you are adding are validations which are specific to your application. As such it belongs in your application - don't try to stuff it back into the library.
Instead can clean up your model by:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name, :user_name
end
I am trying to add a role to a specific user in my Rails Console via Heroku , but I am getting the error:
NoMethodError: undefined method `role=' for # .
If you take a look at the screenshot below the role you can see all of the columns in the User table that is available:
The command I am using from the rails console is below to assign the role to the user. I saw a similar post on StackOverflow about the same error but after the db migrate I still got the same error. Can someone point me in the right direction possibly?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_many :posts
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
Below is my model. Can anyone explain me the purpose of attr_accessor: signin here ?. I have seen from some posts here stating that attr_accessor creates getter and setters.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessor :signin
end
In the devise.rb file, i have something like this.
config.authentication_keys = [ :signin ]
Questions:
Can anyone explain me the purpose of attr_accessor: signin here ?.
What does this config.authentication_keys = [ :signin ] lead to in device.rb file?.
This option enables you to pick authentication key. You are telling devise to use accessor signin (that you declared above).
Default one used is email:
config.authentication_keys = [ :email ]