Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect User to Signup

Using before_action :authenticate_user! to check if user logged in. But it sends users to login instead of signup.

Tried different ways of directing user to signup instead of login, but they do not send the user back to the original page after successful signup.

How can I send a user to signup and direct a user back to the original page afterwards?

Attempts:

before_filter :auth_user

def auth_user
  redirect_to new_user_registration_url unless user_signed_in?
end

Routes File

Rails.application.routes.draw do
  devise_for :installs
  resources :orders
  resources :products
  devise_for :users

  get 'dashboard' => 'pages#dashboard'
  get 'contact' => 'pages#contact'
  get 'cart' => 'carts#index'

  root 'pages#home'
like image 346
Onichan Avatar asked Nov 24 '15 01:11

Onichan


2 Answers

What you are looking for is a referer. Having your before_filter working, you can override the Devise's after_sign_up_path_for:

def after_sign_up_path_for(user)
  request.referer
end

EDIT

Since request.referer is somehow not working, I think you could go with a session-based solution:

def auth_user
  session[:before_sign_up_path] = request.fullpath
  redirect_to new_user_registration_url unless user_signed_in?
end

def after_sign_up_path_for(user)
  session[:before_sign_up_path]
end
like image 143
Andrey Deineko Avatar answered Nov 09 '22 01:11

Andrey Deineko


Devise uses warden for authentication. Thus to override the whole behavior you have to override the way devise asks it to handle authentication failures.

Create a class that extends Devise::FailureApp

class RedirectToSignUp < Devise::FailureApp
  def redirect_url
     new_user_registration_url
  end

  def respond
    if http_auth?
      http_auth
    else
      redirect_to new_user_registration_url
    end
  end
end

Add this to your devise initializer config/initializers/devise.rb

config.warden do |manager|
  manager.failure_app = RedirectToSignUp
end

I believe this will solve your problem and will redirect you to the page you were at since you are overriding only the redirect route.

like image 2
Oss Avatar answered Nov 09 '22 02:11

Oss