skip to Main Content

enter image description hereI am working on a Laravel admin panel that loops back to admin panel when i input the crenditials. The goal is to navigate to th edashboard. Laravel vrsion is 7x. I am working on it from the localhost/xampp. I double checked the database connection in the relevant files, env, database.db. it marches the database in phpmyadmn. Thank you in advance for the suggestions

N/B It has no error to trace

routes/admin.php

<?php
Route::group(['prefix'  =>  'admin'], function () {

    Route::get('login', 'AdminLoginController@showLoginForm')->name('admin.login');
    Route::post('login', 'AdminLoginController@login')->name('admin.login.post');
    Route::get('logout', 'AdminLoginController@logout')->name('admin.logout');

    Route::group(['middleware' => ['auth:admin']], function () {

    Route::get('/', function () {
        return view('admin.dashboard.index');
    })->name('admin.dashboard');

});
});

appHttpControllersAdminLoginContoller.php

<?php

namespace AppHttpControllersAdmin;

use IlluminateHttpRequest;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use Auth;


class LoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Where to redirect admins after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }

    /**
     * @return IlluminateContractsViewFactory|IlluminateViewView
     */
    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    /**
 * @param Request $request
 * @return IlluminateHttpRedirectResponse
 * @throws IlluminateValidationValidationException
 */
public function login(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(route('admin.dashboard'));
    }
    return back()->withInput($request->only('email', 'remember'));

}

/**
 * @param Request $request
 * @return IlluminateContractsViewFactory|IlluminateViewView
 */

public function logout(Request $request)
{
    Auth::guard('admin')->logout();
    $request->session()->invalidate();
    return redirect()->route('admin.login');
}
}

appExceptionsHandler.php


<?php

namespace AppExceptions;

Use IlluminateSupportArr;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use Throwable;
use IlluminateAuthAuthenticationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  Throwable  $exception
     * @return void
     *
     * @throws Exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Throwable  $exception
     * @return SymfonyComponentHttpFoundationResponse
     *
     * @throws Throwable
     */
    public function render($request, Throwable $exception)
    {
        return parent::render($request, $exception);
    }

    /**
 * @param IlluminateHttpRequest $request
 * @param AuthenticationException $exception
 * @return IlluminateHttpJsonResponse|IlluminateHttpRedirectResponse|SymfonyComponentHttpFoundationResponse
 */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['message' => $exception->getMessage()], 401);
        }
        $guard = Arr::get($exception->guards(), 0);
        switch($guard){
            case 'admin':
            $login = 'admin.login';
            break;
            default:
            $login = 'login';
            break;
        }
        return redirect()->guest(route($login));
    }
}

appHttpMiddlewareRedirectAuthenticated.php


<?php

namespace AppHttpMiddleware;

use AppProvidersRouteServiceProvider;
use Closure;
use IlluminateSupportFacadesAuth;

class RedirectIfAuthenticated
{
   /**
 * Handle an incoming request.
 *
 * @param  IlluminateHttpRequest  $request
 * @param  Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    switch($guard){
        case 'admin':
            if (Auth::guard($guard)->check()) {
                return redirect('/admin');
            }
            break;
        default:
            if (Auth::guard($guard)->check()) {
                return redirect('/');
            }
            break;
    }
    return $next($request);
}
}

routes/web.php

<?php

use IlluminateSupportFacadesRoute;
require 'admin.php';

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| 
|
*/

Route::get('/', function () {
    return view('welcome');
});


Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sarliam-shop
DB_USERNAME=root
DB_PASSWORD=''

database.php

 'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'sarliam-shop'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

2

Answers


  1. i don’t know why this is happening exactly but i think laravel is confused due to a problem in using the admin.php routes so ,

    i suggest you take this part of code

        Route::group(['middleware' => ['auth:admin']], function () {
    
        Route::get('/admin', function () {
            return view('admin.dashboard.index');
        })->name('admin.dashboard');
    
    });
    

    and put it in web.php and delete it from routes/admin.php
    note that i added '/admin‘ to the code so that every thing pointing to /admin will work just fine
    hope this help if it didn’t work please keep me posted

    Login or Signup to reply.
  2. I found out your problem lies in the password column as it does not have a hashed value:

    A hashed value appears to be somewhat like this:$2y$10$ZQcgqgmFuqwQcZW7GYQsR.KmGxmw6mXDCwyKRHksw039IhU34A49W

    enter image description here

    Solution:

    Check the method you are using to register admin. Be it seeder or any other procedure, verify if it has the following method called to hash the password string

     Hash::make($data['password'])
    

    Thus when this section executes:

       Auth::guard('admin')->attempt([
        'email' => $request->email,
        'password' => $request->password]);
    

    The user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the password value passed to the method via the array.

    For more details please have a look: https://laravel.com/docs/7.x/authentication#included-authenticating

    I hope this helps. Thank you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search