LoginAction.vala 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Inversion;
  2. using Statum;
  3. using Spry.Authentication;
  4. namespace Spry.Actions {
  5. /**
  6. * Verifies the `username`/`password` form fields and establishes the
  7. * SESSION `auth` slot via {@link Spry.SpryAuth.login}, then navigates to
  8. * {@link landing_uri}.
  9. *
  10. * Unknown usernames and wrong passwords both answer the generic
  11. * {@link invalid_credentials_message} notify, so the response leaks no
  12. * account existence.
  13. */
  14. public class LoginAction : StatumAction {
  15. private UserService users = inject<UserService>();
  16. /** URI navigated to after a successful login. */
  17. protected virtual string landing_uri { get { return "/"; } }
  18. /** Notification shown when the credentials do not match an account. */
  19. protected virtual string invalid_credentials_message { get { return "Invalid username or password"; } }
  20. public override async DirectiveBuilder handle() throws GLib.Error {
  21. string username = "";
  22. string password = "";
  23. if (request.form != null) {
  24. username = request.form.get_field("username") ?? "";
  25. password = request.form.get_field("password") ?? "";
  26. }
  27. var user = yield users.authenticate_user(username, password);
  28. if (user == null) {
  29. return directives().notify("error", invalid_credentials_message);
  30. }
  31. return SpryAuth.login(directives(), (!)user, action_registry).navigate(landing_uri);
  32. }
  33. }
  34. }