dongxing1853 2019-07-27 03:25
浏览 122
已采纳

调用未定义的方法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

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\User;
  4. use Socialite;
  5. use App\SocialAccount;
  6. use App\Http\Resources\UserResource;
  7. class SocialAuthController extends Controller
  8. {
  9. ...
  10. public function handleProviderCallback($provider)
  11. {
  12. $socialUser = Socialite::driver($provider)->stateless()->user();
  13. $userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
  14. /*
  15. if account exist, return the social account user
  16. else create the user account, then return the new user
  17. */
  18. if ($userSocialAccount) {
  19. // generate access token for use
  20. $token = $socialUser->createToken('********')->accessToken;
  21. // return access token & user data
  22. return response()->json([
  23. 'token' => $token,
  24. 'user' => (new UserResource($userSocialAccount))
  25. ]);
  26. } else {
  27. $user = User::create([
  28. 'firstname' => $socialUser->name,
  29. 'lastname' => $socialUser->name,
  30. 'username' => $socialUser->email,
  31. 'email_verified_at' => now()
  32. ]);
  33. if ($user) {
  34. SocialAccount::create([
  35. 'provider_id' => $socialUser->id,
  36. 'provider_name' => $provider,
  37. 'user_id' => $user->id
  38. ]);
  39. }
  40. // assign passport token to user
  41. $token = $user->createToken('********')->accessToken;
  42. return response()->json(['token' => $token, 'user' => new UserResource($user)]);
  43. }
  44. }
  45. }

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 06: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

    1. public function handleProviderCallback($provider)
    2. {
    3. // retrieve social user info
    4. $socialUser = Socialite::driver($provider)->stateless()->user();
    5. // check if social user provider record is stored
    6. $userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
    7. if ($userSocialAccount) {
    8. // retrieve the user from users store
    9. $user = User::find($userSocialAccount->user_id);
    10. // assign access token to user
    11. $token = $user->createToken('Pramopro')->accessToken;
    12. // return access token & user data
    13. return response()->json([
    14. 'token' => $token,
    15. 'user' => (new UserResource($user))
    16. ]);
    17. } else {
    18. ...
    19. }
    20. }

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

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部