donglv9813 2016-10-28 04:20
浏览 129

Laravel auth :: attempt()无效

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.

I want to login with credentials and after login the log should be maintain in login_master and redirected to 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.

Here is the code

create_login_table.php (My Login Table Structure)

<?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)

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)

<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条回答 默认 最新

  • dream6120 2017-09-09 14:50
    关注

    You will need to configure app/auth.php or dotenv to use your own custom model with the Auth Guard helper in Laravel. There you can set the model and table you want to use.

    Else you could implement your own Auth helper/facade to save logged_in_user_id in session. Your custom authentication will basically confirm the passed user credentials and store the logged_in_user_id in session. Your logout method will likewise clear this id from session when called.

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?