Laravel 5 Auth :: attempt总是返回false,即使我输入了正确的数据? (LARAVEL)

时间:2018-11-16 18:11:43

标签: php laravel login

我在LARAVEL应用程序上创建了自定义登录系统,直到昨天一切都运行良好。当我输入与数据库中用户相同的登录名电子邮件和密码时,系统将我重定向回去。我不知道是什么问题,我清除了缓存和所有内容,但仍然无法正常工作。如果有人知道答案,我将不胜感激。

  

登录页面图片:

https://imgur.com/a/xuEWQZg

  

存储在数据库丝毫种子中的用户数据:

https://imgur.com/0mJOviV

`

  

dd函数具有从登录表单发送的数据:

`https://imgur.com/a/D2k8Ztn

主控制器

  function checklogin(Request $request)
    {
     $this->validate($request, [
      'email'   => 'required|email',
      'password'  => 'required|alphaNum|min:3'
     ]);

     $user_data = array(
      'email'  => $request->get('email'),
      'password' => $request->get('password')
     );
     if(Auth::attempt($user_data))
     {

      return redirect('');
     }
     else
     {
      return back()->with('error', 'you typed wrong data');
     }

    }

登录页面

@extends('layout')

@section('content')
<div class="container2">  
<div class="container">
        <div class="card card-container">

            <h1 style="text-align: center;">Admin login</h1>


   @if(isset(Auth::user()->email))
    <script>window.location="/main/successlogin";</script>
   @endif

   @if ($message = Session::get('error'))
   <div class="alert alert-danger alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $message }}</strong>
   </div>
   @endif

   @if (count($errors) > 0)
    <div class="alert alert-danger">
     <ul>
     @foreach($errors->all() as $error)
      <li>{{ $error }}</li>
     @endforeach
     </ul>
    </div>
   @endif

   <form method="post" action="{{ url('/main/checklogin') }}">
    {{ csrf_field() }}
    <div class="form-group">
     <label>Unesi email</label>
     <input type="email" name="email" class="form-control" />
    </div>
    <div class="form-group">
     <label>Unesi šifru</label>
     <input type="password" name="password" class="form-control" />
    </div>
    <div class="form-group">
     <input type="submit" name="login" class="btn btn-primary" value="Login" />
    </div>
   </form>
             </div><!-- /card-container -->
    </div><!-- /container -->
 </div><!-- /container -->
@endsection

路线

Route::get('/', 'MainController@successlogin')->name('main');
Route::get('/admin', 'MainController@index');
Route::post('/main/checklogin', 'MainController@checklogin');
Route::get('main/logout', 'MainController@logout');

用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'lastname', 'level', 
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

function checklogin(Request $request)
    {
     $this->validate($request, [
      'email'   => 'required|email',
      'password'  => 'required|alphaNum|min:3'
     ]);

     $user_data = array(
      'email'  => $request->get('email'),
      'password' => bcrypt($request->get('password'))
     );
     if(Auth::attempt($user_data))
     {

      return redirect('');
     }
     else
     {
      return back()->with('error', 'you typed wrong data');
     }

    }