I 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
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
and put it in
web.php
and delete it fromroutes/admin.php
note that i added
'/admin
‘ to the code so that every thing pointing to /admin will work just finehope this help if it didn’t work please keep me posted
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
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
Thus when this section executes:
For more details please have a look: https://laravel.com/docs/7.x/authentication#included-authenticating
I hope this helps. Thank you.