Search code examples
laravelauthenticationadminlte

AdminLTE multiple auth in laravel


I'm trying to implement multi auth guards with AdminLTE in Laravel (version 6.x) and I have trouble to pass the guard (as $url variable) in login/register forms. My API works fine without AdminLTE, and it works fine with normal user with AdminLTE. I can display Admin login/register forms by taping "/login/admin" and "register/admin" in the address bar, but when I click login/register button I get "404 Not Found" page and in the address bar it displays "/login/$url"

I have changed the login.blade.php and register.blade.php in the following directory:

vendor/.../view/login.blade.php and vendor/.../view/register.blade.php

LoginController.php:

public function adminLogin(Request $request)
{
    $this->validate($request, [
        'email'   => 'required|email',
        'password' => 'required|min:6'
    ]);

    if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
        return redirect()->intended('/admin');
    }

    return back()->withInput($request->only('email', 'remember'));
}

RegisterController.php:

protected function createAdmin(Request $request)
{
    $data = $request->all();

    $this->validateAdmin($data)->validate();

    $admin = Admin::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    return redirect()->intended('login/admin');
}

routes/web.php:

Route::post('/login/admin', 'Auth\LoginController@adminLogin');
Route::post('/register/admin', 'Auth\RegisterController@createAdmin');

login.blade.php:

@isset($url)
  <form action="{{ url('login/$url') }}" method="post">
@else 
  <form action="{{ url(config('adminlte.login_url')) }}" method="post">
@endisset

register.blade.php:

@isset($url)
  <form action="{{ url('register/$url') }}" method="post">
@else 
  <form action="{{ url(config('adminlte.register_url')) }}" method="post">
@endisset

Solution

  • You're hard coding the $url as string as oppose to using it as a variable

    This is due to using single quotes in the blade view files in the url() helper function argument

    Just change it to double quotes

    login.blade.php:

    @isset($url)
      <form action="{{ url("login/$url") }}" method="post">
    @else
      <form action="{{ url(config('adminlte.login_url')) }}" method="post">
    @endisset
    

    register.blade.php:

    @isset($url)
      <form action="{{ url("register/$url") }}" method="post">
    @else 
      <form action="{{ url(config('adminlte.register_url')) }}" method="post">
    @endisset
    

    Changing files in vendor folder is redundant because your changes will be overridden on composer update/install

    From the docs

    1. If you have published and modified the default master, page views or login views, you will need to update them too.

    Option 1:

    • Make a copy of the views you modified.
    • Publish the views again, using
    php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\AdminLteServiceProvider" --tag=views
    
    • Redo the modifications you did.

    Hope this helps