framgia-education / laravel-omniauth-hrsystem
Framgia Education Laravel 封装OAuth 2库。
v1.0
2017-05-29 09:05 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
- illuminate/contracts: ~5.4
- illuminate/http: ~5.4
- illuminate/support: ~5.4
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0|~5.0
This package is not auto-updated.
Last update: 2024-09-20 19:30:09 UTC
README
要开始使用 FramgiaAuth,请使用 Composer 将包添加到项目的依赖项中
composer require framgia-education/laravel-omniauth-hrsystem
配置
安装 FramgiaAuth 库后,在 config/app.php
配置文件中注册 Framgia\Education\Auth\FramgiaAuthServiceProvider
'providers' => [ // Other service providers... Framgia\Education\Auth\FramgiaAuthServiceProvider::class, ],
此外,将 FAuth
门面添加到 app
配置文件中的 aliases
数组
'aliases' => [ // Other aliases 'FAuth' => Framgia\Education\Auth\Facades\FramgiaAuth::class, ],
您还需要添加应用程序使用的OAuth服务的凭据。这些凭据应放在您的 config/services.php
配置文件中,并使用键 framgia
。例如
'framgia' => [ 'client_id' => 'your-framgia-auth-app-id', 'client_secret' => 'your-framgia-auth-app-secret', 'redirect' => 'http://your-callback-url', ],
基本用法
接下来,您就准备好认证用户了!您需要两个路由:一个用于将用户重定向到OAuth提供程序,另一个用于接收提供程序认证后的回调。我们将使用 FAuth
门面访问 Framgia Auth
<?php namespace App\Http\Controllers\Auth; use FAuth; class LoginController extends Controller { /** * Redirect the user to the GitHub authentication page. * * @return Response */ public function redirectToFramgiaAuth() { return FAuth::redirect(); } /** * Obtain the user information from GitHub. * * @return Response */ public function handleFramgiaAuthCallback() { $user = FAuth::user(); // $user->token; } }
redirect
方法负责将用户发送到 Framgia Auth 提供程序,而 user
方法将读取传入的请求并从提供程序检索用户信息。
当然,您需要定义到您控制器方法的路由
Route::get('login/framgia', 'Auth\LoginController@redirectToFramgiaAuth'); Route::get('login/framgia/callback', 'Auth\LoginController@handleFramgiaAuthCallback');
检索用户详细信息
一旦您有了用户实例,您就可以获取更多关于用户的信息
$user = FAuth::user(); $token = $user->token; $refreshToken = $user->refreshToken; // not always provided $expiresIn = $user->expiresIn; // Example infomation: $user->getId(); // Or maybe $user->id $user->getName(); // Or maybe $user->name $user->getEmail(); // Or maybe $user->email $user->getAvatar(); // Or maybe $user->avatar $user->getGender(); // Or maybe $user->gender $user->getBirthday(); // Or maybe $user->birthday $user->getPhoneNumber(); // Or maybe $user->phoneNumber // All infomation about user will be stored here: $user->getRaw(); // Or maybe $user->user
从令牌检索用户详细信息
如果您已经有一个用户的有效访问令牌,您可以使用 userFromToken
方法检索他们的详细信息
$user = FAuth::userFromToken($token);