Authentication

Uchi assumes as little as possible about your application, which means authentication is up to your code. This, however, also means you can continue to use whatever authentication mechanism you’ve already implemented, be it HTTP Basic Authentication, Devise, Rails’ authentication generator, or something entirely different.

Vanilla

Generally speaking, you’ll want to restrict access in a before_action in your Uchi::ApplicationController. Add an Uchi::ApplicationController at app/controllers/uchi/application_controller.rb:

module Uchi
  class ApplicationController < Uchi::Controller
    # Note that Uchi::ApplicationController does not inherit your existing
    # ApplicationController, so you need to include your authentication setup
    # here as well.
    include ExistingAuthentication

    before_action do
      # This assumes you have a `current_user` method from your main
      # application.
      unless current_user&.admin?
        # Handle unauthorized access
        flash[:error] = "Access denied"
        redirect_to(main_app.login_url)
      end
    end
  end
end

Basic authentication

The simplest way to lock your admin interface down is to enable basic authentication in your Uchi::ApplicationController:

module Uchi
  class ApplicationController < Uchi::Controller
    http_basic_authenticate_with name: "uchi", password: "rocks"
  end
end

See HTTP Basic authentication for more advanced examples.

Note

This does not add a logged in user to Uchi::Context.

Rails’ authentication generator

Tip

While the examples here are based on Rails’ authentication generator the patterns should work for pretty much all authentication systems with some modifications.

To restrict access to all Uchi-controllers, add an Uchi::ApplicationController at app/controllers/uchi/application_controller.rb:

module Uchi
  class ApplicationController < Uchi::Controller
  end
end

Include the Authentication module and provide a new_session_path method, which Rails’ Authentication uses to determine where to send your unauthorized users.

Tip

To use routes from your application inside the Uchi namespace you need to prefix them with main_app.

And finally expose the currently logged in user from the session to Uchi’s context.

module Uchi
  class ApplicationController < Uchi::Controller
    include Authentication

    before_action {
      uchi_context.user = Current.session&.user
    }

    def new_session_path
      main_app.new_session_path
    end
  end
end

Devise

If your main application uses Devise for authentication, you can use the existing helper methods from Devise in Uchi.

Assuming your devise model is ‘User’, add an Uchi::ApplicationController at app/controllers/uchi/application_controller.rb, and use authenticate_user! from Devise to verify login, and a custom before_action to restrict access to the admin area:

module Uchi
  class ApplicationController < Uchi::Controller
    before_action :authenticate_user!
    before_action :require_admin

    private

    def require_admin
      return if current_user.admin?

      flash[:notice] = "Access denied"
      redirect_to main_app.new_user_session_url
    end
  end
end

API

The API inherits from Uchi::ApplicationController, so the above also protects it. See API authentication to authenticate the API differently.

Current user

Uchi exposes a Uchi::Context instance which includes the currently logged in user.

To expose the current user to Uchi, set the user value in the context in before_action:

before_action do
  uchi_context.user = current_user
end

Uchi doesn’t care what type the value is, it can be a fully fledged User object, or perhaps a String with a username, it’s up to you.