Laravel 7 Register and Login Account

Laravel 7 Register and Login Account









1) Install Laravel Fresh New Setup

To install a fresh Laravel project, run the following command:

composer create-project --prefer-dist laravel/laravel Blog

2) Setup Database Credentials

After installing Laravel, configure the database in your .env file with the correct credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_DATABASE=your_database_name DB_PORT=3306 DB_USERNAME=your_database_username DB_PASSWORD=your_database_password

Next, run the migration to create the default tables:

php artisan migrate

3) Make Routes

In routes/web.php, create routes for login, registration, post-data, and dashboard.

use App\Http\Controllers\AuthController; Route::get('/', function () { return view('login'); }); Route::get('login', [AuthController::class, 'index']); Route::post('post-login', [AuthController::class, 'postLogin']); Route::get('registration', [AuthController::class, 'registration']); Route::post('post-registration', [AuthController::class, 'postRegistration']); Route::get('dashboard', [AuthController::class, 'dashboard']); Route::get('logout', [AuthController::class, 'logout']);

4) Create Controller

Generate the AuthController with the following Artisan command:

php artisan make:controller AuthController

Now, open the app/Http/Controllers/AuthController.php file and add the following methods:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator; use Redirect; use Response; use App\Models\User; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Session; class AuthController extends Controller { public function index() { return view('login'); } public function registration() { return view('registration'); } public function postLogin(Request $request) { $request->validate([ 'email' => 'required|email', 'password' => 'required', ]); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended('dashboard'); } return Redirect::to("login")->withErrors(['email' => 'Invalid credentials']); } public function postRegistration(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required|email|unique:users', 'password' => 'required|min:6|confirmed', ]); $data = $request->all(); $this->create($data); return Redirect::to("dashboard")->withSuccess('Successfully registered'); } public function dashboard() { if (Auth::check()) { return view('dashboard'); } return Redirect::to("login")->withErrors(['login' => 'Access denied']); } public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); } public function logout() { Session::flush(); Auth::logout(); return Redirect('login'); } }

5) Create Blade Views

a) login.blade.php

In resources/views/login.blade.php, create the login form:

<!DOCTYPE html> <html> <head> <title>Login Form</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="csrf-token" content="{{ csrf_token() }}"> <link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" type="text/css" href="{{url('style.css')}}"> </head> <body> <div class="signup-form"> <form action="{{url('post-login')}}" method="POST" id="logForm"> {{ csrf_field() }} <h2>Login Account</h2> <p class="lead">Please login to access the dashboard.</p> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-paper-plane"></i></span> <input type="email" class="form-control" name="email" id="inputEmail" placeholder="Email Address" required="required"> </div> @if ($errors->has('email')) <span class="error">{{ $errors->first('email') }}</span> @endif </div> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-lock"></i></span> <input type="password" class="form-control" name="password" id="inputPassword" placeholder="Password" required="required"> </div> @if ($errors->has('password')) <span class="error">{{ $errors->first('password') }}</span> @endif </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-block btn-lg">Sign In</button> </div> <p class="small text-center">By clicking the Sign In button, you agree to our <br><a href="#">Terms &amp; Conditions</a>, and <a href="#">Privacy Policy</a>.</p> </form> <div class="text-center">Don't have an account? <a href="{{url('registration')}}">Sign Up here</a>.</div> </div> </body> </html>

b) registration.blade.php

In resources/views/registration.blade.php, create the registration form:

<!-- Similar to login.blade.php with necessary fields for registration -->

c) dashboard.blade.php

In resources/views/dashboard.blade.php, create the dashboard view:

<!DOCTYPE html> <html> <head> <title>Dashboard</title> </head> <body> <h1>Welcome to your dashboard!</h1> <a href="{{url('logout')}}">Logout</a> </body> </html>

6) Run Development Server

Run the Laravel development server using:

php artisan serve

Conclusion

You now have a basic authentication system in Laravel with routes, controllers, views, and a working login, registration, and dashboard flow. You can enhance and customize this as needed for your application.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

2 Comments

CAN FEEDBACK
  1. Mr.Anonymous
    Mr.Anonymous
    where is the registtration and dasboard code bor
  2. Mr.Anonymous
    Mr.Anonymous
    Thanks a lot for giving your hard work
close