0

I'm hoping someone can help me figure out what's going wrong with an auth I'm adding into an app. The app itself uses normal authentication (username/password) using devise. Once a user is logged in, they are supposed to be able to connect to their email provider using OAuth (currently working on Microsoft365) which is what I'm working on right now.

I'm using the omniauth-oauth2 gem to implement the authentication, and as far as I can tell everything is working - the user is presented the MS login page, a POST request is returned with the token. All looks good, however when the callback comes in, the original user session is completely empty, and when I then redirect the user back to another page, they are kicked back to the login screen.

My callback is really simple at the moment

def auth_callback
    # data = request.env['omniauth.auth']
    if has_permission?
      flash[:notice] = 'YAY!'
      redirect_to root_url
    else
      flash[:alert] = "Could not authenticate with your mailbox"
      redirect_to new_mailbox_path
    end
  end

I'm not doing anything with the data, and I know in a normal authentication system this is where I'd be getting/creating the user and signing in - but the user should already be signed in. And since they weren't created by oauth, I can't really use the MS user ID to find them anyway.

The only solution I can think of, is to send the user ID in the state when requesting auth from MS, so that when it is returned I can match that up and the "sign in" the user again but that feels wrong.

I am already bypassing CSRF so it shouldn't be that wiping it out

  skip_before_action :verify_authenticity_token, only: %i[auth_callback]
  skip_before_action :authenticate_user!, only: %i[auth_callback]

Is there anything else that would cause the session to be lost like this?

PaReeOhNos
  • 4,338
  • 3
  • 30
  • 41

0 Answers0