VOOZH about

URL: https://dzone.com/articles/implementing-user-sign-up-in-vaadin-flow

โ‡ฑ Implementing User Sign Up in Vaadin Flow Applications (Simple)


Related

  1. DZone
  2. Coding
  3. Java
  4. Implementing User Sign Up in Vaadin Flow Applications (Simple)

Implementing User Sign Up in Vaadin Flow Applications (Simple)

An experienced developer gives a tutorial on creating a simple implementation of user registration (sign up) in Vaadin Flow Java web applications.

Likes
Comment
Save
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

After sign in (or log in), sign up is probably the next logical use case in the user management workflow of a web application. In this article, I'll summarize the key points that I explain in detail in this video:

Implementing a Sign Up/Register View

A good first step in implementing user sign-up is to add a view so you can test the implementation as you code it. The view should include the required fields to create a user in your database and (if you are using passwords), an extra field to confirm the password, and the logic to validate the input. All the logic for registering the user should be delegated to a service class. Here's an example:

Java




xxxxxxxxxx
1
40


1
@Route("register")
2
public class RegisterView extends Composite {
3

 
4
    private final AuthService authService;
5

 
6
    public RegisterView(AuthService authService) {
7
        this.authService = authService;
8
   }
9

 
10
    @Override
11
    protected Component initContent() {
12
        TextField username = new TextField("Username");
13
        PasswordField password1 = new PasswordField("Password");
14
        PasswordField password2 = new PasswordField("Confirm password");
15
        return new VerticalLayout(
16
                new H2("Register"),
17
                username,
18
                password1,
19
                password2,
20
                new Button("Send", event -> register(
21
                        username.getValue(),
22
                        password1.getValue(),
23
                        password2.getValue()
24
               ))
25
       );
26
   }
27

 
28
    private void register(String username, String password1, String password2) {
29
        if (username.trim().isEmpty()) {
30
            Notification.show("Enter a username");
31
       } else if (password1.isEmpty()) {
32
            Notification.show("Enter a password");
33
       } else if (!password1.equals(password2)) {
34
            Notification.show("Passwords don't match");
35
       } else {
36
            authService.register(username, password1);
37
            Notification.show("Check your email.");
38
       }
39
   }
40
}


Implementing the Backend Service

In this example, the service job is to persist a new User in the database. In later articles, I'll show you more sophisticated implementations. Here's the register method:

Java




x


1
@Service
2
public class AuthService {
3

 
4
    public record AuthorizedRoute(String route, String name, Class<? extends Component> view) {
5

 
6
    private final UserRepository userRepository;
7

 
8
    public AuthService(UserRepository userRepository) {
9
        this.userRepository = userRepository;
10
   }
11

 
12
   ...
13

 
14
    public void register(String username, String password) {
15
        userRepository.save(new User(username, password, Role.USER));
16
   }
17

 
18
}


Summary

This is the first iteration on implementing more realistic user sign-in in Vaadin Flow applications. In the next article, I'll show you how to generate activation links that you can send the user in order to complete the registration process.

application Vaadin Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • How to Create a Custom React Component in Vaadin Flow
  • Blink a LED on a Raspberry Pi With Vaadin
  • What D'Hack Is DPoP?
  • MDC Logging With MuleSoft Runtime 4.4

Partner Resources

ร—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: