问题描述
我正在使用Laravel管理面板,当我输入凭证时,该面板会循环回到管理面板.目标是导航到控制台. Laravel版本是7倍.我正在从localhost/xampp处理它.我仔细检查了相关文件 env , database.db 中的数据库连接.它使数据库进入phpmyadmn.预先感谢您的建议
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 没有错误可以跟踪
routes/admin.php
<?php
Route::group(['prefix' => 'admin'], function () {
Route::get('login', 'Admin\LoginController@showLoginForm')->name('admin.login');
Route::post('login', 'Admin\LoginController@login')->name('admin.login.post');
Route::get('logout', 'Admin\LoginController@logout')->name('admin.logout');
Route::group(['middleware' => ['auth:admin']], function () {
Route::get('/', function () {
return view('admin.dashboard.index');
})->name('admin.dashboard');
});
});
app \ Http \ Controllers \ Admin \ LoginContoller.php
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
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 \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showLoginForm()
{
return view('admin.auth.login');
}
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Validation\ValidationException
*/
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 \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function logout(Request $request)
{
Auth::guard('admin')->logout();
$request->session()->invalidate();
return redirect()->route('admin.login');
}
}
app \ Exceptions \ Handler.php
<?php
namespace App\Exceptions;
Use Illuminate\Support\Arr;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Auth\AuthenticationException;
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 \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
/**
* @param \Illuminate\Http\Request $request
* @param AuthenticationException $exception
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
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));
}
}
app \ Http \ Middleware \ RedirectAuthenticated.php
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $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 Illuminate\Support\Facades\Route;
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
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'),
]) : [],
],
推荐答案
我发现您的问题出在password
列中,因为它没有哈希值:
I found out your problem lies in the password
column as it does not have a hashed value:
散列值似乎有点像这样:$2y$10$ZQcgqgmFuqwQcZW7GYQsR.KmGxmw6mXDCwyKRHksw039IhU34A49W
A hashed value appears to be somewhat like this:$2y$10$ZQcgqgmFuqwQcZW7GYQsR.KmGxmw6mXDCwyKRHksw039IhU34A49W
解决方案:
检查用于注册管理员的方法.无论是播种机还是任何其他过程,请验证其是否具有调用以下方法对密码字符串
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'])
因此执行本节时:
Auth::guard('admin')->attempt([
'email' => $request->email,
'password' => $request->password]);
有关更多详细信息,请查看: https://laravel.com /docs/7.x/authentication#included-authenticating
For more details please have a look: https://laravel.com/docs/7.x/authentication#included-authenticating
我希望这会有所帮助.谢谢你.
I hope this helps. Thank you.
这篇关于尽管凭证正确,但Laravel 7X管理员/登录循环回到管理员/登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!