Devise If logged in automatically send to home page - ruby-on-rails

I have a base installation of Devise with everything setup and working correctly. I have a 'welcome' page and a 'dashboard' page which only logged in users can get to.
What I'm trying to achieve is to make all logged in users automatically skip the landing page and land straight on the dashboard when they come back to the site.
e.g.
I sign up from the landing page
I'm logged in
I close off chrome and go for food
I open chrome and go to the root url (example.com)
I get presented with my dashboard instead of the welcome page as I am logged in.
Any help is greatly appreciated

You should be able to do a simple check in the controller for if the session is active and if so redirect_to dashboard_path
I havent used devise in forever so I don't remember if they have a built in current_user (see the current_user setup here) method or not, but checking if that is nil would be a good check.

You need to create an after sign in devise method in your application controller:
def after_sign_in_path_for(resource)
your_after_sign_in_path
end
That should redirect all users to your_after_sign_in_path after they sign in.

Related

Devise showing user already logged in on password reset,

I am using devise plugin for resetting the user password, i have this problem, so the scenario is like , when i send link for resetting the password it lands to a custom reset page, which have view for resetting the password , but the weird thing the user is already logged in , when it lands to that page, if i click on main tab without resetting the password, i can navigate like i am already logged in.
How can i make sure that user should not be already logged in, once i reset the password then it should login again to use the account.
Maybe you missing this line in your controller:
before_action :authenticate_user! except: :change_password
This will not login the user for that particular controller action.
Note that change_password will be the name of your controller action for changing the password.
Unexpected login sometimes happen if routes.rb allows two different signin path when different user models are used.
Run rake routes and check if there is new_user_session_path along with, for example, new_buyer_session_path.
If there is, fixing your routes might solve your problem.

Rails: redirect back to previous page after sign in / sign up

I am using Rails and devise for user authentication, I don’t know how to redirect the user back to the previous page after successful sign in/sign up.
I know about after_sign_in_path_for, but I don’t know what I should write inside.
You can write custom sessions controller for devise login and use request.referrer to get the URL of the page the user is coming from. Then just redirect back to the previous page after login. Also refer this doc. You could go by one of the methods. Hope it helps.

Rails/Devise: Force sign out & redirect on all pages when user session destroyed

I have a persistent error that keeps on popping up. Here's the use case:
User is logged in & has multiple tabs/windows of the app open.
User logs out (session is successfully destroyed) & is redirected back to home page (root_path).
User reloads or takes action on any of the other already open tabs.
User encounters error and must manually navigate to root_path via address bar.
Is there a best practice to prevent this altogether? Is it better to have an error page that has a link to the root path where they can log back in?
I appreciate any help at all. Thanks!
According to the documentation if a user is not authenticatable you can redirect to a specific page.
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated
Maybe it is also helpful :
Redirect to log in page if user is not authenticated with Devise

Rails return to specific location with route

I have a link on a page that is for users who are not signed in yet. I want this link to redirect to the login_url, which upon logging in will redirect the user back to that page they were on.
Something like this would be great but I don't know how to route this.
Currently, I have a redirect_back helper but that only works on specific actions where you need to be signed in. This is just a link.
The login url is /login and I would like something like this:
("/login?return_to=" + #user.username)
for the link so when you click the link (say on the user jcl), it takes you to /login?return_to=jcl. Then after signing in with a POST request, it returns you to that user's page.
Is there specific routes or helpers that can accomplish this?
you can use a before_filter to save the url in the session and before login you redirect the user to the url in the session.

Devise sign in and registration forms from somewhere else in my app

I am building an e-commerce style site which lists products that you can buy. When you click on buy it takes you through to a URL like: http://shopfront.com/deal/123/buy.
I would like to provide Devise sign up and sign in on that buy page. I can currently get users to sign up and upon a successful sign up they will be redirected back to my buy page for the item they are interested in but if they provide insufficient details, they get redirected to devise's default sign up form which displays the error they encountered. Afterwards they are no longer redirected to my buy page and instead end up on the home page.
I have my own registrations controller which is where I am doing the redirect back to the relevant buy page on successful sign ups but I cannot figure out how to redirect unsuccessful sign ups back to the buy page.
I need to implement more or less the same functionality for a sign in form which will be displayed next to the sign up form on that same buy page. Any assistance would be appreciated.
Nice way is to store client state in session
put something like,
session[:return_to] = request.fullpath
in your controller, you may like to put this in private method
In general "in session", means to store data in cookies,
as session store points by default to
Rails::Application.config.session_store
=>
ActionDispatch::Session::CookieStore

Resources