C83 / THP_2.0

0 stars 0 forks source link

Add authentication #22

Open C83 opened 6 years ago

C83 commented 6 years ago

Why ?

Because we need the users to be authenticated to do certain actions.

Must have

Todo

I advise using my fork of devise_auth_token (multiple bug/feature fixes rebased on last stable version). BUT I can understand that you prefer using the normal version of it. If you use my fork, please read the commits to understand how and why I fixed things.

Reading list

C83 commented 5 years ago

Authentication

Use the fork : gem 'devise_token_auth', :git => "git@github.com:denispasin/devise_token_auth.git"

Generate the User model :

rails g devise_token_auth:install User The prefix of routes is authby default.

Mail server :

In development, we use mailcatcher. It catchs all out mail of the application. Should not include it in the gemfile. Run gem install mailcatcher then mailcatcher to get started. Configure this part with :

# config/environments/development.rb
Rails.application.configure do
  config.action_mailer.default_url_options = { :host => 'localhost` }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { :address => 'localhost`, :port => 1025 }
end
C83 commented 5 years ago

Devise's module

Devise purpose many modules. We add them in the user'model file app/models/user.rb :

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

There are few modules :

Followers exist but don't use yet in our project :

Then, we add the user model concern (after devise's modules) :

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
  include DeviseTokenAuth::Concerns::User

Now, we don't use omniauthable module. It is included by default. So we tell the route helper to skip mounting the omniauth_callbacks controller in config/routes.rb :

  mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]