Devise rememberable does not work in jQuery Mobile - jquery-mobile

Devise rememberable is supposed to set a remember_user_token cookie on the client so that even when the session cookie is deleted, the user does not have to log in again.
t.rememberable is set in the create_users migration and devise :rememberable is present in the user model.

The reason for this is because of the way jQuery mobile handles checkbox inputs and the labels for them when reformatting the page. The simplest fix is to install the devise views in your project by running rails g devise:views. Then in the new.html template for the session controller, switch the order of the checkbox and the label so that the label for :remember_me comes after the checkbox.

Related

Devise Reset Password POSTs instead of PUT

I'm using a non customized version of Devise in my app and am running into some issues with the reset password functionality.
The default reset password form's method is to PUT but upon submitting the form, it POSTs which causes it to give me an error about a missing email field. The POST/create method in the PasswordsController of Devise is emailing the user the password reset link. The PUT/update method is for the actual resetting of the password.
None of the devise stuff is altered in any way. Devise version is 3.5.7 and Rails is 4.2.6
I've put all of the relevant code in a Gist here: https://gist.github.com/dsarhadian/a7950e480bffc2906f77b0e542792f5a
Any help is truly appreciated...
In your logs it says:
Rendered devise/passwords/edit.html.erb within layouts/application (3.3ms)
Are you sure that the edit.html.erb file renders the form.html.erb view?
Because when I look at the source.html file, it seems like it is rendering a different form which has the path for the POST method.
The form.html.erb uses the PUT method. So it seems to me like the wrong form is rendered.

Rails: Conditionally enable / disable devise modules

I am building my own dynamic backend in top of Ruby on Rails framework. I would like to know if there is a way to conditionally enable or disable Devise modules such as registerable, confirmable, omniauthable, ...
What I did try almost worked except I had to restart the rails server to take in consideration my modification but in my case I would like the system to work with boolean defined in the database.
For example:
devise :registerable if registerable_module.enabled? # boolean fetched from database
Thanks for your help

Rails + Devise: Rememberable vs. Non-Rememberable?

So I have a Rails application using Devise, and I'm a little confused as to how Devise Rememberable works. I have :rememberable enabled in my User model. When I go to log in WITHOUT checking the Remember me? box, I'm still logged even after a browser close AND a computer restart.
If this is a case, what is the point of rememberable? Am I missing something?
Thanks!
Removing expire_after: 504.hours from config/initializers/session_store.rb seemes to have solved the problem, rather than removing the file altogether.
Source: how to clear devise session on browser close?

Customizing Devise views in Rails

I'm using devise for user auth, but I have nice mockups for the signup, login, etc. pages.
I've already done the rails generate devise:views User command and have all of the views in the views folder, however, when I replaced the registration/new.html.erb with my own new.html.erb, nothing changes nor looks different. It's as if I had done anything.
Anyone know what I'm doing wrong or at least how to successfully customize devise views
P.S. Is it important to note that I changed the route of devise/registration#new to /signup?
at a glance answer.
...instead of
rails generate devise:views User
use:
rails generate devise:views
If you've already done it, move the folders devise created from app/views/User to a new folder app/views/devise (or just rename the User folder to devise, if that's an option.)
Those folders are:
app/views/User/confirmations
app/views/User/mailer
app/views/User/passwords
app/views/User/registrations
app/views/User/sessions
app/views/User/shared
app/views/User/unlocks
No other changes are necessary.
though this is an old question, I thought I'd add to it in case anybody stumbles on it. I'm not sure if this is a new addition since the question was originally asked but if so the simpler (more modern) approach is this.
in the file config/initializers/devise.rb there is the following block of code:
# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
# config.scoped_views = false
by uncommenting config.scoped_views = false and changing it's value to true, devise will automatically check whether the custom view exists and if so, serve that up.
As the comment says, it may add some overhead to the application but in my experience so far, this is negligible.
Your route signup or devise/registrations#new will render the view
views/devise/registrations/new.html.erb. It sounds like you made
changes to views/user/registrations/new.html.erb, which would explain
why you dont see the changes made since its not being rendered.
You will either need to create a user/registrations_controller.rb that
extends from Devise::RegistrationsController and point your /signup
route to user/registrations#new, or you can just make your changes
directly to views/devise/registrations/new.html.erb
Same idea applies to your login (devise/sessions) pages.
Hope this helps.
For anyone still having a problem with this, the problem lies in the call to rails generate devise:views User. It should be rails generate devise:views for fetching current views from the Devise Rails Engine. This will generate proper views which will work with the default routes.
After generating your custom views e.g
rails generate devise:views User
Turn on scoped_views in config/initializer/devise.rb
view config.scoped_views = true
And you are done.
I had the same problem until I went back and read the devise documentation :)
After rails generate devise:views make sure you go into initializers/devise.rb and set config.scoped_views = true. This is explained in the devise documentation at https://github.com/plataformatec/devise as well as in the devise.rb comments.
After I did this, my own views in views/users started showing up instead of the ones in the gem.
Using rails g devise:views User allows you to customize when you have more than one role.
the proper way to do this is going into your devise.rb in config/initializer/ folder
and uncommenting and setting config.scoped_views = true.
now you can edit the view erb files without any problems
For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.

Devise and ActionMailer confusion

I'm using Devise for authentication, and I'm confused on how to set up mail along with it. Should you still be creating your own mailer and initializer file, or should you be sending all mail through Devise? Where do you go in Devise to create the email template and the method for sending the email?
I realize this is kind of a broad question, so essentially I'm asking what is the best way to set up mail with Devise?
Also, if you wanted to send an email to a user after they have confirmed their email, how would you do this?
Devise does create it's own Mailer--if you take a look on GitHub https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb you can see that it comes with a bunch of methods already packaged in.
You can go ahead and generate the views for these methods using the command
rails g devise views
and then edit them.
If you would like to send additional email messages you should create your own Mailer to do so. I would recommend http://edgeguides.rubyonrails.org/action_mailer_basics.html . It's a pretty good overview of how to set up a mailer from the ground up.
Devise creates the mailer and email templates for you, so you don't have to worry about that. However, if you want to change the email templates, install devise views using the command:
rails g devise:views
This will add a new "devise" folder in your views. You can find all the email templates in the mailer folder under views/devise.
Use the confirmable attribute to send confirmation emails to users post registration.
By default, this attribute is commented out. So, once you install devise using the command rails g devise:install, go to db/migrate and locate the devise_create_users migration and uncomment the following lines:
t.confirmable and
add_index :users, :confirmation_token, :unique => true.
Once done, migrate your database.
Now go to your user model and check if devise has the :confirmable attribute, if not add it and you are all set.

Resources