LoginController.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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("如果您需要注册,请联系管理员,管理员请参考GitHub中的Readme来创建账号。")
  30. })
  31. })
  32. }).action("/").method("get").id("loginForm"),
  33. }, {
  34. Link("2023 © Frp-WebUI by XCBOSA")
  35. .classAdd("link")
  36. .onclick("window.open('https://github.com/XCBOSA/frp-webui-500k.git')")
  37. })
  38. });
  39. }
  40. ContentGeneratorDefineWithNameS("LoginController", false, LoginController(request))
  41. ResponseData *ValidAuthController(RequestData request) {
  42. string arg = request.getURLArgument("v");
  43. JsonModel model = json::parse(arg);
  44. string username = model["username"];
  45. string password = model["password"];
  46. string token = user::tryLogin(username, password);
  47. auto resp = new RedirectResponse("/");
  48. resp->addCookie("Token", token);
  49. return resp;
  50. }
  51. ContentGeneratorDefineS(request.getURLPath() == "/login", ValidAuthController(request))
  52. ResponseData *QuitLoginStateController(RequestData request) {
  53. auto resp = new RedirectResponse("/");
  54. resp->addCookie("Token", "");
  55. return resp;
  56. }
  57. ContentGeneratorDefineS(request.getURLPath() == "/quitLogin", QuitLoginStateController(request))
  58. }