Laravel Custom Table Login with Custom Validation Rules with Custom Message

Shah Md. Iktakhairul Islam
4 min readAug 28, 2022
  1. Loin Table: students

2. Login Blade with code:

<form action="{{ route('student.student_login') }}" method="POST" class="needs-validation" novalidate>
@csrf
<div class="mb-3">
<label for="username" class="form-label">Email</label>
<input name="email" type="text" class="form-control" id="username"
placeholder="Enter username">
@error('email') <span class="text-danger"><i class="entypo-info-circled"></i> {{ $message }}</span> @enderror
</div>

<div class="mb-3">
<div class="float-end">
<a href="{{route('student.password-reset-email')}}" class="text-muted">Forgot password?</a>
</div>
<label class="form-label">Password</label>
<div class="input-group auth-pass-inputgroup">
<input id="password-input" name="password" type="password" class="form-control" placeholder="Enter password" aria-label="Password" aria-describedby="password-addon">
<button onclick="showPassword()" class="btn btn-light " type="button" id="password-addon"><i class="mdi mdi-eye-outline"></i></button>
</div>
@error('password') <span class="text-danger"><i class="entypo-info-circled"></i> {{ $message }}</span> @enderror
</div>

<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember-check">
<label class="form-check-label" for="remember-check">
Remember me
</label>
</div>

<div class="mt-3 d-grid">
<button class="btn btn-primary waves-effect waves-light"
type="submit">Log In</button>
</div>

</form>

3. Config : auth.php

Add new guards and providers like this for student

4. Controller: StudentLogInController

Set guard() and return student auth login blade for frontend.

Set Student Guard For logIn

Login method with custom rules:

Full login system that return clean login with custom validation rules

Full Controller code bellow:

<?php

namespace App\Http\Controllers\Student\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Student\StudentLoginRequest;
use App\Model\Student;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Validator;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class StudentLoginController extends Controller
{
/**
* Student guard check
*
@return Guard|StatefulGuard
*/
protected function guard()
{
return Auth::guard('student');
}

/**
* Return login view pages
*
@return View
*/
public function loginView()
{
return view('student.auth.login');
}

/**
* Student login with custom validation
*
@param StudentLoginRequest $request
*
@return RedirectResponse
*
@throws ValidationException
*/
public function studentLogin(StudentLoginRequest $request)
{
/**
* General email validation with exists check
*/
$this->validate($request, [
'email' => 'required|email|exists:students,email',
'password' => 'required',
]);

/**
* Custom validation rules, message return and validate
*/
$rules = [
'email' => [
Rule::exists('students', 'email')->where(function ($query) {
return $query->where('status', '!=', 'un_verified')->where('email_verified_at', '!=', '');
}),
],
];
$messages = ['email.exists' => 'Your account is not activated yet. Please verify email or contact with authority.'];
$email = Validator::make($request->all(), $rules, $messages);
if ($email->fails()) {
return redirect()->route('student.student_login')->withErrors($email)->withInput();
}

/**
* Login attempt
*/
$student = Student::where('email', $request['email'])->first();
if($this->guard()->attempt(array('email' => $request['email'], 'password' => $request['password']))) {
$student->update([
'last_login' => now()->format('F j, Y, g:i a'),
]);
return redirect()->route('student.dashboard');
}else {
return redirect()->route('student.student_login')->withErrors(array('password' => 'Invalid password, please try again later.'))->withInput();
}
}

/**
* Logout function for student.
*
@param Request $request
*
@return RedirectResponse
*/
public function logout(Request $request)
{
Auth::guard('student')->logout();

$request->session()->invalidate();

return redirect()->route('student.student_login');
}
}

Just paste to your code and test by yourself.

Additional Instruction

Set a middleware for student table users.

In App\Http\Karnel

App\Http\Middleware\StudentAuth with code

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class StudentAuth
{
/**
* Handle an incoming request.
*
*
@param Request $request
*
@param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*
@return RedirectResponse
*/
public function handle($request, Closure $next)
{
if (!Auth::guard('student')->check()) {
return redirect('/student/login');
}
return $next($request);
}
}

Now you can use this middleware in controller and route like this, you can also separate route from user also.

In controller and route

Follow those code and you will definitly found a good result.

— Shah Md. Iktakhirul Islam, Software Engineer.

Contact Number: 01683201359

Email: iktakhairul@gmail.com

GitHub Profile: https://github.com/iktakhairul

--

--