Search code examples
ruby-on-railsapiauthenticationdeviseswagger

Rails: make authentication to swagger API via devise or other


My goal is log in to Swagger API to get access to endpoints. With auth token in future.

I tryed to put Devise line in ApplicationController:

before_action :authenticate_user!

But when load http://localhost:3000/api-docs/index.html - system allows to access the API without authentication.

I tryed to uncomment in /config/initializers/rswag-ui.rb:

  c.basic_auth_enabled = true
  c.basic_auth_credentials 'username', 'password'

but it not this.

Also it did not find the right way to realize Bearer Authentication from official Swagger.io documentation. I do not know is it connected to Devise and do I need to use it.

Which way is good to log in to API via login and password of my Users? Thank you.


Solution

  • With rswag-ui you should only need to uncomment those lines. No other authentication is needed (like devise).

    # /config/initializers/rswag-ui.rb
    
    c.basic_auth_enabled = true
    c.basic_auth_credentials 'username', 'password'
    
    

    I just did it in my project and it worked straight away, so thought I might as well answer.