Skip to content

Commit 893d2f6

Browse files
committed
fix: PR review feedback
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent c9e007a commit 893d2f6

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

app/Services/Auth/UserService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,17 @@ public function registerUser(array $payload, ?OAuth2OTP $otp = null):User
218218
* @param string $token
219219
* @return User
220220
* @throws EntityNotFoundException
221-
* @throws ValidationException
221+
* @throws ValidationException|\Exception
222222
*/
223223
public function verifyEmail(string $token): User
224224
{
225225
return $this->tx_service->transaction(function () use ($token) {
226226
$user = $this->user_repository->getByVerificationEmailToken($token);
227227

228-
if (is_null($user) || !$user->isActive())
228+
if (is_null($user) || !$user->isActive()) {
229+
Log::warning(sprintf("UserService::verifyEmail user with id %s is not active", $user->getId()));
229230
throw new EntityNotFoundException();
231+
}
230232

231233
$user->verifyEmail();
232234

resources/js/login/login.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Grid from '@material-ui/core/Grid';
2424
import CustomSnackbar from "../components/custom_snackbar";
2525
import Banner from '../components/banner/banner';
2626
import OtpInput from 'react-otp-input';
27-
import {handleThirdPartyProvidersVerbiage} from '../utils';
27+
import {handleErrorResponse, handleThirdPartyProvidersVerbiage} from '../utils';
2828

2929
import styles from './login.module.scss'
3030
import "./third_party_identity_providers.scss";
@@ -605,21 +605,22 @@ class LoginPage extends React.Component {
605605
let {user_name} = this.state;
606606
user_name = user_name?.trim();
607607

608+
if (!user_name) {
609+
this.showAlert(
610+
'Something went wrong while trying to resend the verification email. Please try again later.',
611+
'error');
612+
return;
613+
}
614+
608615
resendVerificationEmail(user_name, this.props.token).then((payload) => {
609616
this.showAlert(
610617
'We\'ve sent you a verification email. Please check your inbox and click the link to verify your account.',
611618
'success');
612619
}, (error) => {
613-
let {response, status} = error;
614-
if(status === 412){
615-
const {errors} = response.body;
616-
const allErrors = Object.values(errors ?? {})
617-
?.flat?.()
618-
?.join(', ') || '';
619-
this.showAlert(allErrors, 'error');
620-
return;
621-
}
622-
this.showAlert('Oops... Something went wrong!', 'error');
620+
handleErrorResponse(error, (title, messageLines, type) => {
621+
const message = (messageLines ?? []).join(', ')
622+
this.showAlert(`${title}: ${message}`, type);
623+
});
623624
});
624625
}
625626

resources/js/utils.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import Swal from "sweetalert2";
22

3-
export const handleErrorResponse = (err) => {
4-
if(err.status === 412){
5-
// validation error
6-
let msg= '';
7-
for (let [key, value] of Object.entries(err.response.body.errors)) {
8-
if (isNaN(key)) {
9-
msg += key + ': ';
10-
}
3+
const createErrorHandler = (customHandler = null) => {
4+
const showMessage = (title, message, type) => {
5+
if (customHandler && typeof customHandler === 'function') {
6+
return customHandler(title, message, type);
7+
}
8+
const formattedMessage = Array.isArray(message) ? message.join('<br>') : message;
9+
return Swal(title, formattedMessage, type);
10+
};
1111

12-
msg += value + '<br>';
12+
return (err) => {
13+
if (err.status === 412) {
14+
// validation error
15+
const messageLines = [];
16+
for (let [key, value] of Object.entries(err.response.body.errors)) {
17+
let line = '';
18+
if (isNaN(key)) {
19+
line += key + ': ';
20+
}
21+
line += value;
22+
messageLines.push(line);
23+
}
24+
return showMessage("Validation error", messageLines, "warning");
1325
}
14-
return Swal("Validation error", msg, "warning");
15-
}
16-
return Swal("Something went wrong!", null, "error");
17-
}
26+
return showMessage("Something went wrong!", null, "error");
27+
};
28+
};
29+
30+
export const handleErrorResponse = (err, customHandler = null) => {
31+
const errorHandler = createErrorHandler(customHandler);
32+
return errorHandler(err);
33+
};
1834

1935
/**
2036
*

0 commit comments

Comments
 (0)