dongxing1853 2019-07-27 11:25
浏览 121
已采纳

调用未定义的方法Laravel \ Socialite \ Two \ User :: createToken()

In a laravel 5.8 API project, I want users to login via their social accounts. So far I have been able to use Socialite to retrieve user info from the provider and use it to create a new user record. But when I try to have the user log in again, it throws up the following error

Call to undefined method Laravel\Socialite\Two\User::createToken()

Here's the code I am working with

<?php

namespace App\Http\Controllers;

use App\User;
use Socialite;
use App\SocialAccount;
use App\Http\Resources\UserResource;

class SocialAuthController extends Controller
{
    ...

    public function handleProviderCallback($provider)
    {
        $socialUser = Socialite::driver($provider)->stateless()->user();

        $userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();

        /*
         if account exist, return the social account user
         else create the user account, then return the new user
        */
        if ($userSocialAccount) {

            // generate access token for use
            $token = $socialUser->createToken('********')->accessToken;

            // return access token & user data
            return response()->json([
                'token' => $token,
                'user'  => (new UserResource($userSocialAccount))
            ]);

        } else {

            $user = User::create([
                'firstname'            => $socialUser->name,
                'lastname'             => $socialUser->name,
                'username'             => $socialUser->email,
                'email_verified_at'    => now()
            ]);

            if ($user) {

                SocialAccount::create([
                    'provider_id'   => $socialUser->id,
                    'provider_name' => $provider,
                    'user_id'       => $user->id
                ]);
            }

            // assign passport token to user
            $token = $user->createToken('********')->accessToken;

            return response()->json(['token' => $token, 'user' => new UserResource($user)]);

        }
    }
}

I haven't been able to spot the reason why I am getting the error when the user attempts a second login but there is no error if it's the first time the user logs in with a social account.

Why does it complain about Laravel\Socialite\Two\User::createToken() method? If I try adding this line use Laravel\Socialite\Two\User vscode intelephsense flags it as a duplicate of App\User so what is really going on in my code?

  • 写回答

2条回答 默认 最新

  • dstk51319 2019-07-28 14:14
    关注

    First of all the problem I was having with having a token generated by passport for users authentication after the first social login was because I was calling the createToken method on the user returned by Socialite. As explained by @JorisJ1 Socialite does not have the createToken function so my initial code threw an error.

    Here's how I fixed it

    public function handleProviderCallback($provider)
    {
        // retrieve social user info
        $socialUser = Socialite::driver($provider)->stateless()->user();
    
        // check if social user provider record is stored
        $userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
    
        if ($userSocialAccount) {
    
            // retrieve the user from users store
            $user = User::find($userSocialAccount->user_id);
    
            // assign access token to user
            $token = $user->createToken('Pramopro')->accessToken;
    
            // return access token & user data
            return response()->json([
                'token' => $token,
                'user'  => (new UserResource($user))
            ]);
        } else {
            ...
        }
    }
    

    Comments are welcomed if there is a better way for adding social authentication to API.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大