fixup! merge signup and google signup handling with username support

This commit is contained in:
Bence Pőcze 2023-09-24 01:12:30 +02:00
parent aca8bdb4b1
commit e2dd78fca3
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -271,6 +271,8 @@ class LoginController
return new JsonContent(['redirect' => ['target' => $this->redirectUrl]]); return new JsonContent(['redirect' => ['target' => $this->redirectUrl]]);
} }
$newUser = new User();
$googleUserData = \Container::$request->session()->get('google_user_data'); $googleUserData = \Container::$request->session()->get('google_user_data');
if ($googleUserData !== null) { if ($googleUserData !== null) {
$user = $this->userRepository->getByEmail($googleUserData['email']); $user = $this->userRepository->getByEmail($googleUserData['email']);
@ -285,9 +287,9 @@ class LoginController
]); ]);
} }
$active = true; $newUser->setActive(true);
$email = $googleUserData['email']; $newUser->setEmail($googleUserData['email']);
$googleSub = $googleUserData['sub']; $newUser->setGoogleSub($googleUserData['sub']);
} else { } else {
$user = $this->userRepository->getByEmailOrUsername(\Container::$request->post('email')); $user = $this->userRepository->getByEmailOrUsername(\Container::$request->post('email'));
@ -361,36 +363,31 @@ class LoginController
} }
} }
$active = false; $newUser->setActive(false);
$email = \Container::$request->post('email'); $newUser->setEmail(\Container::$request->post('email'));
$googleSub = null; $newUser->setPlainPassword(\Container::$request->post('password'));
} }
$user = new User(); $newUser->setUsername(strlen(\Container::$request->post('username')) > 0 ? \Container::$request->post('username') : Factory::create()->userName);
$user->setActive($active); $newUser->setCreatedDate(new DateTime());
$user->setEmail($email);
$user->setUsername(strlen(\Container::$request->post('username')) > 0 ? \Container::$request->post('username') : Factory::create()->userName);
$user->setPlainPassword(\Container::$request->post('password'));
$user->setGoogleSub($googleSub);
$user->setCreatedDate(new DateTime());
\Container::$persistentDataManager->saveToDb($user); \Container::$persistentDataManager->saveToDb($newUser);
if ($active) { if ($googleUserData !== null) {
$this->sendWelcomeEmail($user->getEmail()); $this->sendWelcomeEmail($newUser->getEmail());
\Container::$request->setUser($user); \Container::$request->setUser($user);
} else { } else {
$token = bin2hex(random_bytes(16)); $token = bin2hex(random_bytes(16));
$confirmation = new UserConfirmation(); $confirmation = new UserConfirmation();
$confirmation->setUser($user); $confirmation->setUser($newUser);
$confirmation->setToken($token); $confirmation->setToken($token);
$confirmation->setLastSentDate(new DateTime()); $confirmation->setLastSentDate(new DateTime());
\Container::$persistentDataManager->saveToDb($confirmation); \Container::$persistentDataManager->saveToDb($confirmation);
$this->sendConfirmationEmail($user->getEmail(), $token, $user->getCreatedDate()); $this->sendConfirmationEmail($newUser->getEmail(), $token, $newUser->getCreatedDate());
} }
\Container::$request->session()->delete('tmp_user_data'); \Container::$request->session()->delete('tmp_user_data');