FahmidasClassroom

Learn by easy steps

Authentication

Steps:

1.Create a migration file for admin user.

php artisan make:migration add_is_admin_to_user_table --table=users

2. Add is_admin field to user table


public function up()
{
    Schema::table('users', function(Blueprint $table)
    {
        $table->boolean('is_admin')->default(false);
    });
}
public function down()
{
     Schema::table('users', function(Blueprint $table)
     {
         $table->dropColumn('is_admin');
     });
}

3. Run the migrate command

php artisan migrate

4. Create administration controller

php artisan make:controller Admin/UserController

5. Create model user

php artisan make:model User

6. Run the following command to install package for setting namespace in laravel 6.

composer require andrey-helldar/laravel-app

7. Add application namespace as ‘admin’

php artisan app:name newproject

8. Add the route group


Route::get('/utype', 'Admin\UserController@usertype');
Route::group(['prefix'=>'admin','namespace'=>'admin'], function()
{
    Route::resource('/','UserController');
    Route::get('alluser','UserController@all_user');
});

9. Create a new directory named admin under resource/views and add the following code in index.blade.php.

@extends('layouts.app')
@section('content')
<h1 align="center">{{ $message }} </h1>
@endsection

10. Open UserController and add User,Auth and Session class.


namespace newproject\Http\Controllers\Admin;

use newproject\Http\Controllers\Controller;
use Illuminate\Http\Request;
use newproject\User;
use Auth;
use Session;


class UserController extends Controller
{
    public function __construct()
{
    $this->middleware('auth');
}
public function usertype()
{
    if (Auth::user()->is_admin!=1)
        return view('home') ->with('message','Welcome to Home Page');
    else
        return view('Admin.index')->with('message','Welcome to Admin Page');
}
}

11. Modify the code of LoginController

protected $redirectTo = '/utype';

12. Start the server and login as admin or register user

http://localhost:8000/

13. Create a new view file named alluser.blade.php under admin folder.

@extends('layouts.app')
@section('content')
<h1>Registered Users</h1>
<ul>
@forelse ($users as $user)
<li>{{ $user->name }} ({{ $user->email }})</li>
@empty
<li>No registered users</li>
@endforelse
</ul>
@endsection

14. Add the following methods in UserController

public function index()
{
    if (Auth::user()->is_admin!=1)
    {
        Session::put('message', 'Access denied!');
        return \Redirect::to('home');
    }
    return view('Admin.index')->with('message','Welcome to Admin Page');
}
public function all_user()
{
    if (Auth::user()->is_admin!=1)
    {
       Session::put('message', 'Access denied!');
       return \Redirect::to('home');
    }
    $users = User::orderBy('created_at', 'desc')->get();
    return view('Admin.alluser')->with('users', $users);
}

15. Open HomeController and add the following code.

use Auth;
use Session;
public function index()
{
//return view('home');
     $e=Session::get('message');
     Session::put('message', '');
     return view('home')->with('message',$e);
}


16. Add the line in home.blade.php

<h1 align="center"> {{ $message }} </h1>

17. Open welcome.blade.php add the following route for home link

‘/utype’

18. Login as Register and admin user and try the following url

http://localhost:8000/admin
http://localhost:8000/admin/alluser

You can see the following video to show the steps properly.