定制 adamwathan/eloquent-oauth 二次开发

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

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

adamwathan/eloquent-oauth

最新稳定版本:v8.1.0

Composer 安装命令:

composer require adamwathan/eloquent-oauth

包简介

Stupid simple OAuth authentication with Laravel and Eloquent

README 文档

README

Important: This package is not actively maintained. For bug fixes and new features, please fork.

Eloquent OAuth

This Project Has Been Deprecated. Code Climate Scrutinizer Code Quality Build Status

Eloquent OAuth is a package for Laravel designed to make authentication against various OAuth providers ridiculously brain-dead simple. Specify your client IDs and secrets in a config file, run a migration and after that it's just two method calls and you have OAuth integration.

Video Walkthrough

Screenshot

Basic example

// Redirect to Facebook for authorization
Route::get('facebook/authorize', function() {
    return OAuth::authorize('facebook');
});

// Facebook redirects here after authorization
Route::get('facebook/login', function() {
    
    // Automatically log in existing users
    // or create a new user if necessary.
    OAuth::login('facebook');

    // Current user is now available via Auth facade
    $user = Auth::user();

    return Redirect::intended();
});

Supported Providers

  • Facebook
  • GitHub
  • Google
  • LinkedIn
  • Instagram
  • SoundCloud

Feel free to open an issue if you would like support for a particular provider, or even better, submit a pull request.

Installation

Check the appropriate wrapper package for installation instructions for your version of Laravel.

Usage

Authentication against an OAuth provider is a multi-step process, but I have tried to simplify it as much as possible.

Authorizing with the provider

First you will need to define the authorization route. This is the route that your "Login" button will point to, and this route redirects the user to the provider's domain to authorize your app. After authorization, the provider will redirect the user back to your second route, which handles the rest of the authentication process.

To authorize the user, simply return the OAuth::authorize() method directly from the route.

Route::get('facebook/authorize', function() {
    return OAuth::authorize('facebook');
});

Authenticating within your app

Next you need to define a route for authenticating against your app with the details returned by the provider.

For basic cases, you can simply call OAuth::login() with the provider name you are authenticating with. If the user rejected your application, this method will throw an ApplicationRejectedException which you can catch and handle as necessary.

The login method will create a new user if necessary, or update an existing user if they have already used your application before.

Once the login method succeeds, the user will be authenticated and available via Auth::user() just like if they had logged in through your application normally.

use SocialNorm\Exceptions\ApplicationRejectedException;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;

Route::get('facebook/login', function() {
    try {
        OAuth::login('facebook');
    } catch (ApplicationRejectedException $e) {
        // User rejected application
    } catch (InvalidAuthorizationCodeException $e) {
        // Authorization was attempted with invalid
        // code,likely forgery attempt
    }

    // Current user is now available via Auth facade
    $user = Auth::user();

    return Redirect::intended();
});

If you need to do anything with the newly created user, you can pass an optional closure as the second argument to the login method. This closure will receive the $user instance and a SocialNorm\User object that contains basic information from the OAuth provider, including:

  • id
  • nickname
  • full_name
  • avatar
  • email
  • access_token
OAuth::login('facebook', function($user, $details) {
    $user->nickname = $details->nickname;
    $user->name = $details->full_name;
    $user->profile_image = $details->avatar;
    $user->save();
});

Note: The Instagram and Soundcloud APIs do not allow you to retrieve the user's email address, so unfortunately that field will always be null for those provider.

Advanced: Storing additional data

Remember: One of the goals of the Eloquent OAuth package is to normalize the data received across all supported providers, so that you can count on those specific data items (explained above) being available in the $details object.

But, each provider offers its own sets of additional data. If you need to access or store additional data beyond the basics of what Eloquent OAuth's default ProviderUserDetails object supplies, you need to do two things:

  1. Request it from the provider, by extending its scope:

    Say for example we want to collect the user's gender when they login using Facebook.

    In the config/eloquent-oauth.php file, set the [scope] in the facebook provider section to include the public_profile scope, like this:

       'scope' => ['email', 'public_profile'],

For available scopes with each provider, consult that provider's API documentation.

NOTE: By increasing the scope you will be asking the user to grant access to additional information. They will be informed of the scopes you're requesting. If you ask for too much unnecessary data, they may refuse. So exercise restraint when requesting additional scopes.

  1. Now where you do your OAuth::login, store the to your $user object by accessing the $details->raw()['KEY'] data:
       OAuth::login('facebook', function($user, $details) (
           $user->gender = $details->raw()['gender']; // Or whatever the key is
           $user->save();
       });

TIP: You can see what the available keys are by testing with dd($details->raw()); inside that same closure.

adamwathan/eloquent-oauth 适用场景与选型建议

adamwathan/eloquent-oauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59.48k 次下载、GitHub Stars 达 380, 最近一次更新时间为 2013 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 adamwathan/eloquent-oauth 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 59.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 384
  • 点击次数: 12
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 380
  • Watchers: 25
  • Forks: 52
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-12-18