Setting Up User Authentication With Devise
I am starting work on a new application . I wanted it to have multiple users, but I didn't want to reinvent the wheel, so I decided to use a pre-existing gem called Devise. I am on a Mac using Ruby 2 , 3 and Rails 4.
Add Devise
If you want to run Bootstrap and/or Simple Form on your app, please follow the instructions on my blog post Bootstrap Your App before proceeding.
I selected Devise simply because it was the most popular Rails Authentication gem at The Ruby Toolbox and I wanted to try it out. Add the
devise
gem to your Gemfile:
Next run install:
Taking a look at the Getting Started documentation, I followed the next steps. Run the generator:
Doing this also resulted in some intructions from Devise that I followed. I first defined the default url options in
config/environments/development.rb
by adding this line:
I also add flash message display in my
app/views/layouts/application.html.erb
file by adding these lines in the <body>
tag:
Next, copy Devise views (for customization) to your app by running:
Next create a user model and configure it with default Devise modules:
Next create the database and table:
Add sign in, sign out and sign up links to your layout template
These instructions are part of the Devise documentation.
First add sign in/out links, so the appropriate one will show up depending on whether the user is already signed in. Create file
app/views/devise/menu/_login_items.html.erb
with the following code:
Add sign up links that are substituted with something else useful if the user is already signed in. Create file
app/views/devise/menu/_registration_items.html.erb
with the following code:
Then render these templates in your
app/views/layouts/application.html.erb
file. You could put them in the middle of a navigation bar, something like this:
Then add some CSS to style this into a proper navigation bar. And continue on to make the rest of your application. Before you get too far, you might want to deploy to Heroku just to make sure everything is working with the users.
Comments
Post a Comment