Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

1.8 KB
Raw Blame
---
title: Admin Panel Authentication
sidebarTitle: Authentication
description: How to customize the Spree admin panel authentication
canonical: "https://spreecommerce.org/docs/v5/developer/admin/authentication"
---

Spree allows you to use a different model for the admin panel than the storefront (that's the default since Spree 5.2).
Here you can find how to customize the admin panel authentication to use a different model.

Let's assume you have an existing `AdminUser` model in your application and you're using Devise for authentication.

In `config/initializers/spree.rb` file, add the following line:

```ruby
Spree.admin_user_class = 'AdminUser'
```

This will tell Spree to use your `AdminUser` model for the admin panel. You will also need to add the following line in that model file:

```ruby
include Spree::UserMethods
```

In your `config/initializers/routes.rb` file, you will need to define devise routes for the 2nd model:

```ruby
Spree::Core::Engine.routes.prepend_routes do
  # Admin authentication
  devise_for(
    Spree.admin_user_class.model_name.singular_route_key,
    class_name: Spree.admin_user_class.to_s,
    controllers: {
      sessions: 'spree/admin/user_sessions',
      passwords: 'spree/admin/user_passwords'
    },
    skip: :registrations,
    path: :admin_user,
    router_name: :spree
  )
end
```

And now in your `lib/spree/authentication_helpers.rb` file, please replace the following lines: 

```diff
     def spree_admin_login_path(opts = {})
-      spree_login_path(opts)
+      new_admin_user_session_path(opts)
     end

     def spree_admin_logout_path(opts = {})
-      spree_logout_path(opts)
+      destroy_admin_user_session_path(opts)
     end
```

Now when attempting to access the admin panel, you will be redirected to the dedicated admin panel login page.

![Admin panel login page](/images/developer/admin/admin_panel_login.png)