Reputation: 43
I am working on a login system for two users - admin and customer. For the admins, I've used the default auth files, but created new ones for the customer. My question is that the auth::check
for admins works, but it doesn't for customers. So it doesn't query the database and logs in any email and any password. How can I fix this?
customerlogin.php
@extends('layouts.app')
@section('title','CustomerLogin')
@push('css')
@endpush
@section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-1">
@include('layouts.partial.msg')
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title">Customer Login</h4>
</div>
<div class="card-content">
<form method="POST" action="{{ route('customerLogin') }}">
@csrf
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Email</label>
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
<a href="{{ route('homepage') }}" class="btn btn-danger">Back</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@endpush
CustomerLogController
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Item;
use App\Slider;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AutenticatesUser;
use Illuminate\Support\Facades\Hash;
use Auth;
use Redirect;
use Session;
use Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class CustomerLogController extends Controller
{
use AutenticatesUser;
/**
* Where to redirect users after login.
*
* @var string
*/
protected function authenticated() {
if($customers = Auth::customers()){
return redirect()->route('homepage');
}
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Create a new controller instance.
*
* @return void
*/
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$sliders = Slider::all();
$categories = Category::all();
$items = Item::all();
return view('homepage',compact('sliders','categories','items'));
}
public function logout(Request $request) {
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::redirect('lara-admin','login');
Route::get('/','HomeController@index')->name('welcome');
Route::get ('/homepage', 'HomepageController@index')->name('homepage');
Route::post('/reservation','ReservationController@reserve')->name('reservation.reserve');
Route::post('/contact','ContactController@sendMessage')->name('contact.send');
Auth::routes();
Route::resource ('customerLogin', 'CustomerLoginController');
Route::post('homepage', 'CustomerLogController@index')->name('customerLogin');
Route::get('/logout', 'CustomerLogController@logout');
Route::group(['prefix'=>'admin','middleware'=>'auth','namespace'=>'Admin'], function (){
Route::get('dashboard', 'DashboardController@index')->name('admin.dashboard');
Route::resource('slider','SliderController');
Route::resource('category','CategoryController');
Route::resource('item','ItemController');
Route::get('reservation','ReservationController@index')->name('reservation.index');
Route::post('reservation/{id}','ReservationController@status')->name('reservation.status');
Route::delete('reservation/{id}','ReservationController@destory')->name('reservation.destory');
Route::get('contact','ContactController@index')->name('contact.index');
Route::get('contact/{id}','ContactController@show')->name('contact.show');
Route::delete('contact/{id}','ContactController@destroy')->name('contact.destroy');
});
Any help would be much appreciated!
Upvotes: 2
Views: 83
Reputation: 11
In an older version of Laravel, when we created new auth files, we had the same issue as you're having - that Auth::Check()
did not work. At all. Could not work out why.
We ended up manually checking the login, ensuring that it was correct, by using Hash::check()
and then processing the login using Auth::LoginUsingId()
.
if(Hash::check($password, $user->u_pass)){
Auth::LoginUsingId($user->id);
return redirect()->intended('/');
}
At this point, all the Auth::
functions worked as intended.
It was a hacky solution, but it did work.
Upvotes: 1