我知道这是laravel 5.2的一个常见问题,但我一直在寻找数小时而且无法解决这个问题 .
目前,我想通过登录表单进行简单的身份验证调用 . 我正在使用附加到laravel AuthController的自定义数据库 . 我有一个表单发布到auth / login并正确验证,因为它重定向回所需的路由,即主页 . 但是,当我尝试在该路由上调用Auth :: check()时,它总是返回false,就好像它是未定义的一样 . 现在我完全知道必须使用'web'中间件来保存会话数据 . 我已经尝试过这里所说的Laravel 5.2 Auth not Working,遗憾的是它没有为我解决 . 然后我意识到我正在使用版本5.2.32的laravel,这意味着我没有't even need to define ' web'中间件,因为它默认使用通过版本5.2.27,表示一旦删除Web中间件就能收到错误消息 .
一整天这个问题一直困扰着我,身份验证经历了完美和重定向 . 但是,一旦我重定向,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本身,它基本上是默认控制器taht自带的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://stackoverflow.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());并将其替换为,返回($ plain == $ user-> getAuthPassword());在EloquentUserProvider.php中,因为我的密码此刻没有哈希值,一旦测试完成就会被放回 . 我知道这不会影响手头的问题,但我知道为什么我不能在路线中引用Auth
编辑:这是我们正在验证的数据库 . 我们有用户名,密码,甚至是文档中指出的remember_token https://laravel.com/docs/5.2/authentication#introduction-database-considerations
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 回答
所以我最终弄清楚了解决方案,我只是希望这不会被投票 . 但是哦,在我的模型上,我从未表示过主键,因此在检索用户时这是不可能的 .
为了解决这个问题,我只需要这条线