コード例 #1
0
 /**
  * Obtain the user information from Tokenpass.
  * 
  * This is the route called after Tokenpass has granted (or denied) permission to this application
  * This application is now responsible for loading the user information from Tokenpass and storing
  * it in the local user database.
  *
  * @return Response
  */
 public function handleProviderCallback(Request $request)
 {
     try {
         // check for an error returned from Tokenpass
         $error_description = Tokenpass::checkForError($request);
         if ($error_description) {
             return view('account.authorization-failed', ['error_msg' => $error_description]);
         }
         // retrieve the user from Tokenpass
         $oauth_user = Socialite::user();
         // get all the properties from the oAuth user object
         $tokenly_uuid = $oauth_user->id;
         $oauth_token = $oauth_user->token;
         $username = $oauth_user->user['username'];
         $name = $oauth_user->user['name'];
         $email = $oauth_user->user['email'];
         $email_is_confirmed = $oauth_user->user['email_is_confirmed'];
         // find an existing user based on the credentials provided
         $existing_user = User::where('tokenly_uuid', $tokenly_uuid)->first();
         // if an existing user wasn't found, we might need to find a user to merge into
         $mergable_user = $existing_user ? null : User::where('username', $username)->orWhere('email', $email)->where('tokenly_uuid', null)->first();
         if ($existing_user) {
             // update the user
             $existing_user->update(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
             // login
             Auth::login($existing_user);
         } else {
             if ($mergable_user) {
                 // an existing user was found with a matching username
                 //  migrate it to tokenpass
                 if ($mergable_user['tokenly_uuid']) {
                     throw new Exception("Can't merge a user already associated with a different tokenly account", 1);
                 }
                 // update if needed
                 $mergable_user->update(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
                 // login
                 Auth::login($mergable_user);
             } else {
                 // no user was found - create a new user based on the information we received
                 //make sure these fields are all "fillable" in your User model
                 $new_user = User::create(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
                 // login
                 Auth::login($new_user);
             }
         }
         return redirect('/account/login');
     } catch (Exception $e) {
         // some unexpected error happened
         return view('account.authorization-failed', ['error_msg' => 'Failed to authenticate this user.']);
     }
 }