|
1 | 1 | package life.qbic.datamanager.views.login;
|
2 | 2 |
|
3 |
| -import com.vaadin.flow.router.BeforeEvent; |
4 |
| -import java.util.List; |
5 |
| -import java.util.Map; |
| 3 | +import java.util.Objects; |
6 | 4 | import life.qbic.datamanager.Application;
|
7 |
| -import life.qbic.datamanager.views.AppRoutes; |
8 |
| -import life.qbic.datamanager.views.AppRoutes.Projects; |
9 |
| -import life.qbic.datamanager.views.notifications.ErrorMessage; |
10 |
| -import life.qbic.datamanager.views.notifications.InformationMessage; |
11 |
| -import life.qbic.identity.application.user.registration.ConfirmEmailInput; |
12 |
| -import life.qbic.identity.application.user.registration.ConfirmEmailOutput; |
13 | 5 | import life.qbic.logging.api.Logger;
|
14 | 6 | import life.qbic.logging.service.LoggerFactory;
|
15 | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
|
22 | 14 | * @since 1.0.0
|
23 | 15 | */
|
24 | 16 | @Component
|
25 |
| -public class LoginHandler implements LoginHandlerInterface, ConfirmEmailOutput { |
| 17 | +public class LoginHandler { |
26 | 18 |
|
27 | 19 | private static final Logger logger = LoggerFactory.logger(Application.class.getName());
|
28 | 20 |
|
29 |
| - private LoginLayout registeredLoginView; |
30 |
| - |
31 |
| - private final ConfirmEmailInput confirmEmailInput; |
32 | 21 |
|
33 | 22 | private final String emailConfirmationParameter;
|
34 | 23 |
|
35 | 24 | @Autowired
|
36 |
| - LoginHandler(ConfirmEmailInput confirmEmailInput, |
37 |
| - @Value("${EMAIL_CONFIRMATION_PARAMETER:confirm-email}") String emailConfirmationParameter) { |
38 |
| - this.confirmEmailInput = confirmEmailInput; |
39 |
| - this.emailConfirmationParameter = emailConfirmationParameter; |
40 |
| - } |
41 |
| - |
42 |
| - @Override |
43 |
| - public void handle(LoginLayout loginView) { |
44 |
| - if (registeredLoginView != loginView) { |
45 |
| - registeredLoginView = loginView; |
46 |
| - } |
47 |
| - initFields(); |
48 |
| - addListener(); |
49 |
| - } |
50 |
| - |
51 |
| - private void initFields() { |
52 |
| - clearNotifications(); |
53 |
| - } |
54 |
| - |
55 |
| - private void showInvalidCredentialsError() { |
56 |
| - showError("Incorrect username or password", "Please try again."); |
57 |
| - } |
58 |
| - |
59 |
| - private void showEmailConfirmationInformation() { |
60 |
| - showInformation("Email address confirmed", "You can now login with your credentials."); |
61 |
| - } |
62 |
| - |
63 |
| - private void showEmailConfirmationReminder() { |
64 |
| - showInformation("Registration mail sent", |
65 |
| - "Please check your mail inbox to confirm your registration"); |
66 |
| - } |
67 |
| - |
68 |
| - public void clearNotifications() { |
69 |
| - registeredLoginView.notificationLayout.removeAll(); |
70 |
| - } |
71 |
| - |
72 |
| - public void showError(String title, String description) { |
73 |
| - clearNotifications(); |
74 |
| - ErrorMessage errorMessage = new ErrorMessage(title, description); |
75 |
| - registeredLoginView.notificationLayout.add(errorMessage); |
76 |
| - } |
77 |
| - |
78 |
| - public void showInformation(String title, String description) { |
79 |
| - clearNotifications(); |
80 |
| - InformationMessage informationMessage = new InformationMessage(title, description); |
81 |
| - registeredLoginView.notificationLayout.add(informationMessage); |
82 |
| - } |
83 |
| - |
84 |
| - private void addListener() { |
85 |
| - registeredLoginView.addLoginListener(it -> |
86 |
| - onLoginSucceeded()); |
87 |
| - registeredLoginView.addForgotPasswordListener( |
88 |
| - it -> it.getSource().getUI().ifPresent(ui -> ui.navigate(AppRoutes.RESET_PASSWORD))); |
89 |
| - } |
90 |
| - |
91 |
| - private void onLoginSucceeded() { |
92 |
| - clearNotifications(); |
93 |
| - registeredLoginView.getUI().ifPresentOrElse( |
94 |
| - ui -> ui.navigate(Projects.PROJECTS), |
95 |
| - () -> logger.error("No UI found!")); |
| 25 | + LoginHandler(@Value("${EMAIL_CONFIRMATION_PARAMETER:confirm-email}") String emailConfirmationParameter) { |
| 26 | + this.emailConfirmationParameter = Objects.requireNonNull(emailConfirmationParameter); |
96 | 27 | }
|
97 | 28 |
|
98 |
| - @Override |
99 |
| - public void handle(BeforeEvent beforeEvent) { |
100 |
| - Map<String, List<String>> queryParams = beforeEvent.getLocation().getQueryParameters() |
101 |
| - .getParameters(); |
102 |
| - if (queryParams.containsKey("error")) { |
103 |
| - showInvalidCredentialsError(); |
104 |
| - } |
105 |
| - if (queryParams.containsKey(emailConfirmationParameter)) { |
106 |
| - String userId = queryParams.get(emailConfirmationParameter).iterator().next(); |
107 |
| - confirmEmailInput.confirmEmailAddress(userId); |
108 |
| - } |
109 |
| - if (queryParams.containsKey("userRegistered")) { |
110 |
| - showEmailConfirmationReminder(); |
111 |
| - } |
| 29 | + public String emailConfirmationParameter() { |
| 30 | + return emailConfirmationParameter; |
112 | 31 | }
|
113 | 32 |
|
114 |
| - @Override |
115 |
| - public void onEmailConfirmationSuccess() { |
116 |
| - showEmailConfirmationInformation(); |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void onEmailConfirmationFailure(String reason) { |
121 |
| - showError("Email confirmation failed", reason); |
122 |
| - } |
123 | 33 | }
|
0 commit comments