<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Laravel 的 社會化登錄功能 - [簡介](#introduction) - [授權](#license) - [官方文檔](#official-documentation) - [配置](#configuration) - [基本用法](#basic-usage) - [無狀態身份驗證](#stateless-authentication) - [檢索用戶詳細信息](#retrieving-user-details) - [從令牌檢索用戶詳細信息](#retrieving-user-details-from-token) <a name="introduction"></a> ## 簡介 Laravel 社會化登錄通過 Facebook , Twitter ,Google ,LinkedIn ,GitHub 和 Bitbucket 提供了一個富有表現力的,流暢的 OAuth 身份驗證界面。它幾乎能處理所有你害怕處理的各種樣板社會認證代碼。 > 翻譯自 Readme: https://github.com/laravel/socialite **我們不接受新的適配器。** **如果你使用 Laravel 5.3 或更低版本,請使用 [Socialite 2.0](https://github.com/laravel/socialite/tree/2.0)。** 社區驅動的社會化登錄提供商網站上可以找到為其他平臺提供的適配器列表。 <a name="license"></a> ## 授權 Laravel 社會化登錄是根據 [MIT 授權](http://opensource.org/licenses/MIT) 許可的開源軟件 <a name="official-documentation"></a> ## 官方文檔 除了常規的基于表單的身份驗證之外, Laravel 也提供了一種簡單,方便的辦法來使用 [Laravel 社會化登錄](https://github.com/laravel/socialite) 向 OAuth 提供程序進行身份驗證。社公化登錄目前支持 `Facebook` , `Twitter` , `LinkedIn` ,`Google` ,`GitHub` 和 `Bitbucket` 的身份驗證。 要開始社會化登錄,使用 `composer` 將相應包加入到你項目的依賴項中。 composer require laravel/socialite <a name="configuration"></a> ### 配置 安裝完社會化登錄庫之后,在你的 `config/app.php` 文件中注冊 `Laravel\Socialite\SocialiteServiceProvider` 。 ```php 'providers' => [ // Other service providers... Laravel\Socialite\SocialiteServiceProvider::class, ], ``` 同時,在你的 `app` 配置文件中,把 `Socialite` facade 加入到 `aliases` 數組中。 ```php 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ``` 你還需要為你應用使用的 OAuth 服務加入憑據。這些憑據應該放在你的 `config/services.php` 文件中,并且使用 `facebook` , `twitter` , `linkedin` , `google` , `github` 或 `bitbucket` 作為鍵名,具體取決于在你的應用中由哪個程序來提供驗證服務,比如: ```php 'github' => [ 'client_id' => 'your-github-app-id', 'client_secret' => 'your-github-app-secret', 'redirect' => 'http://your-callback-url', ], ``` <a name="basic-usage"></a> ### 基本用法 接下來,你已經準備好了驗證用戶了!你需要兩個路由:一個重定向用戶到 OAuth 提供商,另一個在提供商驗證之后接收回調。我們用 `Socialite` facade 來訪問: ```php <?php namespace App\Http\Controllers\Auth; use Socialite; class LoginController extends Controller { /** * Redirect the user to the GitHub authentication page. * * @return Response */ public function redirectToProvider() { return Socialite::driver('github')->redirect(); } /** * Obtain the user information from GitHub. * * @return Response */ public function handleProviderCallback() { $user = Socialite::driver('github')->user(); // $user->token; } } ``` `redirect` 方法負責發送用戶到 OAuth 提供商,而 `user` 方法將讀取傳入的請求并從提供商處檢索用戶信息。在重定向用戶之前,你還可以加入附加的 `scope` 方法來設置請求的「scope」。這個方法會覆蓋所有現有的范圍。 ```php return Socialite::driver('github') ->scopes(['scope1', 'scope2'])->redirect(); ``` 你可以使用 `setScopes` 方法覆蓋所有已經存在的 scopes: ```php return Socialite::driver('github') ->setScopes(['scope1', 'scope2'])->redirect(); ``` 當然,你需要定義通往你的控制器方法的路由。 ```php Route::get('login/github', 'Auth\LoginController@redirectToProvider'); Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback'); ``` 一部分 OAuth 提供商在重定向請求中支持攜帶可選參數。要在請求中包含任何可選參數,調用 `with` 方法時傳入可選的數組即可。 ```php return Socialite::driver('google') ->with(['hd' => 'example.com'])->redirect(); ``` 當使用 `with` 方法時,注意不要傳遞保留關鍵字,比如 `state` 或 `response_type` 。 <a name="stateless-authentication"></a> #### 無狀態身份驗證 `stateless` 方法可以用于禁用 session 狀態的驗證,這個方法在向 API 添加社會化身份驗證時非常有用。 ```php return Socialite::driver('google')->stateless()->user(); ``` <a name="retrieving-user-details"></a> #### 檢索用戶詳細信息 一旦你有了一個用戶實例,你可以獲取這個用戶的更多詳細信息: ```php $user = Socialite::driver('github')->user(); // OAuth Two Providers $token = $user->token; $refreshToken = $user->refreshToken; // not always provided $expiresIn = $user->expiresIn; // OAuth One Providers $token = $user->token; $tokenSecret = $user->tokenSecret; // All Providers $user->getId(); $user->getNickname(); $user->getName(); $user->getEmail(); $user->getAvatar(); ``` <a name="retrieving-user-details-from-token"></a> #### 從令牌檢索用戶詳細信息 如果你已經有了一個用戶的有效訪問令牌,你可以使用 `userFromToken` 方法檢索用戶的詳細信息。 ```php $user = Socialite::driver('github')->userFromToken($token); ``` ## 譯者署名 | 用戶名 | 頭像 | 職能 | 簽名 | |---|---|---|---| | [@qufo](https://github.com/qufo) | <img class="avatar-66 rm-style" src="https://avatars1.githubusercontent.com/u/2526883?v=3&s=460?imageView2/1/w/100/h/100"> | 翻譯 | 歡迎共同探討。[@Qufo](https://github.com/qufo) | --- > {note} 歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動共創開源社區。 > > 轉載請注明:本文檔由 Laravel China 社區 [laravel-china.org](https://laravel-china.org) 組織翻譯,詳見 [翻譯召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)。 > > 文檔永久地址: https://d.laravel-china.org
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看