I had the same issue ("Omniauth returns a 401 unauthorized") but the cause was different.
In my User#from_omniauth
I do this
where(provider: auth.provider, uid: auth.uid).first_or_create
which means that it creates a new user if one was not already there. Also, my User
is devise :confirmable
, and unconfirmed User can not sign in. That is why the authentication failed.
There would have been an error message stating this, but in the view that I was redirected to there was no:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
So, the solution was to add this to the method in the CallbacksController:
def github
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
@user.skip_confirmation!
@user.save
#...
skip_confirmation is a devise method and it does this on the User:
self.confirmed_at = Time.now.utc
so, alternatively this could have been done in User.from_omniauth