Custom login

Rewrite the login page and login logic

Way 1, rewrite the login controller method:

The default login controller uses the class App\Admin\AuthController, which can be modified by configuring the parameter admin.auth.controller.

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use Dcat\Admin\Controllers\AuthController as BaseAuthController;
  4. class AuthController extends BaseAuthController
  5. {
  6. // Custom login view template
  7. protected $view = 'admin.login';
  8. // Rewrite the logic of your landing page
  9. public function getLogin(Content $content)
  10. {
  11. ...
  12. }
  13. ...
  14. }

Way 2, override routing:

In the routing file app/Admin/routes.php, override the routing of the landing page and login logic to implement custom features

  1. Route::group([
  2. 'prefix' => config('admin.prefix'),
  3. 'namespace' => Admin::controllerNamespace(),
  4. 'middleware' => ['web', 'admin'],
  5. ], function (Router $router) {
  6. $router->get('auth/login', 'AuthController@getLogin');
  7. $router->post('auth/login', 'AuthController@postLogin');
  8. });

Implement your own login page and login logic in the getLogin and postLogin methods of your custom router AuthController.

Rewrite lavel authentication

If you don’t use the built-in authentication login logic of Dcat Admin, you can customize the login authentication logic in the following way.

The first thing to do is to define a user provider, which is used to get the user’s identity, e.g. app/Providers/CustomUserProvider.php:

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Contracts\Auth\Authenticatable;
  4. use Illuminate\Contracts\Auth\UserProvider;
  5. class CustomUserProvider implements UserProvider
  6. {
  7. public function retrieveById($identifier)
  8. {}
  9. public function retrieveByToken($identifier, $token)
  10. {}
  11. public function updateRememberToken(Authenticatable $user, $token)
  12. {}
  13. public function retrieveByCredentials(array $credentials)
  14. {
  15. // Use the username and password in $credentials to get user information, and then return the Illuminate\Contracts\Auth\Authenticatable object.
  16. }
  17. public function validateCredentials(Authenticatable $user, array $credentials)
  18. {
  19. // Verifies the user with the username and password in $credentials.
  20. }
  21. }

In the methods retrieveByCredentials and validateCredentials, the incoming $credentials is the array of usernames and passwords submitted by the login page, and you can use $credentials to implement your own login logic

Interface Illuminate\Contracts\Auth\Authenticatable definitions are as follows:

  1. <?php
  2. namespace Illuminate\Contracts\Auth;
  3. interface Authenticatable {
  4. public function getAuthIdentifierName();
  5. public function getAuthIdentifier();
  6. public function getAuthPassword();
  7. public function getRememberToken();
  8. public function setRememberToken($value);
  9. public function getRememberTokenName();
  10. }

Explanation of each method of the above interface reference adding-custom-user-providers

Once you have defined User provider, open app/Providers/AuthServiceProvider.php and register it:

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  5. class AuthServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Register any application authentication / authorization services.
  9. *
  10. * @return void
  11. */
  12. public function boot()
  13. {
  14. $this->registerPolicies();
  15. Auth::provider('custom', function ($app, array $config) {
  16. // Return an instance of Illuminate\Contracts\Auth\UserProvider...
  17. return new CustomUserProvider();
  18. });
  19. }
  20. }

Finally, change the configuration, open config/admin.php, find the auth section to change:

  1. 'auth' => [
  2. 'guards' => [
  3. 'admin' => [
  4. 'driver' => 'session',
  5. 'provider' => 'admin',
  6. ]
  7. ],
  8. // modify the following
  9. 'providers' => [
  10. 'admin' => [
  11. 'driver' => 'custom',
  12. ]
  13. ],
  14. ],

This completes the logic of custom login authentication. Custom login is a complex part of laravel and requires patience to debug step by step.