Troubleshooting OAuth for Rails App
To begin — there are so many clean explanations for how to set up an Omniauth login for your Rails App:
I am here to walk you through all of the ways I got lost between a clean cut homework assignment and publishing my final app.
has_secure_password is ✨
There are many ways to accidentally fail validations. One of the most common ways, was I would set up too many validations leading to a conflicting rules. Let the rails class method has_secure_password
do all of the work for you (reference link):
- install the
bcrypt
gem - add
has_secure_password
to your User model
The gem will automatically run these validations:
- Password must be present on creation
- Password length should be less than or equal to 72 bytes
- Confirmation of password (using a
XXX_confirmation
attribute)
No need to add additional custom password validations. Just assign a secure password upon User’s omniauth creation (again view a great full tutorial here) with user.password = SecureRandom.hex
Separate the Omniauth method
Let yourself be free and separate your concerns by creating two different methods in your Sessions Controller: create
and omniauth
create
— will still be your custom method for direct sign-ups and sign-insomniauth
— references yourfrom_omniauth(auth)
User class method
Since the params of each session type are slightly different, it makes more sense to separate them into different controller methods.
Debug! Debug! Debug!
The byebug gem is your friend. Use it early and often to understand the params coming in and how your models/controllers are processing it all.
If you’re stuck at a nil
wall during session creation — take advantage of save!
to get a full list of errors on which validations failed.
Even if everything’s working, take a moment to dive into the omniauth params to be in-the-know of what info you’ve been given.
The logic of building an omniauth login for your rails app isn’t always a clear path. With the tools above, I hope your app building process goes smoothly!