Laravel 5.2 Auth 登录有效,Auth::check 失败
Posted
技术标签:
【中文标题】Laravel 5.2 Auth 登录有效,Auth::check 失败【英文标题】:Laravel 5.2 Auth login works, Auth::check fails 【发布时间】:2016-09-27 15:02:27 【问题描述】:我知道这是 laravel 5.2 的一个常见问题,但我一直在寻找几个小时却无法解决这个问题。
目前,我想通过登录表单进行简单的身份验证调用。我正在使用附加到 laravel AuthController 的自定义数据库。我有一个表单可以发布到 auth/login 并在它重定向回所需的路由主页时正确验证。但是,当我尝试在该路由上调用 Auth::check() 时,它总是返回 false,就好像它未定义一样。现在我完全意识到必须为我的路由使用“网络”中间件来保存会话数据。我已经尝试过,如Laravel 5.2 Auth not Working 所示,遗憾的是它并没有为我解决。然后我意识到我使用的是 laravel 的 5.2.32 版本,这意味着我什至不需要定义“web”中间件,因为它默认使用通过了 5.2.27 版本,如网络一旦接收错误消息的能力所示中间件被移除。
这个问题一直困扰着我一整天,身份验证完美通过并重定向。但是,一旦我重定向,Auth 就不起作用了。 如果我至少能对这段代码有新的看法,那将不胜感激。
******routes.php************************************
// Route::group not necessary after version 5.2.27 of laravel on by default, simply here for display purposes.
Route::group(['middleware' => 'web'], function ()
Route::get('/', function()
if(Auth::check())
echo "logged in";
return view('pages.home');
);
Route::get('channels/channel', function($channel)
return view('pages.channels', ['channel' => $channel] );
);
Route::get('events', function()
return view('pages.events');
);
Route::get('news', function()
return view('pages.news');
);
Route::get('contact', function()
return view('pages.contact');
);
// Login Form routes
Route::get('login', function()
return view('pages.login');
);
// Just Get login working, instead of using Route::auth()
Route::post('auth/login', 'Auth\AuthController@login');
);
*****login.blade.php****************************** 我的表单
@extends('layouts.master')
@section('content')
<div class="text-center col-md-4 col-md-offset-4">
<h1>Login</h1>
<ul>
@foreach($errors->all() as $error)
<li> $error </li>
@endforeach
</ul>
!! Form::open(array('url' => '/auth/login', 'class' => 'form')) !!
<div class="form-group">
!! Form::label('Username') !!
!! Form::text('username', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your username')) !!
</div>
<div class="form-group">
!! Form::label('Password') !!
!! Form::text('password', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your password')) !!
</div>
<div class="form-group">
!! Form::submit('Login',
array('class'=>'btn btn-primary')) !!
</div>
!! Form::close() !!
<div>
Don't have an account? <a href="">Sign up.</a>
</div>
</div>
@endsection
************New_Client.php ************************自定义数据库模型
namespace App;
// http://laravel-recipes.com/recipes/11/changing-your-authentication-model
// model change to https://laravel.com/docs/5.0/upgrade
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class New_Client extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'new_client';
/*Turn off timestamps, as we do not have them*/
public $timestamps = false;
use Authenticatable, Authorizable, CanResetPassword;
*注意我已经在 config/auth.php 中的 provider->users->model 下附加了这个模型
最后是 AuthController.php 本身,它基本上是与 laravel 一起打包的默认控制器。但是 loginPath、redirectPath 和 username 变量只改变了。
<?php
namespace App\Http\Controllers\Auth;
use App\New_Client;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
// temp
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Redirect user after failed login
*/
protected $loginPath = '/login';
// Override email required field with this
//https://***.com/questions/28584531/modify-existing-authorization-module-email-to-username
protected $username = 'username';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
return Validator::make($data, [
'username' => 'required|max:255',
'password' => 'required|min:6',
]);
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
return User::create([
'username' => $data['name'],
'password' => bcrypt($data['password']),
]);
*这完全无关,但我也删除了该行 返回 $this->hasher->check($plain, $user->getAuthPassword()); 并将其替换为, return ($plain == $user->getAuthPassword()); 在 EloquentUserProvider.php 中,因为我的密码目前没有经过哈希处理,一旦测试完成就会放回去。我知道这不会影响手头的问题,但我知道为什么我不能在路由中引用 Auth
编辑:这是我们正在验证的数据库。我们有用户名、密码,甚至是文档https://laravel.com/docs/5.2/authentication#introduction-database-considerations中指出的remember_token
CREATE TABLE `new_client` (
`client_idx_NW` int(11) NOT NULL,
`ID` int(11) NOT NULL DEFAULT '450',
`username` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(20) NOT NULL DEFAULT 'John',
`surname_name` varchar(20) NOT NULL DEFAULT 'Smith',
`organization_name` varchar(20) NOT NULL DEFAULT 'Sony',
`phone_number` varchar(20) NOT NULL DEFAULT '6476231234',
`address_street` varchar(256) NOT NULL DEFAULT '230-140 Cresent Road',
`city` varchar(20) NOT NULL DEFAULT 'Burlington',
`province` varchar(20) NOT NULL DEFAULT 'Ontario',
`country` varchar(20) NOT NULL DEFAULT 'Canada',
`postal_code` varchar(20) NOT NULL DEFAULT 'l7l4C3',
`email_address` varchar(20) NOT NULL DEFAULT 'JohnSmith@sony.ca',
`credit_card_number` varchar(20) NOT NULL,
`expiry_date` date NOT NULL,
`security_code` varchar(20) NOT NULL,
`accepted` tinyint(1) NOT NULL,
`remember_token` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
问候
【问题讨论】:
【参考方案1】:所以我最终找到了解决方案,我只是希望这没有被否决。但是哦,好吧,在我的模型上,我从未指定主键,因此在检索用户时这是不可能的。
为我修复它只需要这条线
protected $primaryKey = 'client_idx_NW';
【讨论】:
以上是关于Laravel 5.2 Auth 登录有效,Auth::check 失败的主要内容,如果未能解决你的问题,请参考以下文章
异常页面(布局)上的 Laravel 5.2 Auth::check()