Authenticate With Username or Email
Today I learned a way to implement a login form that accepts email or username as the login. I've been on the other side of a form like this many times, but had never written one myself.
Here's one solution, with ActiveRecord:
User.where('username = ? or email = ?', "jwworth", "jake@example.com")
title
or slug
are represented by the same parameter, and either can be nil
.
login = params.fetch('username_or_email')
User.where('username = ? or email = ?', login, login)
Tweet