mardi 22 août 2023

Tryin to get Users to log in to Microsoft Active directory on my rails app using Omniauth gem

i keep getting this from the console

Started POST "/auth/microsoft_graph" for ::1 at 2023-08-22 11:57:05 +0100
D, [2023-08-22T11:57:05.241303 #14844] DEBUG -- omniauth: (microsoft_graph) Request phase initiated.

without any further response or action on the console. i am not receiving any tokens and also not being redirected to sign in like I should. I have tried it with omniauth-facebook gem to be sure and it's the same response. I believe I'm missing a step but I don't know what it is

i have installed these gem

gem 'omniauth-oauth2'
gem 'omniauth-microsoft_graph'
gem "omniauth-rails_csrf_protection"

then I created this button to sign in:

<%= button_to "Connect microsoft_graph", "/auth/microsoft_graph", method: :post, class: "mt-4 p-2 btn bg-blue-400 rounded" %>

this is the omniauth.rb located in the initializers folder:

Rails.application.config.middleware.use OmniAuth::Builder do
   provider :microsoft_graph, "a47d3c7d-xxxxxxxx", "YOV8Q~9z~xxxxxxxxxxxx"
end

I also created the route in the route.rb file

get "/auth/microsoft_graph/callback", to: "omniauth_callbacks#microsoft"

finally created the controller file and action and microsoft_account model referencing user

class OmniauthCallbacksController < applicationController
    def microsoft
        Rails.logger.info auth
        # render plain: "success"
        current_user.microsoft_accounts.create(

            name: auth.info.name,
            username: auth.info.name,
            token: auth.credentials.token,
            secret: auth.credentials.secret

        )

        redirect_to root_path, notice: "Successfully connected your account"
    end
 def auth
        request.env['omniauth.auth']
    end

Aucun commentaire:

Enregistrer un commentaire