Devise can't detect :omniauthable, but it's there - ruby-on-rails

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

Related

attr_encrypted?': undefined method `has_key?' for nil:NilClass (NoMethodError)

I am working on Multi Factor authentication on a rails api with devise JWT auth.
I am following the tutorial that comes with the gem "devise-two-factor", here is the link to it : https://github.com/tinfoil/devise-two-factor
When I add an encryption key in the model as an environment variable and run this command :
rails generate devise_two_factor MODEL ENVIRONMENT_VARIABLE
I get this error that I couldn't find anything relevant to :
`attr_encrypted?': undefined method `has_key?' for nil:NilClass (NoMethodError)
Here is the user model:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:jwt_authenticatable,
jwt_revocation_strategy: JwtDenylist
devise :two_factor_authenticatable,
:otw_secret_encryption_key => ENV['encryption_key']
end
I can't understand the problem because I'm not experienced with rails and not a good Ruby developer

exclude authentication for email (one of the user details) rails

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

Issues when adding devise-jwt to a existente User model in Rails

Im trying to add the devise-jwt to an existent app I have. Im adding API endpoints now, and want to use the same Model I have already
I added the gem devise-jwt following this article here: https://medium.com/#mazik.wyry/rails-5-api-jwt-setup-in-minutes-using-devise-71670fd4ed03
I had configured my devise.rb file with:
config.jwt do |jwt|
jwt.secret = ENV['DEVISE_JWT_SECRET_KEY']
jwt.dispatch_requests = [
['POST', %r{^/login$}]
]
jwt.revocation_requests = [
['DELETE', %r{^/logout$}]
]
jwt.expiration_time = 1.day.to_i
end
Had created my jwt_blacklist.rb and migrations for it:
class CreateJwtBlacklist < ActiveRecord::Migration[6.0]
def change
create_table :jwt_blacklist do |t|
t.string :jti, null: false
end
add_index :jwt_blacklist, :jti
end
end
class JWTBlacklist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Blacklist
self.table_name = 'jwt_blacklist'
end
When I try to add those lines to my user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook],
:jwt_authenticatable,
jwt_revocation_strategy: JWTBlacklist
And when I try to start my server im getting:
/Users/fmaymone/.rvm/gems/ruby-2.6.3/gems/activesupport-
6.0.0/lib/active_support/dependencies.rb:511:in `load': /booksculp/app/models/user.rb:6:
syntax error, unexpected ',', expecting => (SyntaxError)
:jwt_authenticatable ,
Someone knows what Im doing wrong here?
thanks
Change your User model to the following:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,
:jwt_authenticatable, jwt_revocation_strategy: JWTBlacklist, omniauth_providers: [:facebook]

NameError in RailsAdmin::Main#index

I'm using rails_admin along with the devise gem, looks like everything is working fine with dashboard but whenever I switch to user it throw uninitialized constant User::Authentication raise NameError. new("uninitialized constant #{candidates.first}", candidates.first) error. https://i.stack.imgur.com/DWOP4.png This is my error screenshot. Any help will be appreciated.
Below is my user.rb file,
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
has_many :comments
has_many :authentications
end
I struggled with this today while trying to namespace a model, and found that the basic search filters were to blame;
The problem was resolved as soon as I added a new filter; I'd say it's because adding a new filter removed the basic filters that created the issue... (here is an explanation on how to do it : https://activeadmin.info/3-index-pages.html)
I hope this will be useful.

Devise, where to add a model

I want to add a timeoutable model to devise after my initial configuration.
I enabled the config/initializers/devise.rb:
config.timeout_in = 30.minute
But where do I actually add the devise :timeoutable model?
After you install devise you need to configure model using built in generator
rails generate devise MODEL
example rails generate devise User. See this section
https://github.com/plataformatec/devise#user-content-getting-started
Once you have that inside app/models/user.rb you can add desired modules
class User < ApplicationRecord
devise :timeoutable, :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable
end
Hope it helps

Resources