try {
$createdUser = app('firebase.auth')->createUser($userProperties);
} catch (\Throwable $exception) {
throw ValidationException::withMessages(['email' => 'Email Already In Use']);
}
Spark classic: Check user with external service on registration
Hello there!
I'm relatively new to Laravel and PHP in general (coming from Node). I'm using spark classic v11 to migrate an old system to Laravel. The legacy system uses Firebase to manage users which should be able to use the new system just like the old one. I have been trying to check for credentials upon registration with following code in the boot() method of my SparkServiceProvider:
Spark::createUsersWith(function ($request) {
$user = Spark::user();
$data = $request->all();
$userProperties = [
'email' => $request['email'],
'password' => $request['password'],
'displayName' => $request['name'],
'emailVerified' => false,
'disabled' => false,
];
// Verification in Firebase
try {
$createdUser = app('firebase.auth')->createUser($userProperties);
} catch (\Throwable $exception) {
throw $exception;
//return $exception;
}
$user->forceFill([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'last_read_announcements_at' => Carbon::now(),
'trial_ends_at' => Carbon::now()->addDays(Spark::trialDays()),
])->save();
return $user;
});
The createUser from firebase.authmay return a firebase user record or an exception containing the error like the following:
{
"message": "The email address is already in use by another account.",
"exception": "Kreait\Firebase\Exception\Auth\EmailExists",
"file": "/var/www/html/vendor/kreait/firebase-php/src/Firebase/Exception/AuthApiExceptionConverter.php",
"line": 75,
"trace": [...]
}
I would like to pass the message prop to the frontend and display to the user but can't find a way to do so. I tried return back()->withErrors(['error' => $exception->message]) but that only redirects to http://localhost/undefined.
What could be the best way to pass down the error and notify the user?
Any help more than appreciated!!
Please or to participate in this conversation.