我使用Auth进行登录验证,Auth::attempt 验证通过后,页面进行跳转到用户中心,但是经过中间Auth::check()进行验证,就无法验证通过..
不知道什么问题?求指教..
{ {
路由文件
// 后台
Route::group(['namespace' => 'Backend', 'prefix' => 'Backend', 'middleware' => ['auth.admin']], function () {
Route::get('center', 'CenterController@index');
});
// 登录
Route::group(['middleware' => ['web']], function () {
// 后台登录
Route::get('Backend/login', 'Backend\MemberController@login');
Route::post('Backend/login', 'Backend\MemberController@in');
Route::post('Backend/logout', 'Backend\LoginController@logout');
});
} }
这个是后台中间件..
/**
@abstract 后台登录前中间件
*/
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;class AuthAdmin {
/**
Handle an incoming request.
*@param \Illuminate\Http\Request $request
@param \Closure $next
@return mixed
*/public function handle($request, Closure $next) {
if (!Auth::guard('admin')->check()) {
if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('Backend/login'); }
}
return $next($request);
}
}
登录页面.验证已经通过..但就是上面中间件那里check()就不行.查看user()为空.
<?php
/**
@abstract 后台登录控制器
*/
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Validator;class MemberController extends Controller {
/**
@abstract 登录页面
*/public function login() {
return view('backend.member.index');
} /**
@abstract 登入
*/public function in(Request $request) {
if ($request->ajax()) {
$validator = Validator::make($request->all(), [ 'username' => 'required|max:50', 'password' => 'required|max:50', ]); if ($validator->fails()) { return response()->json($validator->errors()); } if (!Auth::guard('admin')->attempt(['username' => $request->input('username'), 'password' => $request->input('password'), 'is_using' => 'On'], $request->input('remember_me'))) { // The user is not active, not suspended, and exists. return response()->json('认证用户失败!!'); } return response()->json(TRUE);
}
return;
} /**
@abstract 忘记密码
*/public function forget(Request $request) {
if ($request->ajax()) {
}
return view('backend.member.forget');
} /**
@abstract 注销用户
*/public function logout() {
Auth::logout();
return;}
}