0

I am using devise token authentication(devise_token_auth) for user login and signup based on token because it is only for developing an api. I need to make the user logged in soon after the user registers his/her account. How can i do it so? I have tried but could not succeed so i am here with the hope of help.

class Users::RegistrationsController < DeviseTokenAuth::RegistrationsController
  prepend_before_action :configure_permitted_parameters
    protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:account_update, keys: [:id, :first_name, :last_name, :phone, :status])
      devise_parameter_sanitizer.permit(:sign_up, keys: [:confirm_success_url])
    end

  private

    def user_params
      params.permit.all #(:id, :email, :firstname, :lastname, :phone, :status)
    end
end

Rails.application.routes.draw do
  # devise_for :users, controllers: { confirmations: 'confirmations' }
  mount_devise_token_auth_for 'User', at: 'auth', controllers: {
     # confirmations: 'confirmations',
      registrations: 'users/registrations',
      passwords: 'users/passwords'
  }

class ApplicationController < ActionController::API
  before_action :authenticate_user!
  include DeviseTokenAuth::Concerns::SetUserByToken

  def authenticate_current_user
    head :unauthorized if get_current_user.nil?
  end

  def get_current_user
    return nil unless cookies[:auth_headers]
    auth_headers = JSON.parse cookies[:auth_headers]
    puts('################################')
    puts('auth_headers', auth_headers)
    expiration_datetime = DateTime.strptime(auth_headers["expiry"], "%s")
    current_user = User.find_by(uid: auth_headers["uid"])

    if current_user &&
       current_user.tokens.has_key?(auth_headers["client"]) &&
       expiration_datetime > DateTime.now

       @current_user = current_user
    end

    @current_user
  end

end

I tried this below code

def after_sign_up_path_for(resource)
      puts('it should be shown')
      puts('################################')
      puts('resource', resource)
      puts('header', request.headers['client'])
      client_id = request.headers['client']
      new_auth_header = @resource.create_new_auth_token(client_id)
      response.headers.merge!(new_auth_header)
  end

in the Users::RegistrationController but it is not executed at all after successfully signing up.

Serenity
  • 3,884
  • 6
  • 44
  • 87

1 Answers1

0

Not too complicated. After creating the user, just call sign_in and pass the resource (user).

https://github.com/plataformatec/devise/wiki/How-To:-Sign-in-from-a-controller

Example:

sign_in @current_user

The Devise::RegistrationController in fact, already does this.

Furthermore, since you're using the DeviseTokenAuth gem and the DeviseTokenAuth::RegistrationsController controller inherits from Devise's own base controller, you (should) have access to all the helpers that Devise controllers do.

One implementation might look like this.

class Users::RegistrationsController < DeviseTokenAuth::RegistrationsController
  def create
    super do |resource|
      sign_in(resource)
    end
  end
end
Volte
  • 1,905
  • 18
  • 25
  • so does this works when I am developing only an api from the rails? – Serenity Mar 12 '18 at 23:58
  • Should i use that sign_in controller inside registration_controller(class Users::RegistrationsController) like def sign_in? or can you show me where in the code should i implement that? – Serenity Mar 13 '18 at 01:23
  • @Serenity updated answer, let me know if that doesn't help! – Volte Mar 13 '18 at 21:28
  • but I am using DeviseTokenAuth(devise_token_auth gem). – Serenity Mar 14 '18 at 02:24
  • @Serenity I did the footwork and `Read The Fantastic Manual` for you ;) check out the update answer! – Volte Mar 15 '18 at 01:07
  • I tried that create function but i get error RegistrationController has not create function, something like that. probably its because i have not use the super keyword. – Serenity Mar 15 '18 at 02:49