login.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="robots" content="noindex" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="icon" type="image/png" href="./favicon.png" />
  8. <title>Login | Zoraxy</title>
  9. <link rel="stylesheet" href="script/semantic/semantic.min.css">
  10. <script type="application/javascript" src="script/jquery-3.6.0.min.js"></script>
  11. <script type="application/javascript" src="script/semantic/semantic.min.js"></script>
  12. <style>
  13. body {
  14. background: rgb(245,245,245);
  15. background: linear-gradient(28deg, rgba(245,245,245,1) 63%, rgba(255,255,255,1) 100%);
  16. }
  17. .background{
  18. position: fixed;
  19. top: 0;
  20. right: 0;
  21. height: 100%;
  22. width: 100%;
  23. opacity: 0.8;
  24. z-index: -99;
  25. background-image: url("img/public/bg.jpg");
  26. background-size: auto 100%;
  27. background-position: right top;
  28. background-repeat: no-repeat;
  29. overflow-x: hidden;
  30. }
  31. form {
  32. margin:auto;
  33. }
  34. #loginForm{
  35. height: 100%;
  36. background-color: white;
  37. width: 25em;
  38. margin-left: 10em;
  39. margin-top: 0 !important;
  40. margin-bottom: 0 !important;
  41. }
  42. @media all and (max-width: 550px) {
  43. /* CSS rules here for screens lower than 750px */
  44. #loginForm{
  45. width: calc(100% - 4em);
  46. margin-left: 2em;
  47. }
  48. }
  49. #errmsg{
  50. color: #9f3a38;
  51. margin-top: 1em;
  52. margin-bottom: 0.4em;
  53. text-align: left;
  54. }
  55. .registerOnly{
  56. display:none;
  57. }
  58. .ui.fluid.button.registerOnly{
  59. display:none;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div class="background"></div>
  65. <div id="loginForm" class="ui middle aligned center aligned grid">
  66. <div class="column">
  67. <form class="ui large form">
  68. <div class="ui basic segment">
  69. <img class="ui fluid image" src="img/public/logo.svg" style="pointer-events:none;">
  70. <p class="registerOnly">Account Setup</p>
  71. <div class="field">
  72. <div class="ui left icon input">
  73. <i class="user icon"></i>
  74. <input id="username" type="text" name="username" placeholder="Username">
  75. </div>
  76. </div>
  77. <div class="field">
  78. <div class="ui left icon input">
  79. <i class="lock icon"></i>
  80. <input id="magic" type="password" name="password" placeholder="Password">
  81. </div>
  82. </div>
  83. <div class="field registerOnly">
  84. <div class="ui left icon input">
  85. <i class="lock icon"></i>
  86. <input id="repeatMagic" type="password" name="passwordconfirm" placeholder="Confirm Password">
  87. </div>
  88. </div>
  89. <div class="field loginOnly" style="text-align: left;">
  90. <div class="ui checkbox">
  91. <input id="rmbme" type="checkbox" tabindex="0" class="hidden">
  92. <label>Remember Me</label>
  93. </div>
  94. </div>
  95. <div id="loginbtn" class="ui fluid basic blue button loginOnly">Login</div>
  96. <div id="regsiterbtn" class="ui fluid basic blue button registerOnly">Create</div>
  97. <div id="errmsg"></div>
  98. <div id="forgetPassword" class="field loginOnly" style="text-align: right;">
  99. <a href="#" onclick="sendResetAccountEmail();">Forget Password</a>
  100. </div>
  101. </div>
  102. <div class="ui divider"></div>
  103. <small>Proudly powered by Zoraxy</small>
  104. </form>
  105. </div>
  106. </div>
  107. <script>
  108. var redirectionAddress = "/";
  109. var loginAddress = "/api/auth/login";
  110. $(".checkbox").checkbox();
  111. $(document).ready(function(){
  112. var currentdate = new Date();
  113. var datetime = currentdate.getDate() + "/"
  114. + (currentdate.getMonth()+1) + "/"
  115. + currentdate.getFullYear() + " "
  116. + currentdate.getHours() + ":"
  117. + currentdate.getMinutes() + ":"
  118. + currentdate.getSeconds();
  119. $("#requestTime").text(datetime);
  120. //Check if this is a new system
  121. $.get("/api/auth/userCount", function(data){
  122. if (data == 0){
  123. //Allow user creation
  124. $(".loginOnly").hide();
  125. $(".registerOnly").show();
  126. }
  127. });
  128. //Check if the user already logged in
  129. $.get("/api/auth/checkLogin",function(data){
  130. try{
  131. if (data === true || data.trim() == "true"){
  132. //User already logged in. Redirect to target page.
  133. if (redirectionAddress == ""){
  134. //Redirect back to index
  135. window.location.href = "/";
  136. }else{
  137. console.log(data);
  138. //window.location.href = redirectionAddress;
  139. }
  140. }
  141. }catch(ex){
  142. //Assume not logged in
  143. console.log(data);
  144. }
  145. });
  146. });
  147. function updateYear() {
  148. const year = new Date().getFullYear();
  149. const elements = document.getElementsByClassName("year");
  150. for (let i = 0; i < elements.length; i++) {
  151. elements[i].textContent = year;
  152. }
  153. }
  154. updateYear();
  155. //Event handlers for buttons
  156. $("#loginbtn").on("click",function(){
  157. login();
  158. });
  159. $("input").on("keydown",function(event){
  160. if (event.keyCode === 13) {
  161. event.preventDefault();
  162. if ($(this).attr("id") == "magic"){
  163. login();
  164. }else{
  165. //Fuocus to password field
  166. $("#magic").focus();
  167. }
  168. }
  169. });
  170. $("#regsiterbtn").on("click", function(event){
  171. var username = $("#username").val();
  172. var magic = $("#magic").val();
  173. var repeatMagic = $("#repeatMagic").val();
  174. if (magic !== repeatMagic) {
  175. alert("Password does not match");
  176. return;
  177. }
  178. $.ajax({
  179. url: "/api/auth/register",
  180. method: "POST",
  181. data: {
  182. username: username,
  183. password: magic
  184. },
  185. success: function(data) {
  186. if (data.error != undefined){
  187. alert(data.error);
  188. }else{
  189. //Register success. Refresh page
  190. window.location.reload();
  191. }
  192. },
  193. error: function(xhr, status, error) {
  194. console.error("Error registering user:", error);
  195. }
  196. });
  197. });
  198. //Send account reset email to preset admin account
  199. function sendResetAccountEmail(){
  200. $("#forgetPassword").html(`<i class="ui loading spinner icon"></i> Sending Email`);
  201. $("#forgetPassword").css("opacity", "0.8");
  202. $.get("/api/account/reset", function(data){
  203. if (data.error !== undefined){
  204. $("#forgetPassword").html(`<a href="#" onclick="sendResetAccountEmail();">Forget Password</a>`);
  205. alert(data.error);
  206. }else{
  207. window.location.href = "/web/reset.html"
  208. }
  209. });
  210. }
  211. //Login system with the given username and password
  212. function login(){
  213. var username = $("#username").val();
  214. var magic = $("#magic").val();
  215. var rmbme = document.getElementById("rmbme").checked;
  216. $("#errmsg").stop().finish().slideUp("fast");
  217. $("input").addClass('disabled');
  218. $.post(loginAddress, {"username": username, "password": magic, "rmbme": rmbme}).done(function(data){
  219. if (data.error !== undefined){
  220. //Something went wrong during the login
  221. $("#errmsg").html(`<i class="red remove icon"></i> ${data.error}`);
  222. $("#errmsg").stop().finish().slideDown('fast');
  223. }else if(data.redirect !== undefined){
  224. //LDAP Related Code
  225. window.location.href = data.redirect;
  226. }else{
  227. //Login succeed
  228. if (redirectionAddress == ""){
  229. //Redirect back to index
  230. window.location.href = "./";
  231. }else{
  232. window.location.href = redirectionAddress;
  233. }
  234. }
  235. $("input").removeClass('disabled');
  236. });
  237. }
  238. function get(name){
  239. if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
  240. return decodeURIComponent(name[1]);
  241. }
  242. $(".thisyear").text(new Date().getFullYear());
  243. function updateRenderElements(){
  244. if (window.innerHeight < 520){
  245. $(".bottombar").hide();
  246. }else{
  247. $(".bottombar").show();
  248. }
  249. }
  250. updateRenderElements();
  251. $(window).on("resize", function(){
  252. updateRenderElements();
  253. });
  254. </script>
  255. </body>
  256. </html>