a2workspace / laravel-social-entry
提供一個整合的第三方登入入口
1.0.0
2022-07-17 01:36 UTC
Requires
- php: ^7.4|^8.0
- jakeasmith/http_build_url: ^1
- socialiteproviders/line: ^4.1
Requires (Dev)
- orchestra/testbench: 6.x
This package is auto-updated.
Last update: 2024-11-04 06:24:17 UTC
README
提供一個幾乎零配置,整合前後端的第三方登入的 API 身分認證接口
特性:
- 基於 Laravel Socialite 的 API 身分認證接口
- 專為前端 API 接口設計
- 解決一般登入與社群登入不統一的問題
- 解決新使用者透過社群登入後,要填寫完成註冊表格才能完成註冊的情況
- 整合社群帳號連結功能
- 支援多使用者模型
- 相容 Nuxt.js;參考套件 nuxt-social-entry
快速前往:
Installation | 安裝
要在專案中使用 SocialEntry,執行下列命令透過 Composer 引入到你的 Laravel 專案中:
composer require a2workspace/laravel-social-entry
接著使用 vendor:publish
命令生成設定檔:
php artisan vendor:publish --tag=@a2workspace/laravel-social-entry
現在你可以在 config/social-entry.php
中指定要啟用的第三方授權登入。
Configuration | 配置
開始用 SocialEntry 前,如同使用 Laravel Socialite,你必須要先將第三方服務設定加到 config/services.php
內。你可以使用下面的範例或參考說明 Laravel Socialite Configuration。
一個簡單的設定範例:
'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URL', '/auth/github/callback'), ], 'facebook' => [ 'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'redirect' => env('FACEBOOK_REDIRECT_URL', '/auth/facebook/callback'), ], 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_REDIRECT_URL', '/auth/google/callback'), ], 'line' => [ 'client_id' => env('LINE_CHANNEL_ID'), 'client_secret' => env('LINE_SECRET'), 'redirect' => env('LINE_REDIRECT_URL', '/auth/line/callback'), ],
.env
/.env.example
設定:
# See https://github.com/settings/developers GITHUB_CLIENT_ID= GITHUB_CLIENT_SECRET= GITHUB_REDIRECT_URL= # See https://developers.facebook.com/apps/?show_reminder=true FACEBOOK_CLIENT_ID= FACEBOOK_CLIENT_SECRET= FACEBOOK_REDIRECT_URL= # See https://console.cloud.google.com/apis/credentials GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= GOOGLE_REDIRECT_URL= # See https://developers.line.biz/console/ LINE_CHANNEL_ID= LINE_SECRET= LINE_REDIRECT_URL=
這裡提供第三方的登入設定頁面連結:
Github
: https://github.com/settings/developersFacebook
: https://developers.facebook.com/apps/?show_reminder=trueGoogle
: https://console.cloud.google.com/apis/credentialsLine
: https://developers.line.biz/console/
Registering Routes | 註冊路由
接著你應該在 App\Providers\AuthServiceProvider
的 boot
方法中,呼叫 SocialEntry::routes
方法來註冊路由。
<?php namespace App\Providers; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Gate; use A2Workspace\SocialEntry\SocialEntry; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array<class-string, class-string> */ protected $policies = [ // 'App\Models\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any authentication / authorization services. * * @return void */ public function boot() { $this->registerPolicies(); if (! $this->app->routesAreCached()) { SocialEntry::routes(); } } }