1

On our Rails 5 app, I've been asked to implement a feature that shows the user a message if the software has been updated since their last login, and possibly enable it to do general announcements or a message of the day.

I'm fine with the logic for comparing login dates, retrieving messages, and whatnot, but it seems like this needs to interact with the Devise login process in some way, possibly a callback, and I don't have any idea how to do that. Some Googling hasn't led me to much.

I guess I'd just like some guidance as to how to get started. I planned just to use the notice/alert flash message feature, but I can go a different direction if that would be better.


Update: I found another SO entry that suggested using the after_database_authentication callback, but when I put that callback in my User model with a debugger, I don't seem to hit the code.

whognu
  • 439
  • 1
  • 5
  • 15

2 Answers2

2

One possible solution would be to subclass the Devise session controller as described in their README.

In the SessionsController#create method (or #new, whatever is appropriate for your application) just add a block in the form of

def create
  super do
    flash[:notice] = load_motd
  end
end
Marcus Ilgner
  • 6,935
  • 2
  • 30
  • 44
1

So with devise you can just override the following controller

class UserSessionsController < Devise::SessionsController

   after_action :after_login, :only => :create
   def after_login
      flash[:message_of_the_day] = "Welcome to our site!" #or make some db query to grab your saved text.
   end
end

Then in your view, you can display the flash message like so:

<h1><%= flash[:message_of_the_day] %></h1> 

Add this to your routes file:

devise_for :users, :controllers => { :sessions => "user_sessions" }

When you create a new user you will need to create a user_registrations_controller.rb file and place the following code in that file.

class UserRegistrationsController < Devise::RegistrationsController

  after_action :after_sign_up, :only => :create   

  def after_sign_up
     if current_user.present?
       flash[:message_of_the_day] = "Welcome to our site"
     end
  end
end

Then edit the route to look like this:

devise_for :users, :controllers => { :sessions => "user_sessions", registrations: 'user_registrations' }
Cannon Moyer
  • 3,014
  • 3
  • 31
  • 75
  • Thanks! Where would this code go - just a new controller file in app/controllers? And do I need to call super in some way, instead of an outright override? – whognu Nov 28 '18 at 19:44
  • 1
    Yes, just put it app/controllers. No need to call super since you're inheriting `Devise::SessionsController` and the method is overridden. – Cannon Moyer Nov 28 '18 at 19:57
  • I created file user_sessions_controller.rb, put almost your exact code in it, but put a debugger in the after_login method, but it's not hitting that code when I log in. – whognu Nov 28 '18 at 19:59
  • 1
    Are you logging in with an existing user account or creating a new one? I'm using that exact code to initialize cookies on a production app. I am using devise 4.4.3. – Cannon Moyer Nov 28 '18 at 20:03
  • I'm logging in using an existing user - I assumed the :create restriction referred to making a new session, not a new user. – whognu Nov 28 '18 at 20:09
  • 1
    I'm an idiot. I've updated the answer with the needed route. – Cannon Moyer Nov 28 '18 at 20:14
  • Thanks! Right now, our routes.rb has this line: `devise_for :users, controllers: { confirmations: 'confirmations' }` I don't really know what that does - do you know if changing it will cause problems? – whognu Nov 28 '18 at 20:17
  • 1
    There's a confirmations.rb file in your app/controllers directory. It would be another class override just like we have done with the sessions. I don't see any reason that adding that callback in the sessions override would break anything in the confirmations controller. I would definitely test that however to make sure. – Cannon Moyer Nov 28 '18 at 20:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184431/discussion-between-cannon-moyer-and-whognu). – Cannon Moyer Nov 28 '18 at 20:27