|
| 1 | +--- |
| 2 | +title: Login and Logout Links |
| 3 | +description: How to create login and logout links or buttons using Control Center identity management security. |
| 4 | +order: 40 |
| 5 | +--- |
| 6 | + |
| 7 | + |
| 8 | += Login & Logout Links |
| 9 | + |
| 10 | +For users to login and logout, it can be useful to provide them with a link or button. This page explains how to do that in a Vaadin application when using the <<../identity-management#,Identity Management feature>> in Control Center. |
| 11 | + |
| 12 | + |
| 13 | +== Adding Login |
| 14 | + |
| 15 | +To add a login link or button to a Vaadin application, you'll need to create a component that redirects users to the login page provided by Control Center -- for when they click on the link or button. Here's an example of how you might do that: |
| 16 | + |
| 17 | +[source,java] |
| 18 | +---- |
| 19 | +public class MainLayout extends AppLayout { |
| 20 | +
|
| 21 | + public MainLayout() { |
| 22 | + /* <1> */ |
| 23 | + Button loginButton = new Button("Login", event -> { |
| 24 | + getUI().ifPresent(ui -> ui.getPage().setLocation("/oauth2/authorization/control-center")); |
| 25 | + }); |
| 26 | +
|
| 27 | + /* <2> */ |
| 28 | + Anchor loginLink = new Anchor("/oauth2/authorization/control-center", "Login"); |
| 29 | + loginLink.setRouterIgnore(true); |
| 30 | +
|
| 31 | + addToNavbar(loginButton, loginLink); |
| 32 | + } |
| 33 | +} |
| 34 | +---- |
| 35 | + |
| 36 | +<1> This part creates a login button. |
| 37 | +<2> This creates a login link. You may not want to do both a button and a link, though. |
| 38 | + |
| 39 | + |
| 40 | +== Adding Logout |
| 41 | + |
| 42 | +To add a logout link or button to a Vaadin application, you'll need to create a component that triggers the logout process. Here's an example of how to create a button, only: |
| 43 | + |
| 44 | +[source,java] |
| 45 | +---- |
| 46 | +public class MainLayout extends AppLayout { |
| 47 | +
|
| 48 | + public MainLayout(@Autowired AuthenticationContext authenticationContext) { |
| 49 | +
|
| 50 | + Button logoutButton = new Button("Logout", click -> authenticationContext.logout()); |
| 51 | +
|
| 52 | + addToNavbar(logoutButton); |
| 53 | + } |
| 54 | +} |
| 55 | +---- |
| 56 | + |
| 57 | + |
| 58 | +== Deploy the Application |
| 59 | + |
| 60 | +Deploy the application as described in <<../application-deployment#,Deploying a Vaadin Application with Control Center>>. Be sure to activate the Identity Management feature. |
0 commit comments