Authentication is the process of identifying the user credentials. In web applications, authentication is managed by sessions which take the input parameters such as email or username and password, for user identification. If these parameters match, the user is said to be authenticated.
Register.blade.php
<div class="container card" style="padding:20px">
<form action="{{ route('custom_register') }}" method="post">
<h2>Register Form</h2><br/>
@csrf
<div class="input-container">
<i class="fa fa-user icon"></i>
<input class="input-field form-control" type="text" placeholder="Fullname" name="name">
</div>
@error('name')
<div class="alert alert-danger">{{$message}}</div>
@enderror<br/>
<div class="input-container">
<i class="fa fa-user icon"></i>
<input class="input-field form-control" type="text" placeholder="email or mobile or username" name="email">
</div>
@error('email')
<div class="alert alert-danger">{{$message}}</div>
@enderror<br/>
<div class="input-container">
<i class="fa fa-envelope icon"></i>
<input class="input-field form-control" type="password" placeholder="password" name="password">
</div>
@error('password')
<div class="alert alert-danger">{{$message}}</div>
@enderror<br/>
<div class="input-container">
<i class="fa fa-key icon"></i>
<input class="input-field form-control" type="password" placeholder="confirm password" name="password_confirmation">
</div>
@error('password_confirmation')
<div class="alert alert-danger">{{$message}}</div>
@enderror<br/>
<select class="form-control" name="role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select> <br/>
<button type="submit" class="btn btn-success">Register</button>
</form>
</div>
RegisterController
function signup(Request $request){
$request->validate([
'name' => 'required|string|max:250',
'email' => 'required|email|unique:users',
'password' => 'required|min:8|confirmed' ]); User::create([ 'name' => $request->name, 'email' => $request->email, 'role' => $request->role, 'password' => Hash::make($request->password) ]); $credentials = $request->only('email', 'password'); if(Auth::attempt($credentials)){ $request->session()->regenerate(); $count = DB::table('users')->where('id', auth()->user()->id)->where('role', 'admin')->count(); if($count > 0){ return redirect('dashboard'); } else{ return redirect('/'); } }
UserModel
protected $fillable = [
'name',
'email',
'password',
'role'
];
LoginController
function login(Request $request){
$validated = $request->validate([
'email'=>'required',
'password'=>'required',
]);
if(Auth::attempt($validated)) {
$request->session()->regenerate();
$count = DB::table('users')->where('id', auth()->user()->id)->where('role', 'admin')->count();
if($count > 0){
return redirect('dashboard');
}
else{
return redirect('/');
}
return redirect()->intended('dashboard');
}
returnback()->withErrors([
'email'=>'The provided credentials do not match our records.',
])->onlyInput('email');
}
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) { // Authentication was successful... }
Logout Form
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit" class="btn btn-danger">
Logout
</button>
</form>