Let's Create Custom Laravel Login and Registration From Scratch
In this tutorial I will show how to make a custom login and registration feature with Laravel 8. We will make custom authentication.
Though laravel provides auth scaffolding but in this tutorial we will create our own custom login and registration features in our application. Laravel provides the basic login, registration, and password reset functionalities.
Let's Start Our Laravel Custom Auth Login And Registration Tutorial!
Step 1: Create Laravel Project
As we create custom auth from scratch, run the following command to install the new laravel app. However, you can skip this step if the app is already installed.
composer create-project --prefer-dist laravel/laravel your-project-name
Step 2: Setup route
After we have created our Laravel Project, we have to setup our routes.
In routes\web.php
Route::namespace('Auth')->group(function () {
Route::get('/login','LoginController@show_login_form')->name('login');
Route::post('/login','LoginController@process_login')->name('login');
Route::get('/register','LoginController@show_signup_form')->name('register');
Route::post('/register','LoginController@process_signup');
Route::post('/logout','LoginController@logout')->name('logout');
});
Step 3: Create LoginController
We will need to create a login controller where we write our custom login and registration code.
App\Http\Controllers\Auth\LoginController.php
namespace App\Http\Controllers\Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class LoginController extends Controller {
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function show_login_form() {
return view('login');
}
public function process_login(Request $request) {
$request->validate([
'name' => 'required',
'password' => 'required'
]);
$credentials = $request->except(['_token']);
$user = User::where('name',$request->name)->first();
if (auth()->attempt($credentials)) {
return redirect()->route('home');
} else {
session()->flash('message', 'Invalid credentials!');
return redirect()->back();
}
}
public function show_signup_form() {
return view('backend.register');
}
public function process_signup(Request $request) {
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required'
]);
$user = User::create([
'name' => trim($request->input('name')),
'email' => strtolower($request->input('email')),
'password' => bcrypt($request->input('password')),
]);
session()->flash('message', 'Account created successfully!');
return redirect()->route('login');
}
public function logout() {
\Auth::logout();
return redirect()->route('login');
}
}
Step 4: Create Auth Blade View Files
You need to create auth folder in resources/views/
folder and create a new login.blade.php
file in it.
Create register.blade.php
inside resources/views/
folder.
Everything is done for our login and registration. We just need to setup log-out feature and we're done. Paste below code where ever you want to show logout button.
If you want to redirect user to custom path / url after logged-in you can do it pretty easily. Go path app/Providers/RouteServiceProvider.php
and change path:
public const HOME = '/give_your_required_path_here';
If you like this blog post and it was useful to you, please follow us on Twitter and Facebook.
Related articles
Sources:
0 Comments