Laravel User Authentication

Author: Al-mamun Sarkar Date: 2020-07-19 17:48:12

এই লেসনে লারাভেল এর Authentication নিয়ে আলোচনা করবো।দেখবো কিভাবে Laravel এ Login, Registration, Authentication check, Logout করতে হয়। 

 

Source Code:

web.php

use Illuminate\Support\Facades\Route;

Route::get('login', 'Auth\LoginController@index')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::get('logout', 'Auth\LoginController@logout');

Route::get('register', 'Auth\RegistrationController@index');
Route::post('register', 'Auth\RegistrationController@store');

Route::get('test', function() {
    if (Auth::check()) {
    	return Auth::user();
    } else {
    	return 'Please Login';
    }
});


Route::middleware('auth')->group( function() {
    Route::get('admin', function() {
	    return Auth::user();
	    return 'Welcome to admin';
	});
	Route::get('dashboard', function() {
	    return 'Welcome to dashboard';
	});
	Route::get('update', function() {
	    return 'Welcome to udate';
	});
	Route::get('posts', function() {
	    return 'Welcome to posts';
	});
});

 

LoginController

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function index()
    {
    	return view('auth.login');
    }


    public function login( Request $request )
    {
    	$data = $request->only(['email', 'password']);

    	if (Auth::attempt($data)) {
    		return redirect()->intended('/admin');
    	}
    	
    	return redirect()->to('/login');
    }


    public function logout()
    {
    	Auth::logout();

    	return redirect()->to('/login');
    }

}

 

RegistrationController:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\User;

class RegistrationController extends Controller
{
    public function index()
    {
    	return view('auth.register');
    }


    public function store(Request $request)
    {
    	$data = $request->only(['name', 'email']);
    	$data['password'] = Hash::make($request->get('password'));
    	User::create($data);
    	return 'registerd successfully';
    }
}

 

login.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Login</title>
</head>
<body>
	<form accept="/login" method="post">
		@csrf
		Eamil: <input type="email" name="email"> <br/><br/>
		Password: <input type="password" name="password"> <br/><br/>
		<input type="submit" value="Login">
	</form>
</body>
</html>

 

register.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Registration</title>
</head>
<body>
	<form accept="/register" method="post">
		@csrf
		Name: <input type="text" name="name"> <br/><br/>
		Eamil: <input type="email" name="email"> <br/><br/>
		Password: <input type="password" name="password"> <br/><br/>
		<input type="submit" value="Signup">
	</form>
</body>
</html>