定制 rzb/social-auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rzb/social-auth

Composer 安装命令:

composer require rzb/social-auth

包简介

Social authentication boilerplate.

README 文档

README

Socialite wrapper that handles all the remaining social authentication boilerplate, exposing endpoints ready for use that enable multiple providers at once for any model(s) you need.

Installation

  1. Require via Composer
$ composer require rzb/socialauth
  1. Run auto-discovered migrations
$ php artisan migrate
  1. Add the contracts and traits to the User model, or to whatever model(s) you need
<?php

namespace App\Models\User;

use Rzb\SocialAuth\Contracts\Resourceable as ResourceableContract;
use Rzb\SocialAuth\Contracts\Sociable as SociableContract;
use Rzb\SocialAuth\Traits\Resourceable;
use Rzb\SocialAuth\Traits\Sociable;
// ...

class User extends AuthUser implements SociableContract, ResourceableContract
{
    use Resourceable;
    use Sociable;
    // ...
}
  1. (optional) If you need to authenticate any model(s) other than the User, add/remove providers or tweak the exposed Routes, publish the config file
$ php artisan vendor:publish --provider="Rzb\SocialAuth\SocialAuthServiceProvider" --tag="config"

How it works

By default, once installed the package automatically exposes 2 stateless endpoints and accepts Google, Facebook and Twitter providers for the User model. These routes expect the same 2 segments.

Routes

  • Returns the auth URL for the given provider, so your frontend can load the provider's auth page.
    GET  /auth/social/{provider}/{sociable}
  • Callback route that finds or creates the given sociable model and returns its JsonResource.
    POST /auth/social/{provider}/{sociable}
    [
        'access_token' => 'required|string' // incoming token from provider
    ]

URL Segments

  • provider - the provider name. e.g. google, facebook, etc.

  • sociable - the name of the sociable model as per your config file. e.g.user, customer, etc.

Customization

For most projects, you won't have to do anything at all after installation. If however you have different needs, you're free to tweak the configuration to add/remove models and providers, use your own controller or even implement your own DB logic.

Customize supported models and providers

You can support authentication for any model you want.

  1. Add an entry to the sociables key in the config file, indicating the model class and the providers you want to allow specifically for that model.
    // config/socialauth.php
    
    'sociables' => [
    
        // default model. You can remove it or tweak it.
        'user' => [
            'model' => User::class,
            'providers' => [
                'google',
                'facebook',
                'twitter',
            ],
        ],
        
        // Customer model example
        // 'customer' => [
        //     'model' => Customer::class,
        //     'providers' => [
        //         'google',
        //         'github',
        //     ],
        // ],
    ],
  1. Next, make sure your model implements and uses the following contracts and traits.
// Transforms User profile info from the social account to your sociable model.
use Rzb\SocialAuth\Contracts\Sociable as SociableContract;
use Rzb\SocialAuth\Traits\Sociable;

// Converts your sociable model to its respective JsonResource class. 
// Only needed if you're using the package's default controller.
use Rzb\SocialAuth\Contracts\Resourceable as ResourceableContract;
use Rzb\SocialAuth\Traits\Resourceable;

Customize routes and controller

You can apply middlewares and route prefix to the package routes. Or even replace its controller with your own.

    // config/socialauth.php
    
    'routes' => [
        'controller' => SocialAuthController::class,
        'middleware' => null,
        'prefix' => 'auth/social',
    ],

Customize DB logic

The Sociable trait covers a basic User model with username and password. If your model has a different structure you may want to define your own createFromSocialUser method.

use Laravel\Socialite\Contracts\User as SocialUser;

public static function createFromSocialUser(SocialUser $socialUser): self
{
    return self::forceCreate([
        // map your model attributes here
        'email' => $socialUser->getEmail(),
        'name' => $socialUser->getName(),
    ]);
}

Credits

rzb/social-auth 适用场景与选型建议

rzb/social-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「socialauth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rzb/social-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rzb/social-auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 rzb/social-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 143
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-06