public function testUniqueUsername() { // Insert some test users to $this->createUser(['username' => 'Foo Baz']); $this->createUser(['username' => 'Bar Foo']); $this->createUser(['username' => 'BarFoo']); $this->createUser(['username' => 'Bar Baz']); $this->createUser(['username' => 'BarBaz']); $this->createUser(['username' => 'Bar.Baz']); $this->createUser(['username' => 'foo']); $this->createUser(['username' => 'bar']); $this->createUser(['username' => 'bar 1']); $this->createUser(['username' => 'bar 2']); $user = new User(); $user->map(['user' => ['first_name' => '', 'last_name' => '']]); $user->name = 'Foo Bar'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('Foo Bar', $socialUser->username); $user->name = 'Foo Baz'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('FooBaz', $socialUser->username); $user->name = 'Bar Baz'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('Baz Bar', $socialUser->username); $this->createUser(['username' => 'Baz Bar']); $user->name = 'Bar Baz'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('BBaz', $socialUser->username); $user->name = 'Foo'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('Foo 1', $socialUser->username); $user->name = 'bar'; $socialUser = ParserFactory::parse($user, 'facebook'); $this->assertEquals('bar 3', $socialUser->username); }
/** * Obtain the user information from Provider. * * @param string $provider * @param Socialite|SocialiteManager $socialite * @param User $userModel * * @throws UnprocessableEntityException * * @return ApiResponse */ public function handleProviderCallback($provider, Socialite $socialite, User $userModel) { $this->validateProvider($provider); $socialUser = $socialite->with($provider)->user(); // Verify so we received an email address, if using oAuth credentials // with Twitter for instance, that isn't whitelisted, no email // address will be returned with the response. // See the notes in Spira API doc under Social Login for more info. if (!$socialUser->email) { // The app is connected with the service, but the 3rd party service // is not configured or allowed to return email addresses, so we // can't process the data further. Let's throw an exception. \Log::critical('Provider ' . $provider . ' does not return email.'); throw new UnprocessableEntityException('User object has no email'); } // Parse the social user to fit within Spira's user model $socialUser = ParserFactory::parse($socialUser, $provider); // Get or create the Spira user from the social login try { $user = $userModel->findByEmail($socialUser->email); } catch (ModelNotFoundException $e) { $user = $userModel->newInstance(); $user->fill(array_merge($socialUser->toArray(), ['user_type' => 'guest'])); $user->save(); } $socialLogin = new SocialLogin(['provider' => $provider, 'token' => $socialUser->token]); $user->addSocialLogin($socialLogin); // Prepare response data $token = $this->jwtAuth->fromUser($user, ['method' => $provider]); $returnUrl = $socialite->with($provider)->getCachedReturnUrl() . '?jwtAuthToken=' . $token; $response = $this->getResponse(); $response->redirect($returnUrl, 302); return $response; }