Laravel 8.5版本实现自定义登录的完整流程,需求如下:

报名缴费系统需要实现考生个人与合作机构两种登录方式,其中个人登录使用 Laravel Breez 套件实现,这里主要讲一下合作机构登录的实现方式。

一、创建数据表和 Model 

Partner 表:

DROP TABLE IF EXISTS `partners`;
CREATE TABLE `partners`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `address` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `phone` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '固定电话',
  `contact_person` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '负责人',
  `contact_phone` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `licenses` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `types` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `years` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '合作时长,以年为单位',
  `expiration_date` timestamp NULL DEFAULT NULL COMMENT '账号合作期限(报名)',
  `is_valid` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '账号是否可以使用(登录)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `partners_name_unique`(`name`) USING BTREE COMMENT '不应存在同名机构'
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

Partner Model:

<?php

namespace AppModels;

use DcatAdminTraitsHasDateTimeFormatter;

use IlluminateDatabaseEloquentModel;
use IlluminateContractsAuthAuthenticatable;
use IlluminateAuthAuthenticatable as AuthenticableTrait;

class Partner extends Model implements Authenticatable
{
  use HasDateTimeFormatter,AuthenticableTrait;
}

注意:Partner Model 要实现 IlluminateContractsAuthAuthenticatable 契约

二、配置自定义 Auth Guard

在 config/auth.php 文件的 providers 中新增 partners 配置项,guards 中新增 partner 配置项,具体如下:

partners:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => AppModelsUser::class,
    ],

    'partners' => [
        'driver' => 'eloquent',
        'model' => AppModelsPartner::class,
    ]
],

guards:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'partner' => [
        'driver' => 'session',
        'provider' => 'partners',
    ],
],

三、创建路由、控制器及模板文件

未完待续…