LoginController.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Created by xcbosa on 2023/1/30.
  3. //
  4. #include "../processor/processor.h"
  5. #include "../webuiconf.h"
  6. #include "../processor/templates/framework7/Framework7Document.hpp"
  7. #include "../fs.hpp"
  8. #include "../user.hpp"
  9. using namespace std;
  10. using namespace xc::processor;
  11. using namespace xc::processor::templates;
  12. using namespace xc::processor::templates::framework7;
  13. using namespace configor;
  14. namespace xc::controller {
  15. ResponseData *LoginController(RequestData request) {
  16. return new TemplateResponseData({
  17. Framework7Document({
  18. BlockTitleView("需要登陆"),
  19. FormView({
  20. FormInputView("username", "用户名", "text", "输入您的用户名").id("username"),
  21. FormInputView("password", "密码", "password", "输入您的密码").id("password"),
  22. FormItemView({
  23. BlockView({
  24. ButtonView("登陆").onclick("doLogin('" + conf::userPasswordSalt + "')"),
  25. VerticalSpacer(10),
  26. Label("如果您需要注册,请联系管理员")
  27. })
  28. })
  29. }).action("/").method("get").id("loginForm"),
  30. }, {
  31. Link("2023 © Frp-WebUI by XCBOSA")
  32. .classAdd("link")
  33. .onclick("window.open('https://github.com/XCBOSA/frp-webui-500k.git')")
  34. })
  35. });
  36. }
  37. ContentGeneratorDefineWithNameS("LoginController", request.getURLPath() == "/", LoginController(request))
  38. ResponseData *ValidAuthController(RequestData request) {
  39. string arg = request.getURLArgument("v");
  40. JsonModel model = json::parse(arg);
  41. string username = model["username"];
  42. string password = model["password"];
  43. string token = user::tryLogin(username, password);
  44. // TemplateResponseData *resp = new TemplateResponseData({
  45. // If(token.empty(), {
  46. // ContentGeneratorReference("LoginController", request)
  47. // }, {
  48. // ContentGeneratorReference("PortListController", request)
  49. // })
  50. // });
  51. auto resp = new RedirectResponse("/");
  52. resp->addCookie("Token", token);
  53. return resp;
  54. }
  55. ContentGeneratorDefineS(request.getURLPath() == "/login", ValidAuthController(request))
  56. }