Guest User

Untitled

a guest
Dec 5th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. <?php
  2. /**
  3. * Obtain the user information from sns.
  4. *
  5. * @return Response
  6. */
  7. public function handleSocialiteCallback($sns_name)
  8. {
  9. try{
  10. // ユーザー情報を取得
  11. $facebook_user = Socialite::driver($sns_name)->user();
  12.  
  13. // 取得したメールアドレスを持つユーザーの取得。存在しなければユーザー作成
  14. $user = \App\User::firstOrCreate([
  15. 'email' => $facebook_user->getEmail()
  16. ]);
  17.  
  18. // 新規ユーザーの場合
  19. if (\App\User::find($user->id) == null) {
  20. $user->name = $facebook_user->getName();
  21. $user->password = uniqid();
  22. $user->save();
  23. }
  24.  
  25. Auth::login($user);
  26. return redirect("成功した場合のリダイレクト先");
  27. } catch (\Exception $ex) {
  28. return redirect("失敗した場合のリダイレクト先");
  29. }
  30. }
Add Comment
Please, Sign In to add comment