This evening, you will be building a User model for a Rails auth system. This project won't end in an app with login/logout functionality, but this is a concept that you will use over and over again.
Refer to the first auth reading and the second auth reading if you get stuck.
rails new app_name --database=postgresql
- Pro-tip: You can
generate
a model and pre-populated migration from the command line by passingrails g model
an options hash- Example:
rails g model User username:string password_digest:string session_token:string
- Example:
- Add the appropriate constraints and indices to the migration and run
rails db:migrate
- Check
schema.rb
to make sure it worked
- Validate the presence of
username
andsession_token
- Validate the presence of
password_digest
and set the error message toPassword can't be blank
- Validate the length of
password
, and setallow_nil
totrue
- Make sure that
ensure_session_token
gets calledbefore_validation
- Write
::find_by_credentials
- This method takes in a
username
and apassword
and returns the user that matches
- This method takes in a
- Write
::generate_session_token
- This is a helper method I like to write that uses
SecureRandom::urlsafe_base64
and returns a 16-digit pseudorandom string
- This is a helper method I like to write that uses
- Write
#reset_session_token!
- This method resets the user's
session_token
and saves the user
- This method resets the user's
- Write
#ensure_session_token
- This method makes sure that the user has a
session_token
set, setting one if none exists
- This method makes sure that the user has a
- Write
#password=
- This method sets
@password
equal to the argument given so that the appropriate validation can happen - This method also encrypts the argument given and saves it as this user's
password_digest
- Remember: you have to add an
attr_reader
forpassword
for the validation to occur!
- This method sets
- Check your model against the solutions.
- Try creating a user in the Rails console
- Make sure your validations work, and also make sure that you're storing
password_digest
as an encrypted string
- Make sure your validations work, and also make sure that you're storing
Example testing code below:
User.all # shows no users
empty_gage = User.new
empty_gage.save # rollback because of failed validations
password_too_short_gage = User.new(username: 'gage', password: 'test')
password_too_short_gage.save # rollback because of failed validations
gage = User.new(username: 'gage', password: 'testing')
gage.save # inserted into Users
User.all #<ActiveRecord::Relation [#<User id: 1, username: "gage"...
User.first.password_digest # shows a string of gibberish that you can't hack
- You have written an important part of a Rails auth system!