How to Make Login in Laravel Authentication with Laravel
How to Make Login in Laravel Authentication with Laravel
Suggested:
How to Make Registration in Laravel Authentication with Laravel
In this tutorial, we have handled Laravel Authentication (Register,login) Data using mysql,jQuery. and user registration & login With html form in Laravel, MySQL and jQuery. The registration form submit and store into MySQL database
We have handled tutorial in very easy steps. So let’s start the coding
Here put some sample code. for full code end of post click download now
install laravel / setup laravel
This are following command
create-project --prefer-dist laravel/laravel RegisterLogin
cd RegisterLogin
In this step, we will first create our database with name register_login and then configure your database credentials in the .env file
.env file avalible on Laravel root folder
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=custom_login_register
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password
php artisan migrate
Create Registration Login Routes
// Login
Route::get('/', [AuthController::class, 'index'])->name('login');
Route::post('/', [AuthController::class, 'login'])->name('login.post');
// Register
Route::get('/register', [AuthController::class, 'register_view'])->name('register');
Route::post('/register', [AuthController::class, 'register'])->name('register.post');
Register controller
public function register(Request $request)
{
$request->validate(
[
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|confirmed',
'remember' => 'required',
],
[
'remember.required' => 'Please Accept T&C',
]
);
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Alert::success('success', 'User Register SuccessFully');
return redirect()->route('login');
}
Step 1: Create a MySQL database table .
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) NOT NULL,
`remember_token` varchar(100) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Step 2: connection to the MySQL database.
In this step, we will first create our database with name register_login and then configure your database credentials in the .env file
.env file avalible on laravel root folder
Step 3: Add the Laravel, JavaScript, jQuery, and Bootstrap files.
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
Step 4 Create the HTML for the Register Form .
<div class="col-12">
<form action="{{ route('register.post') }}" method="post">
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" placeholder="Name"
value="{{ old('name') }}" />
@if ($errors->has('name'))
<p class="text-danger">{{ $errors->first('name') }}</p>
@endif
</div><br>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" class="form-control" placeholder="Email"
value="{{ old('email') }}" />
@if ($errors->has('email'))
<p class="text-danger">{{ $errors->first('email') }}</p>
@endif
</div><br>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" />@if ($errors->has('password'))<p class="text-danger">{{ $errors->first('password') }}</p>
@endif
</div><br>
<div class="form-group">
<label for="password_confirmation">Confirm Password</label>
<input type="password" name="password_confirmation" class="form-control"
placeholder="Confirm Password" />
@if ($errors->has('password_confirmation'))
<p class="text-danger">{{ $errors->first('password_confirmation') }}</p>
@endif
</div><br>
<div class="row">
<div class="col-8">
<div class="form-check">
<input type="checkbox" class="form-check-input border-dark" name="remember"
id="remember">
<label class="form-check-label" for="remember">Accept T&C</label>
@if ($errors->has('remember'))
<p class="text-danger">{{ $errors->first('remember') }}</p>
@endif
</div>
</div>
<div class="col-4 text-center">
<input class="btn btn-primary" type="submit" value="Register">
</div>
</div>
</form>
<br>
<p>Already have an account? <a href="{{ route('login') }}">Login</a></p>
</div>
Sample Screen short
Step 5: Use Laravel ,jQuery to valdate the form and submit it.
$request->validate(
[
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|confirmed',
'remember' => 'required',
],
[
'remember.required' => 'Please Accept T&C',
]
);
Sample screen short
Step 6: submit data Save in to database server.
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Alert::success('success', 'User Register SuccessFully');
return redirect()->route('login');
7. Run Laravel Development Server
Now we have successfully completed all the steps Now run the Laravel development server for this command.
php artisan serve
And then open your web browser and browser or visit the following URL to test
http://127.0.0.1:8000/register
Download Full Code HERE
How to Make Login in Laravel Authentication with Laravel