laravel使用Socialite 社会化登录,如apple、Google等

官方文档:https://socialiteproviders.com/
一、apple、facebook、Google、line登录,先按照以下依赖包

composer require socialiteproviders/apple
composer require socialiteproviders/facebook
composer require socialiteproviders/google
composer require socialiteproviders/line

二、在 App\Providers\EventServiceProvider 注册事件监听,如图所示
在这里插入图片描述
在$list数组新增以下代码

\SocialiteProviders\Manager\SocialiteWasCalled::class => [
            // add your listeners (aka providers) here
            'SocialiteProviders\\Facebook\FacebookExtendSocialite@handle',
            'SocialiteProviders\Line\LineExtendSocialite@handle',
            'SocialiteProviders\\Google\\GoogleExtendSocialite@handle',
            'SocialiteProviders\\Apple\\AppleExtendSocialite@handle',
        ],

三、在config\services.php新增配置,如图所示
在这里插入图片描述
四、在 App\Http\Controllers 新增SocialiteController

<?php

namespace App\Http\Controllers;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Http\Request;
use Firebase\JWT\JWT;

class SocialiteController extends Controller
{
 	/**
     * 前往授权页面
     * @return mixed
     */
    public function googleRedirect(Request $request)
    {
     	return Socialite::driver("google")->redirect();
    }
    public function lineRedirect(Request $request)
    {
     	return Socialite::driver("line")->redirect();
    }
    public function facebookRedirect(Request $request)
    {
     	return Socialite::driver("facebook")->redirect();
    }
    public function appleRedirect(Request $request)
    {
     	return Socialite::driver("apple")->redirect();
    }
    /**
     * 授权回调
     */
    public function callback($type){
    	 if ($type == 'apple') {
    	  	config()->set('services.apple.client_secret', $this->appleGenerate());
    	 }
    	 $socialiteUser = Socialite::driver($type)->user();
    	 //获取openid
    	 //socialiteUser->getId();
    	 //获取昵称
    	 //socialiteUser->getNickname();
    	 //获取用户名
    	 //socialiteUser->getName();
    	 //获取邮箱
    	 //socialiteUser->getEmail();
    	 //获取头像
    	 //socialiteUser->getAvatar();
    }
    /**
     * apple 生成 client_secret
     */
	public function appleGenerate()
    {
        $teamId = config('services.apple.team_id');
        $clientId = config('services.apple.client_id');  //对应 Bundle ID
        $privateKey = config('services.apple.private_key'); //.p8文件里面的内容,格式要保持一致
        $keyId = config('services.apple.key_id');

        $payload = [
            'iss' => $teamId,
            'iat' => time(),
            'exp' => time() + 86400,
            'aud' => 'https://appleid.apple.com',
            'sub' => $clientId,
        ];
        return JWT::encode($payload, $privateKey, 'ES256', $keyId);
    }
}

五、在routes\web.php 添加路由

 Route::any('socialite/redirect/google', [ThirdPartyController::class, 'googleRedirect']);
 Route::any('socialite/redirect/line', [ThirdPartyController::class, 'lineRedirect']);
 Route::any('socialite/redirect/facebook', [ThirdPartyController::class, 'facebookRedirect']);
 Route::any('socialite/redirect/apple', [ThirdPartyController::class, 'appleRedirect']);
 Route::any('socialite/callback/{type}', [ThirdPartyController::class, 'callback']); //配置的回调地址,需要和配置 services中的redirect以及Google、apple等配置要一致,例如:在Google可以配置成:http://xxx.com/socialite/callback/google,配置 services中的Google的redirect配置就是http://xxx.com/socialite/callback/google
 

六、qq、微信、GitHub等其他登录类似

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Laravel 中实现验证码功能可以通过以下步骤来完成: 1. 首先,你需要安装一个验证码库,比如 popular-captcha 或 Socialite。你可以使用 Composer 运行以下命令来安装所需的库: ``` composer require mews/captcha ``` 2. 在 Laravel 中,你可以使用 Laravel 自带的 Validator 类或 captcha() 函数来对验证码进行验证。Validator 类提供了一个简单的方法来验证用户输入的验证码是否正确。你可以在控制器的验证方法中添加以下代码来进行验证码验证: ```php use Illuminate\Support\Facades\Validator; public function validateCaptcha(Request $request) { $validator = Validator::make($request->all(), [ 'captcha' => 'required|captcha', ]); if ($validator->fails()) { // 验证码验证失败处理 } else { // 验证码验证成功处理 } } ``` 3. 当用户输入错误的验证码时,你可以使用 Laravel 自带的 Validation Exception 来处理验证码错误。你可以在控制器方法中使用 try-catch 块来捕获验证码验证的异常,并在异常处理程序中给出相应的提示信息: ```php use Illuminate\Validation\ValidationException; public function validateCaptcha(Request $request) { try { $validator = Validator::make($request->all(), [ 'captcha' => 'required|captcha', ]); if ($validator->fails()) { throw ValidationException::withMessages([ 'captcha' => '验证码错误', ]); } // 验证码验证成功处理 } catch (ValidationException $e) { // 验证码验证失败处理 return redirect()->back()->withErrors($e->errors()); } } ``` 通过以上步骤,你可以在 Laravel 中实现验证码功能。你可以根据你的具体需求来定制验证码的生成和验证逻辑,并在验证失败时给出适当的提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗼唸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值