我正在尝试使用laravel进行登录,但是当我使用Auth :: Attempt ........时总是返回false。我已多次查看代码,但仍无法找到错误。可以指导我吗?谢谢:))
型号:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class Users extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'users';
}
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\Users as user;
class UserController extends Controller
{
public function PreLogin(Request $request){
$validator = Validator::make($request->all(),[
'EmailLogin' => 'require|email',
'PasswordLogin' => 'require|min:6'
]);
if($validator->fails()){
return response()->json([
'success' => false,
'error' => $validator->errors()->toArray()
]);
}else{
return $this->Login($request);
}
}
private function Login(Request $request){
if(Auth::attempt(['email' => $request->EmailLogin, 'password' => $request->PasswordLogin])){
return response()->json([
'success' => true
]);
}else{
return response()->json([
'success' => false,
'error' => 'not login'
]);
}
}
}
auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Users::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
答案 0 :(得分:0)
在您使用Auth :: Attempt的路线上使用 auth 中间件