LoginController.cpp 2.8 KB

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