Laravel auth :: attempt()无效

时间:2016-10-28 04:20:52

标签: php laravel authentication laravel-5.3

我是Laravel的新手并且遇到了登录机制我在这里使用我的自定义登录机制(不使用Laravel身份验证),并且我无法使用身份验证凭据登录。< / p>

我想使用凭据登录,登录后日志应该在login_master中维护并重定向到home。但如果凭据错误则不保持加号,那么它应该重定向回signin.blade.php,但它会重定向到家。

这是代码

create_login_table.php(我的登录表结构)

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLoginTable extends Migration
{
public function up()
{
    Schema::create  ('login_master',function($table)
    {
    $table->increments('login_id',true);
    $table->integer('user_id')->unsigned();
    $table->date('login_date');
    $table->date('login_time');
    });
    // to create foreign key :)

    Schema::table('login_master', function($table) {
        $table->foreign('user_id')->references('user_id')->on('registration_master');
    });

}
public function down()
{
    Schema::dropIfExists('login_master');
}
}

LoginController(用于处理表的控制器)

namespace App\Http\Controllers;
use DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Validator;
class LoginController extends Controller
{
public function loginform()
{
    return view('signin');
}
public function authenticate(Request $request)
{
    $userdata= Input::all();
    $rules= array(
        'email' => 'required|email',
        'password' => 'required| min:6',
    );
    $validator = Validator::make($userdata,$rules);
    if($validator->fails())
    {
        redirect('/signin')->withInput(Input::except('password'))->withErrors($validator);
    }
    else
    {
        $userdata= array(
          'email' => Input::get('email'),
            'password' => Input::get('pwd')

        );
        if(Auth::validate($userdata))
        {
            if(Auth::attempt($userdata))
            {
             $email= $userdata['email'];
              $password= $userdata['password'];
                $live_date=Carbon::now();
                $log_date=$live_date->toDateString();
                $log_time=$live_date->toTimeString();
                $user_id= DB::select('select user_id from registration_master where email= ? and password = ?',[$email, $password]);
                $record= DB::insert('insert into login_master
(user_id, login_date, login_time) values(?, ?, ?)',[$user_id, $log_date, $log_time]);
                echo $email;
                echo $password;
                return Redirect::intended('/');
            }
        }
        else
        {
            Session::flash('error', 'Something went wrong');
            return Redirect::to('signin');
        }
    }
}
public function logout()
{
    Auth::logout();
    return Redirect::to('signin');
}
}

signin.blade.php(登录视图)

<html>
<head>
<title>App Name - @yield('title')</title>
@include('library')
</head>
<body>
@include('header')

<div class="form-content">
<div class="headingstyle form-title">
    <h1 class="heading1">Log in!</h1>
</div>
 <form  method="post" action="/home">
      <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
      <div class="formgroup">
                  <input class="input" type="text" name="uemail"required>
                  <label class="label">Email</label>
              </div>
              <div class="formgroup">
                  <input class="input" type="password" name="pwd"required>
                  <label class="label">Password</label>
              </div>
              <div class="formgroup bottomstyle">
                  <div id="check-awesome" class="form-group checkbox">
                                      <input type="checkbox" id="RememberMeUser" mame="RememberMeUser">
                                          <label for="RememberMeUser">
                                              <span></span>
                                              <span class="check"></span>
                                              <span class="box"></span>
                                              Remember Me
                                          </label>
                                      </div><!--<a href="#" id="login" class="button">Login</a>-->
                                      <input type="submit" value="Login"><br>
                  <a data-dismiss="modal" class="button cancelbtn">Cancel</a><br>
                  <span class="links"> <a class="bottomlink" href="#">Forgot Password?</a></span><br>
                  <span class="links">New User
                  <!--<a class="bottomlink" href="#" data-dismiss="modal" data-toggle="modal" data-target="#id02">Sign Up?</a> -->
                  {{ Html::linkAction('SignUpController@signupform','Sign Up', array(), array('class' => 'bottomlink')) }}
                  </span>
              </div>
 </form>
 </div>
<div>
     @include('footer')  
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要配置app/auth.php或dotenv以使用您自己的自定义模型与Laravel中的Auth Guard帮助程序。在那里,您可以设置要使用的modeltable

否则,您可以实现自己的Auth帮助器/外观以在会话中保存logged_in_user_id。您的自定义身份验证将基本确认传递的用户凭据并将logged_in_user_id存储在会话中。您的注销方法同样会在调用时从会话中清除此ID。