Issues when adding devise-jwt to a existente User model in Rails - ruby-on-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]

Related

uninitialized constant Users in Login facebook rails

I am developing an app in Rails that will be integrated with facebook at the beginning of the session, but as I test the code, I get the following error.
uninitialized constant Users
I'm using Devise and omniauth-facebook
devise.rb
config.omniauth :facebook, "App ID", "App Secret", callback_url: "http://localhost:3000/users/auth/facebook/callback"
model/User.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :omniauthable,
omniauth_providers: %i[facebook]
end
routes.rb
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
root 'main#home'
I am using rails in its latest version (5.2.1) and ruby ​​2.5.1
with this code you should already establish a connection

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 can't detect :omniauthable, but it's there

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

devise signin using username or email

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 ]

Get variable from another table in ROR models

I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end

Resources