laravel5.6 Custom Background (Administrator) Logon

It can be achieved by using Laravel's own login and modifying it.

Run the command line: php artisan make:auth

First, we need to determine the data needed for login. I use the account encryption number (for example):

1. Create the admins data table and add the field id name number password.

    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('number');
            $table->string('name');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

OK, so we execute php artisan migrate for data migration.

We need to build an admin model:

Create a new Models folder under app, then php artisan make:model Models/Admin

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    //
    use Notifiable;
    protected $fillable = [
        'number', 'name', 'password'
    ];
    protected $guraded = [
        'remember_token', 'created_at', 'updated_at'
    ];
    protected $hidden = [
        'remember_token', 'created_at', 'updated_at'
    ];
}

After completing this step, don't forget to fill in the data (how to test empty data?................)), go directly to the code:

AdminFactory.php (new under database/dactories file) - - - ----- php artisan make:factory AdminFactory

$factory->define(App\Models\Admin::class, function (Faker $faker) {
    return [
        //
        'number' => rand(11111,22222),
        'name' => $faker->name,
        'password'=>'$2y$10$3K3BbMFSkvF70tZ4/WHDv.vF4B8fAPKyDk9Yw7RPMAy.09YAH/11K',
    ];
});

AdminsTableSeeder.php (new under database/seeds folder) - - - - - - - - php artisan make:seeder AdminsTableSeeder

    public function run()
    {
        //
        factory(App\Models\Admin::class,5)->create();
    }

In DatabaseSeeder.php:

    public function run()
    {
        // $this->call(UsersTableSeeder::class);
        $this->call(AdminsTableSeeder::class);
    }

Emmmm...... Execute php artisan db:seed so that data can be added....

We also need to modify the configuration:

config/auth.php:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

services.php:

    'stripe' => [
        'model' => App\Models\Admin::class,
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
    ],

It is based on the original modification.

The page has been generated before, so we need to modify the data in the page: the original email, we need to change it to number.

   <div class="form-group">
                        <input type="text" name="number" placeholder="Account number"
                            class="form-control  input-lg text-center no-border">
   </div>

The error message also needs to be modified, that is, to change the name, which is not much to say.

So let's go to LoginController:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/redirect';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function logout()
    {
        Auth::logout();
        return redirect('/login');
    }

    public function username (){
        return 'number';
    }
}

web.php

Route::get('/login', function () {
    if (Auth::check()) {
        return redirect('/redirect');
    }
    return view('auth.login');
});

Auth::routes();
// Redirection after Admin login
Route::group(['middleware' => ['web', 'auth']], function () {
    Route::get('/redirect', 'HomeController@index');
});

RedirectIfAuthenticated.php:

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/redirect');
        }

        return $next($request);
    }

So you can complete a simple login.

Tags: PHP Database Laravel

Posted on Wed, 09 Oct 2019 23:14:24 -0700 by tzikis