protected function eventListener() { $app = $this->app; $app->on(OAuthEvents::USER, function ($event) use($app) { $this->usrToken = $event->getToken(); $this->email = $this->usrToken->getEmail(); $this->name = $this->usrToken->getUsername(); $this->usrId = $this->usrToken->getUid(); try { // // Login user $user = $this->usrManager->loadUserByUsername($this->email ?: $this->name); $this->user = $this->usrManager->refreshUser($user); $this->usrToken->setUser($this->user); $this->user->setConfirmationToken(null); $this->user->setEnabled(true); $this->usrManager->loginAsUser($this->user); $this->logger(" Try to login user from " . $this->usrToken->getService()); } catch (UsernameNotFoundException $e) { // // Register new user $this->generatePassword(); $this->user = $this->usrManager->createUser($this->email, $this->password, $this->name); $this->usrToken->setUser($this->user); $this->user->setConfirmationToken(null); $this->user->setEnabled(true); $this->user->setUsername($this->usrId); $this->usrManager->insert($this->user); $this->usrManager->loginAsUser($this->user); $app['session']->getFlashBag()->set('alert', 'Account created.'); $this->logger(" Try to register user from " . $this->usrToken->getService()); } if ($this->app['user']) { $this->setSuccessHandler($this->usrToken->getService()); } // TODO send an email with information about the user $app->after(function () use($app) { $this->usrManager->loginAsUser($this->user); if ($this->usrManager->isLoggedIn()) { $newUrl = $app['url_generator']->generate('user.view', array('id' => $this->user->getId())); return new RedirectResponse($newUrl); } }); }); return $app; }