Authentication
Authentication is the process of verifying the identify of a user. It is handled by Warden, and comes with a basic setup with helpers for authenticating routes.
User model
Once you have set up authentication, you will see that a model was created for storing user's account information. This model is located at app/models/user.rb
.
The default columns in the model are username
and encrypted_password
.
Password encryption
For security, passwords are hashed using the BCrypt encryption algorithm. The encryption mechanism is already built into the user model.
When creating a new user, it is essential that the encrypted password is stored in the database, and not the unencrypted one.
Creating a new user
To create a new user:
Although this appears to be storing the unencrypted password in the database, the password=
instance method will first encrypt the password.
Confirmations
For registering (and numerous other purposes), you might want to require the user to enter some information twice for verification. The user model already comes with this functionality.
For example, if we require the password to be entered twice:
The confirmation method name will always be <name of field>_confirmation
.
You can also pass multiple field symbols to the with_confirm
method:
Authentication controller
Unless you used a specific flag during the authentication setup, an authentication controller will have been created.
This controller handles the routes for logging in or authenticating users, logging out users, and what to do when a page requiring authentication is accessed by an unauthenticated user.
Authentication helpers
Three helper methods are defined during the authentication setup. Both are accessible in all controllers.
authenticate
- Permits an action to be performed if done by an authenticated user. If the user is not authenticated, they will be redirected.This is typically placed at the start of a route handler (as seen in this example):
current_user
- The user object of the currently authenticated user (ornil
if no user is authenticated). Since this isn't always an explicit boolean value, it is suggested to use theauthenticated?
method for conditionals. For example:authenticated?
aliaslogged_in?
- Checks whether or not a user is currently authenticated or logged in. This should be used for conditionals rather thancurrent_user
, since it is always an explicit boolean value. This can be used in views for conditional displays.
Last updated