Laravel Login Registration Tutorial
1). Install Laravel Fresh New Setup
First, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :
First, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :
composer create-project --prefer-dist laravel/laravel Blog
composer create-project --prefer-dist laravel/laravel Blog
2). Setup Database
After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :
After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=here your database name here
DB_PORT=3306
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_DATABASE=here your database name here DB_PORT=3306 DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Next, migrate the table into the database using the below command :
Next, migrate the table into the database using the below command :
php artisan migrate
php artisan migrate
|
3). Make Route
In this step, we will create some routes like custom login route, custom registration route, post data route, and dashboard, etc.
In this step, we will create some routes like custom login route, custom registration route, post data route, and dashboard, etc.
Route::get('/', function () {
return view('login');
});
Route::get('login', 'AuthController@index');
Route::post('post-login', 'AuthController@postLogin');
Route::get('registration', 'AuthController@registration');
Route::post('post-registration', 'AuthController@postRegistration');
Route::get('dashboard', 'AuthController@dashboard');
Route::get('logout', 'AuthController@logout');
Route::get('/', function () { return view('login'); }); Route::get('login', 'AuthController@index'); Route::post('post-login', 'AuthController@postLogin'); Route::get('registration', 'AuthController@registration'); Route::post('post-registration', 'AuthController@postRegistration'); Route::get('dashboard', 'AuthController@dashboard'); Route::get('logout', 'AuthController@logout');
5). Create Controller
We need to create a controller name AuthController. Use the below command and create Controller :
We need to create a controller name AuthController. Use the below command and create Controller :
php artisan make:controller AuthController
php artisan make:controller AuthController
After successfully create controller go to app/controllers/AuthController.php and update the below code in your controller:
After successfully create controller go to app/controllers/AuthController.php and update the below code in your controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response;
Use App\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',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
return Redirect::to("login")->withSuccess('Oppes! You have entered invalid credentials');
}
public function postRegistration(Request $request)
{
request()->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
//'contact_number' => 'required|contact_number|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return Redirect::to("dashboard")->withSuccess('Great! You have Successfully loggedin');
}
public function dashboard()
{
if(Auth::check()){
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
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');
}
}
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response; Use App\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', 'password' => 'required', ]); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { // Authentication passed... return redirect()->intended('dashboard'); } return Redirect::to("login")->withSuccess('Oppes! You have entered invalid credentials'); } public function postRegistration(Request $request) { request()->validate([ 'name' => 'required', 'email' => 'required|email|unique:users', //'contact_number' => 'required|contact_number|unique:users', 'password' => 'required|min:6', ]); $data = $request->all(); $check = $this->create($data); return Redirect::to("dashboard")->withSuccess('Great! You have Successfully loggedin'); } public function dashboard() { if(Auth::check()){ return view('dashboard'); } return Redirect::to("login")->withSuccess('Opps! You do not have access'); } 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'); } } |
6). Create Blade view
In this step, we need to create three-blade view files. The first is login blade.php and the second one is registration blade.php and the last one is dashboard.blade.php.
In this step, we need to create three-blade view files. The first is login blade.php and the second one is registration blade.php and the last one is dashboard.blade.php.
The First Login Blade.php
Now you can create login.blade.php file and update the below code into your file:
Now you can create login.blade.php file and update the below code into your file:
<!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="description" content="">
<meta name="author" content="">
<meta name="csrf-token" content="{{ csrf_token() }}">
<!--Bootsrap 4 CDN-->
<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 form too dashbord admin.</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 Up</button>
</div>
<p class="small text-center">By clicking the Sign Up button, you agree to our <br><a href="#">Terms & Conditions</a>, and <a href="#">Privacy Policy</a>.</p>
</form>
<div class="text-center">Already have an account? <a href="{{url('registration')}}">Sign Up here</a>.</div>
</div>
</body>
</html>
<!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="description" content="">
<meta name="author" content="">
<meta name="csrf-token" content="{{ csrf_token() }}">
<!--Bootsrap 4 CDN-->
<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 form too dashbord admin.</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 Up</button>
</div>
<p class="small text-center">By clicking the Sign Up button, you agree to our <br><a href="#">Terms & Conditions</a>, and <a href="#">Privacy Policy</a>.</p>
</form>
<div class="text-center">Already have an account? <a href="{{url('registration')}}">Sign Up here</a>.</div>
</div>
</body>
</html>
2 Comments
where is the registtration and dasboard code bor
ReplyDeleteThanks a lot for giving your hard work
ReplyDeleteCAN FEEDBACK
Emoji