简体   繁体   English

Laravel auth :: attempt()不起作用

[英]Laravel auth::attempt() not working

I'm newbie to Laravel and got stuck with login mechanism I'm using my custom login mechanism here (not using Laravel Authentication) and there I'm not able to login with authenticate credentials. 我是Laravel的新手,并且无法使用登录机制,我在这里使用了自定义登录机制(不使用Laravel身份验证),并且无法使用身份验证凭据进行登录。

I want to login with credentials and after login the log should be maintain in login_master and redirected to home. 我想使用凭据登录,登录后该日志应保留在login_master中,并重定向到home。 but it is not maintaining plus if the credentials are wrong then it should redirect back to signin.blade.php but it's redirecting to home. 但如果凭据错误,则无法维护,则应将其重定向回signin.blade.php但会将其重定向到home。

Here is the code 这是代码

create_login_table.php (My Login Table Structure) 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 (Controller for working with table) 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 (view for login) 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>

You will need to configure app/auth.php or dotenv to use your own custom model with the Auth Guard helper in Laravel. 您将需要配置app/auth.php或dotenv,以通过Laravel中的Auth Guard帮助器使用自己的自定义模型。 There you can set the model and table you want to use. 您可以在model设置要使用的modeltable

Else you could implement your own Auth helper/facade to save logged_in_user_id in session. 否则,您可以实现自己的Auth助手/外观,以在会话中保存logged_in_user_id Your custom authentication will basically confirm the passed user credentials and store the logged_in_user_id in session. 您的自定义身份验证将基本上确认传递的用户凭据,并将logged_in_user_id存储在会话中。 Your logout method will likewise clear this id from session when called. 您的注销方法同样会在调用时从会话中清除此ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM