I am using the default Devise controllers. I assumed when a user created a new account, they'd be redirected to the sign_in page and shown the flash message for signed_up_but_unconfirmed. The redirect is happening, but the flash message being displayed is unauthenticated.
Why would I need to tell Devise to show the signed_up_but_unconfirmed message when new accounts are created? That seems like the way it should work out of the box.
And more importantly, how do I configure it to show signed_up_but_unconfirmed?
I finally found an answer in an older question. The short of it is this:
Override the registration controller:
rails g devise:controllers users -c=registrations
Uncomment the after_inactive_sign_up_path_for method in the registratons_controller:
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
Related
I'm trying to add this callback to my User model, which I generated using Devise.
before_save :check_invite_code
def check_invite_code
if self.invite_code == 'first20'
User.save
else
{icon: 'error', message: "Sorry that's not a valid invite code"}
end
end
The issue I am having is with passing the returned hash with the else block back to my view. Typically I'd be able to use the icon and message in the flash in my controller. I'm not sure how to do that. I don't have a UsersController because devise takes care of a the routing with the path being devise/controller#action. So do I create a devise directory inside of controllers, then create the corresponding controllers like sessions, etc and override the devise methods? Looking for some guidance from someone with devise experience.
If you want to customize the UserController you can easily do that by
rails generate devise:controllers [scope]
For example
rails generate devise:controllers users
Check out the documentation here
I was recently using Svbtle.com where they show a page immediately after logging out. It says "Goodbye.", along with link to go "Back to SVBTL".
I like the idea of a 'farewell' page, similar to how they did it, and would like to do something similar in a project I'm working on.
The 'farewell' page on Svbtle has a path of https://svbtle.com/notify?logout. When you reload the page or try to navigate to https://svbtle.com/notify?logout, it redirects you to the site landing page.
What is this magic?
How would I go about only showing a page upon user logout, but then prevent them from visiting it otherwise?
I'm using Rails 5.0.0.1 and Devise for authentication.
Create a static goodbye page with whatever content you want. Edit your routes.rb and give the goodbye page a route (we'll call it goodbye_page_path for illustrative purposes here).
Go into app/controllers/application_controller.rb and create a method called after_sign_out_path_for, which is a standard Devise helper. Set it up like this:
def after_sign_out_path_for(resource_or_scope)
goodbye_page_path
end
That should redirect users to your goodbye page whenever they log out.
To prevent access to the goodbye page, store a flag in the session object. In the controller method handling logout:
session[:goodbye] = true
In the controller method that handles displaying the goodbye page:
def goodbye_page
if session[:goodbye] && session[:goodbye] == true
render 'goodbye_page'
end
end
I started implementing this and wanted to share what I went with. I tried writing to session initially but ran into problems as session wasn't available after logout where the GoodbyeMessagesController scope lies. I ended up going with a cookie that is set immediately after sign out, then deleted in my goodbye controller:
ApplicationController
def after_sign_out_path_for(resource_or_scope)
cookies[:single_view_page] = true
goodbye_path
end
GoodbyeMessagesController
def show
if cookies[:single_view_page]
cookies.delete :single_view_page
# Other logic...
else
redirect_to root_path
end
end
It ended up being super easy, and it works great.
So I have this app that I'm making where users have profiles after they signup and input their information.
At the moment I'm trying to add a feature that allows for new unregistered users to go to a profile to see what the app is like before they need to sign up (I'm planning on putting a "try it for free" button on the home_controller#index action. For authentication, I'm using the Devise gem.
Currently, I've watched the Railscast (393) on this, but I can't figure out (after several hours of trying) how to implement guest users and log them in using Devise.
I've also read about 4 different solutions on SO, and have decided to stick to this one (how to create a guest user in Rails 3 + Devise):
class ApplicationController < ActionController::Base
def current_user
super || guest_user
end
private
def guest_user
User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id])
end
def create_guest_user
u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(99)}#example.com")
u.save(:validate => false)
u
end
...
end
I have this in my application_controller.rb and don't understand how I would use these functions in the home_controller#index to create a guest user and log into the profile when the "Try it" button is clicked.
I've tried manually creating a user, saving it without authentication and then using Devise's sign_in method on link like so: Try it! and also tried
Try it!
I tried this, but the profile throws some validation messages saying I need to log in to view it. I've also tried removing before_filter authenticate! on the profiles_controller but I can't seem to get this to work at all.
Would anyone know how to create a user on the button click, and auto sign them into a guest profile? Thanks a lot for any help, I'm completely lost here.
I think you have a misunderstanding on what a guest user is. You say that you want guest users to auto sign in, and that is wrong. Guest users can't sign in, because... Well, because they are guests. You want to create them, but not sign them in.
This is why you want these helper methods in your ApplicationController, because when you try to get the current_user, if that doesn't exist (nil), you will have a fallback (that is why you use the || operator), that will assign a guest_user as a current_user.
So, forget about using sign_in links for guest users and you should be fine.
I can have two types of users sign up on my app, "girls" and "boys". If a girl signs up I want to redirect to "/girls" and if a boy signs up I want to redirect to "/boys".
Is it possible to do custom redirection with Devise?
The closest docs I found are here: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in. The problem is I can't do any check to switch the redirect route.
Options I've considered:
Pass an additional URL param when user clicks "sign-up". like
?is_girl=1.
After they click sign-up, when determining the redirect route, I could look at the users model and see if they're a girl or boy. Then redirect accordingly.
I am going to assume as part of the sign up process you ask them if they are a boy or girl and this is saved in the database.
So you would just need to do like the example in the Devise docs is showing
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
if resource.sex == 'boy'
'/boy' # or you could create a route in routes.rb and do boy_path
else
'/girl' # with routes setup: girl_path
end
end
I'm using Devise with my rails 3 app. The app requires users to validate their email before continuing.
How can I redirect users to a specific url like /gettingstarted after they successfully validate their email address via the email confirmation msg they receive?
Thanks
When a user clicks on the confirm link they are taken to a confirm page which checks the confirmation token and if it's valid automatically logs them into the application. You could overwrite the after_sign_in_path_for method in your ApplicationController (as shown on the Devise wiki) and then redirect them to your getting started page the first time a user logs in.
def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User) && first login
getting_started_path
else
super
end
end
For "first login" you could test if the confirmed_at timestamp is within a couple minutes of now, if your also using the trackable module in devise you can check if the sign_in_count is 1 or you could create your own field in the user model that tracks this information.
I'm checking devise source code at https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb
and seems that we have a callback to do it "after_confirmation_path_for"but I couldn't get it working without rewrite Devise::ConfirmationController
I hope that helps and if somebody get it working just defining after_confirmation_path_for just let us know.
I'm using the last_sign_in_at field from the 'trackable' model to achieve this. I've got the following code in my root action:
if current_user.last_sign_in_at.nil? then
redirect_to :controller => :users, :action => :welcome
end
http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Trackable
Seems to work reasonably well.
inside the 'after_sign_in_path_for' the current_user.last_sign_in_at.nil? will not work since it is alerady after the first sign-in. However this will work
if current_user.sign_in_count == 1
# do 1 thing
else
# do another thing
end