public class LoginForm
A simple login form which shows a simple login form; calls a handler provided in the LoginForm.onLogin when user clicks the "Sign In" button.
There are two ways to use this form. If the whole app is user-protected (an user must log in to view any view of the app, there are no views that an anonymous user may view), then it is simply possible to show the form as a full-screen in the UI if no user is logged in:
class MyUI : UI() {
override fun init(request: VaadinRequest) {
if (Session.loggedInUser == null) {
content = LoginView()
return;
} else {
content = MainLayout() // create a main layout, populate the menu, etc
navigator = Navigator(this, content as ViewProvider)
navigator.addProvider(autoViewProvider)
VokSecurity.install()
// you need to install the error handler which would handle AccessRejectedException
}
}
class LoginView : VerticalLayout() {
init {
setSizeFull()
loginForm("My App") {
alignment = Alignment.MIDDLE_CENTER
onLogin { username, password ->
val user = User.findByUsername(username)
if (user == null) {
usernameField.componentError = UserError("The user does not exist")
} else if (!user.passwordMatches(password)) {
passwordField.componentError = UserError("Invalid password")
} else {
Session.loggedInUser = user
Page.getCurrent().reload() // this will cause the UI to be re-created, but the user is now logged in so the MainLayout should be instantiated etc.
}
}
}
}
}
If only parts of the app are protected, you may simply show the LoginForm class in a Window, when your app-specific login button is pressed.
LoginForm.onLogin| Constructor and Description |
|---|
LoginForm(java.lang.String appName)
A simple login form which shows a simple login form; calls a handler provided in the
LoginForm.onLogin when user clicks the "Sign In" button. |
| Modifier and Type | Method and Description |
|---|---|
com.vaadin.ui.Label |
getAppNameLabel() |
com.vaadin.ui.Button |
getLoginButton() |
com.vaadin.ui.TextField |
getPasswordField() |
com.vaadin.ui.TextField |
getUsernameField() |
void |
onLogin(kotlin.jvm.functions.Function2<? super java.lang.String,? super java.lang.String,kotlin.Unit> loginHandler)
The loginHandler will try to log in the user with given username and password. Both are not blank and trimmed. If such user does not exist, or the password
does not match, just set the appropriate UserError to
LoginForm.getUsernameField or passwordField and bail out. Else,
log in the user (e.g. by storing the user into the session) and reload the page (com.vaadin.server.Page.reload) (so that the UI
is re-created and redraws the welcome page for the user, if the entire app is user-protected), or navigate to the user's welcome view. |
public LoginForm(java.lang.String appName)
A simple login form which shows a simple login form; calls a handler provided in the LoginForm.onLogin when user clicks the "Sign In" button.
There are two ways to use this form. If the whole app is user-protected (an user must log in to view any view of the app, there are no views that an anonymous user may view), then it is simply possible to show the form as a full-screen in the UI if no user is logged in:
class MyUI : UI() {
override fun init(request: VaadinRequest) {
if (Session.loggedInUser == null) {
content = LoginView()
return;
} else {
content = MainLayout() // create a main layout, populate the menu, etc
navigator = Navigator(this, content as ViewProvider)
navigator.addProvider(autoViewProvider)
VokSecurity.install()
// you need to install the error handler which would handle AccessRejectedException
}
}
class LoginView : VerticalLayout() {
init {
setSizeFull()
loginForm("My App") {
alignment = Alignment.MIDDLE_CENTER
onLogin { username, password ->
val user = User.findByUsername(username)
if (user == null) {
usernameField.componentError = UserError("The user does not exist")
} else if (!user.passwordMatches(password)) {
passwordField.componentError = UserError("Invalid password")
} else {
Session.loggedInUser = user
Page.getCurrent().reload() // this will cause the UI to be re-created, but the user is now logged in so the MainLayout should be instantiated etc.
}
}
}
}
}
If only parts of the app are protected, you may simply show the LoginForm class in a Window, when your app-specific login button is pressed.
LoginForm.onLoginpublic com.vaadin.ui.Label getAppNameLabel()
public com.vaadin.ui.TextField getUsernameField()
public com.vaadin.ui.TextField getPasswordField()
public com.vaadin.ui.Button getLoginButton()
public void onLogin(kotlin.jvm.functions.Function2<? super java.lang.String,? super java.lang.String,kotlin.Unit> loginHandler)
The loginHandler will try to log in the user with given username and password. Both are not blank and trimmed. If such user does not exist, or the password
does not match, just set the appropriate UserError to LoginForm.getUsernameField or passwordField and bail out. Else,
log in the user (e.g. by storing the user into the session) and reload the page (com.vaadin.server.Page.reload) (so that the UI
is re-created and redraws the welcome page for the user, if the entire app is user-protected), or navigate to the user's welcome view.
LoginForm.getUsernameField