Simple User Authentication for Rails Apps
Whats the first thing you do when you download a new app? Typically, you sign up and log in!

If you are new to Rails, you might be wondering the best way to create log ins and seamlessly track new and returning users. One way to do this in rails is using a wonderful gem called devise.
This gem allows you to generate logins and store passwords for authentication using your User model.
In this blog, I will give you 6 simple steps for integrating devise into your app. Using this, you will have user authentication set up in just a few minutes!
- First, add your gem to your Gemfile.rb.

and dont forget to:
bundle install2. After your gem is installed, run:
rails generate devise installYou should see some instructions in your terminal for setup:

Add your default url path in your config/environments/development.rb file

Typically, that URL would look like this:
config.action_mailer.default_url_options = { host: ‘localhost’, port: 3000 }Be sure to follow steps 2–4 as well. This sets up a view file for you and connects your controller methods to their routes.
4. Ok, so now you have your devise gem set up. Woo hoo!
Now, lets hook it up to the appropriate model. In most use cases, this would be the User model, but it could also be assigned to an Admin model or any other, if needed.
In terminal, run:
rails generate devise UserThis command should add a user.rb file to your models folder.

Dont forget to:
rake db:migrateOnce that is done, check your schema to see what your new user table looks like! You should have a bunch of fields like so:

5. Last, utilize the built in helper methods in your UsersController file (that you will need to create!)

If you run rake routes, you will see all of the new routes available to you

6. Let’s test it out! Run rails s and get your server going.
Visit http://localhost:3000/users/sign_up and voila! You should see a simple sign up/log in page rendered for you.

Play around with the different pages and make changes as you like. You have the ability to set different password character minimums and action permissions.
Devise is also handy when you want to allow users the ability to only edit their own posts and comments.
If you run into any issues, be sure to check out the great documentation on github here!
