Morez SA Morez SA - 1 month ago 8
Ruby Question

Show page only once after sign up - Ruby on Rails

I have an app that is using Devise, I would like that after a user signs up, they are directed to a specific page, this page calls an API and saves the value from the API, I need this page to only be accessible or available after a user completes the sign-up form and clicks submit, and is then redirected to this page.

I do not want this page or URL accessible any other way but after sign-up, as the API will send a new value if accessed again. How can I accomplish this?

Once a user signs up they will be redirected to the page calling the API:

def after_sign_up_path_for(resource)
api_call_path ##path that can only be accessed after sign_up
end


The API is called and the response from the JSON data is automatically saved to the database once the page is opened, if the page gets opened again a new JSON response will be received with new data, which is what I would like to avoid.

So in a nutshell, my question is how can I restrict access to a specific path, and only make that path accessible if a user completes the sign-up form (devise) OR is there a way that I can run the code from the controller using a callback/filter after the user is created through the User model?

Answer

I was just busy with something similar. You do not need to direct the user to a page to run the code, you can just run the code that needs to be run after the user logs in the first time.

You need to create a Session Controller, and create a conditional statement that checks if the user has logged in previously:

#config/routes.rb
devise_for :users, controllers: { sessions: "sessions" }

#app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController

  def after_sign_in_path_for(resource)
    if resource.sign_in_count == 1
       ##Do something awesome
    else
       root_path
    end
  end
end

As Emmanuel advised you can check futher info on the Devise Controllers.