Hello Geek, here is an example of how to create a custom login and registration system in Laravel 10:
Step 1: Install Laravel
First, you need to install Laravel. You can do this by running the following command in your terminal:
Code snippet
composer create-project laravel/laravel custom-login-and-registration
This will create a new Laravel project called custom-login-and-registration
.
Step 2: Create Database and Configure Database Credentials
Next, you need to create a database and configure the database credentials in the .env
file. You can do this by adding the following lines to the .env
file:
Code snippet
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=custom_login_and_registration
DB_USERNAME=root
DB_PASSWORD=password
Step 3: Migrate Tables to Database
Once you have created the database, you need to migrate the tables to the database. You can do this by running the following command:
Code snippet
php artisan migrate
This will create the users
table in the database.
Step 4: Create Custom Registration Login Routes
Next, you need to create custom registration and login routes. You can do this by adding the following lines to the routes/web.php
file:
Code snippet
Route::get('register', 'Auth\RegisterController@showRegistrationForm');
Route::post('register', 'Auth\RegisterController@register');
Route::get('login', 'Auth\LoginController@showLoginForm');
Route::post('login', 'Auth\LoginController@login');
Step 5: Create a LoginRegister Controller
Next, you need to create a LoginRegisterController
. You can do this by creating a new file called app/Http/Controllers/Auth/LoginRegisterController.php
.
The LoginRegisterController
should contain the following code:
PHP
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginRegisterController extends Controller
{
public function showRegistrationForm()
{
return view('auth.register');
}
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
$user = new \App\Models\User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->save();
Auth::login($user);
return redirect()->route('home');
}
public function showLoginForm()
{
return view('auth.login');
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|string|email|max:255',
'password' => 'required|string|min:6',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->route('home');
}
return redirect()->back()->withErrors('Invalid email or password.');
}
}
Step 6: Define Your New Home
Finally, you need to define your new home page. You can do this by creating a new file called resources/views/home.blade.php
.
The home.blade.php
file should contain the following code:
PHP
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to your new home!</h1>
</body>
</html
All the best nerd!