duanchui1955 2016-11-30 05:02
浏览 66
已采纳

Laravel 5.3中的奇怪登录问题

I'm using Laravel 5.3 I'm trying to build a simple login where there are two different backend panel for different user. I've placed a cross check of user and redirect it through middleware. I've made this with making Auth controller in the framework. Now the problem is with the login. I don't know but it is not connecting with the database properly, I mean the user is not getting logged in. But once I do a registration the user gets registered and database is being added. Now if I login through the recent user registered details, it logs in and show the appropriate view. Please help me out:

Following is my route file:

Auth::routes();
Route::get('/memberlogin', function(){
  return view('admin.memberlogin');
});

My Blade file:

@extends('admin.authlayouts')

@section('content')
    <!-- Login content -->
    <div class="col-xs-12 tab-pane fade in active login-content">
        <span class="text-center">Login to your account</span>
        <div class="col-xs-12 login-form">
            <span class="text-center"><img src="images/icon-login.png" alt="login-icon"></span>
            <form role="form" method="POST" action="{{ url('/login') }}">
              {{ csrf_field() }}
                <div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
                    <label class="sr-only" for="username">Username</label>
                    <input type="text" id="username" name="username" value="{{ old('username') }}" placeholder="Username" class="input-field username form-control" required autofocus>
                  @if ($errors->has('username'))
                      <span class="help-block">
                          <strong>{{ $errors->first('username') }}</strong>
                      </span>
                    @endif
                </div>
                <div class="form-group">
                    <label class="sr-only" for="password">Password</label>
                    <input id="password" type="password" name="password" placeholder="Password" class="input-field password form-control">
                  @if ($errors->has('password'))
                      <span class="help-block">
                          <strong>{{ $errors->first('password') }}</strong>
                      </span>
                    @endif
                </div>
                <div class="form-group">
                    <label><input type="checkbox" name="" id="remember-check"> Remember me</label>
                    <input type="submit" value="Sign in" class="btn btn-success pull-right">
                </div>
            </form>                        
            <div class="forget-password">
                <span class="bold">Forgot your password?</span>
                No worries, <a class="bold" href="/memberresetpassword">click here</a> to reset your password
            </div>
            <div class="text-center click-to-register">
                <span>Don't have an Account yet?</span>
                <a class="btn btn-success btn-lg" href="/memberregister">Create an account</a>
            </div>
        </div>
    </div>
@endsection

And my Controller/Auth/LoginController :

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use  \Auth;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = 'member/dashbaord';

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

}

And my Http/Middleware/RidirectIfAuthenticated :

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
           if (Auth::user()->is_admin == 0)
           {     
             return redirect()->intended('/admin/dashboard');   
           }
           else 
           {
             return redirect('/member/dashboard');      
           }  
         }
        return $next($request);
    }
}
  • 写回答

1条回答 默认 最新

  • dongye4192 2016-11-30 05:05
    关注

    You gotta login the user using:

    \Auth::attempt($credentials)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算