LoginController.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "../view/view.h"
  10. using namespace std;
  11. using namespace xc::processor;
  12. using namespace xc::processor::templates;
  13. using namespace xc::processor::templates::framework7;
  14. using namespace configor;
  15. namespace xc::controller {
  16. ResponseData *LoginController(RequestData request) {
  17. return new TemplateResponseData({
  18. Framework7Document({
  19. If(request.getCookie("Token") == "loginFailed", {
  20. OnLoadScript("app.dialog.alert('登陆失败,请检查用户名或密码')")
  21. }),
  22. BlockTitleView("需要登陆"),
  23. FormView({
  24. FormInputView("username", "用户名", "text", "输入您的用户名").id("username"),
  25. FormInputView("password", "密码", "password", "输入您的密码").id("password"),
  26. FormItemView({
  27. BlockView({
  28. ButtonView("登陆").onclick("doLogin('" + conf::userPasswordSalt + "')"),
  29. VerticalSpacer(10),
  30. Label({
  31. "如果您需要注册,请联系管理员,管理员请参考",
  32. Link("GitHub中的Readme").classAdd("link").onclick("window.open('https://github.com/XCBOSA/frp-webui-500k.git')"),
  33. "来创建账号。"
  34. })
  35. })
  36. })
  37. }).action("/").method("get").id("loginForm"),
  38. }, {
  39. Link("2023 © Frp-WebUI by XCBOSA")
  40. .classAdd("link")
  41. .onclick("window.open('https://github.com/XCBOSA/frp-webui-500k.git')")
  42. })
  43. });
  44. }
  45. ContentGeneratorDefineWithNameS("LoginController", false, LoginController(request))
  46. ResponseData *ValidAuthController(RequestData request) {
  47. string arg = request.getURLArgument("v");
  48. JsonModel model = json::parse(arg);
  49. string username = model["username"];
  50. string password = model["password"];
  51. string token = user::tryLogin(username, password);
  52. auto resp = new RedirectResponse("/");
  53. resp->addCookie("Token", token);
  54. return resp;
  55. }
  56. ContentGeneratorDefineS(request.getURLPath() == "/login", ValidAuthController(request))
  57. ResponseData *QuitLoginStateController(RequestData request) {
  58. auto resp = new RedirectResponse("/");
  59. resp->addCookie("Token", "");
  60. return resp;
  61. }
  62. ContentGeneratorDefineS(request.getURLPath() == "/quitLogin", QuitLoginStateController(request))
  63. }