Nitish Kumar Nitish Kumar - 7 days ago 5
PHP Question

Strange login issue in 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);
}
}

Answer

You gotta login the user using:

\Auth::attempt($credentials)