login.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //const validEmail = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
  2. // Validation Regexes.
  3. const validUsername = /^[a-zA-Z0-9_]{1,16}$/
  4. const basicEmail = /^\S+@\S+\.\S+$/
  5. // Login Elements
  6. const loginEmailError = document.getElementById('loginEmailError')
  7. const loginUsername = document.getElementById('loginUsername')
  8. const loginPasswordError = document.getElementById('loginPasswordError')
  9. const loginPassword = document.getElementById('loginPassword')
  10. const checkmarkContainer = document.getElementById('checkmarkContainer')
  11. const loginRememberOption = document.getElementById('loginRememberOption')
  12. const loginButton = document.getElementById('loginButton')
  13. // Control variables.
  14. let lu = false, lp = false
  15. /**
  16. * Show a login error.
  17. *
  18. * @param {HTMLElement} element The element on which to display the error.
  19. * @param {string} value The error text.
  20. */
  21. function showError(element, value){
  22. element.innerHTML = value
  23. element.style.opacity = 1
  24. }
  25. /**
  26. * Shake a login error to add emphasis.
  27. *
  28. * @param {HTMLElement} element The element to shake.
  29. */
  30. function shakeError(element){
  31. if(element.style.opacity == 1){
  32. element.classList.remove('shake')
  33. void element.offsetWidth
  34. element.classList.add('shake')
  35. }
  36. }
  37. /**
  38. * Validate that an email field is neither empty nor invalid.
  39. *
  40. * @param {string} value The email value.
  41. */
  42. function validateEmail(value){
  43. if(value){
  44. if(!basicEmail.test(value) && !validUsername.test(value)){
  45. showError(loginEmailError, '* Invalid Value')
  46. loginDisabled(true)
  47. lu = false
  48. } else {
  49. loginEmailError.style.opacity = 0
  50. lu = true
  51. if(lp){
  52. loginDisabled(false)
  53. }
  54. }
  55. } else {
  56. lu = false
  57. showError(loginEmailError, '* Required')
  58. loginDisabled(true)
  59. }
  60. }
  61. /**
  62. * Validate that the password field is not empty.
  63. *
  64. * @param {string} value The password value.
  65. */
  66. function validatePassword(value){
  67. if(value){
  68. loginPasswordError.style.opacity = 0
  69. lp = true
  70. if(lu){
  71. loginDisabled(false)
  72. }
  73. } else {
  74. lp = false
  75. showError(loginPasswordError, '* Required')
  76. loginDisabled(true)
  77. }
  78. }
  79. // Emphasize errors with shake when focus is lost.
  80. loginUsername.addEventListener('focusout', (e) => {
  81. validateEmail(e.target.value)
  82. shakeError(loginEmailError)
  83. })
  84. loginPassword.addEventListener('focusout', (e) => {
  85. validatePassword(e.target.value)
  86. shakeError(loginPasswordError)
  87. })
  88. // Validate input for each field.
  89. loginUsername.addEventListener('input', (e) => {
  90. validateEmail(e.target.value)
  91. })
  92. loginPassword.addEventListener('input', (e) => {
  93. validatePassword(e.target.value)
  94. })
  95. /**
  96. * Enable or disable the login button.
  97. *
  98. * @param {boolean} v True to enable, false to disable.
  99. */
  100. function loginDisabled(v){
  101. if(loginButton.disabled !== v){
  102. loginButton.disabled = v
  103. }
  104. }
  105. /**
  106. * Enable or disable loading elements.
  107. *
  108. * @param {boolean} v True to enable, false to disable.
  109. */
  110. function loginLoading(v){
  111. if(v){
  112. loginButton.setAttribute('loading', v)
  113. loginButton.innerHTML = loginButton.innerHTML.replace('LOGIN', 'LOGGING IN')
  114. } else {
  115. loginButton.removeAttribute('loading')
  116. loginButton.innerHTML = loginButton.innerHTML.replace('LOGGING IN', 'LOGIN')
  117. }
  118. }
  119. /**
  120. * Enable or disable login form.
  121. *
  122. * @param {boolean} v True to enable, false to disable.
  123. */
  124. function formDisabled(v){
  125. loginDisabled(v)
  126. loginUsername.disabled = v
  127. loginPassword.disabled = v
  128. if(v){
  129. checkmarkContainer.setAttribute('disabled', v)
  130. } else {
  131. checkmarkContainer.removeAttribute('disabled')
  132. }
  133. loginRememberOption.disabled = v
  134. }
  135. /**
  136. * Parses an error and returns a user-friendly title and description
  137. * for our error overlay.
  138. *
  139. * @param {Error | {cause: string, error: string, errorMessage: string}} err A Node.js
  140. * error or Mojang error response.
  141. */
  142. function resolveError(err){
  143. // Mojang Response => err.cause | err.error | err.errorMessage
  144. // Node error => err.code | err.message
  145. if(err.cause != null && err.cause === 'UserMigratedException') {
  146. return {
  147. title: 'Error During Login:<br>Invalid Credentials',
  148. desc: 'You\'ve attempted to login with a migrated account. Try again using the account email as the username.'
  149. }
  150. } else {
  151. if(err.error != null){
  152. if(err.error === 'ForbiddenOperationException'){
  153. if(err.errorMessage != null){
  154. if(err.errorMessage === 'Invalid credentials. Invalid username or password.'){
  155. return {
  156. title: 'Error During Login:<br>Invalid Credentials',
  157. desc: 'The email or password you\'ve entered is incorrect. Please try again.'
  158. }
  159. } else if(err.errorMessage === 'Invalid credentials.'){
  160. return {
  161. title: 'Error During Login:<br>Too Many Attempts',
  162. desc: 'There have been too many login attempts with this account recently. Please try again later.'
  163. }
  164. }
  165. }
  166. }
  167. } else {
  168. // Request errors (from Node).
  169. if(err.code != null){
  170. if(err.code === 'ENOENT'){
  171. // No Internet.
  172. return {
  173. title: 'Error During Login:<br>No Internet Connection',
  174. desc: 'You must be connected to the internet in order to login. Please connect and try again.'
  175. }
  176. } else if(err.code === 'ENOTFOUND'){
  177. // Could not reach server.
  178. return {
  179. title: 'Error During Login:<br>Authentication Server Offline',
  180. desc: 'Mojang\'s authentication server is currently offline or unreachable. Please wait a bit and try again. You can check the status of the server on <a href="https://help.mojang.com/">Mojang\'s help portal</a>.'
  181. }
  182. }
  183. }
  184. }
  185. }
  186. if(err.message != null){
  187. // Unknown error with request.
  188. return {
  189. title: 'Error During Login:<br>Unknown Error',
  190. desc: err.message
  191. }
  192. } else {
  193. // Unknown Mojang error.
  194. return {
  195. title: err.error,
  196. desc: err.errorMessage
  197. }
  198. }
  199. }
  200. // Bind login button behavior.
  201. loginButton.addEventListener('click', () => {
  202. // Disable form.
  203. formDisabled(true)
  204. // Show loading stuff.
  205. loginLoading(true)
  206. AuthManager.addAccount(loginUsername.value, loginPassword.value).then((value) => {
  207. loginButton.innerHTML = loginButton.innerHTML.replace('LOGGING IN', 'SUCCESS')
  208. $('.circle-loader').toggleClass('load-complete')
  209. $('.checkmark').toggle()
  210. //console.log(value)
  211. setTimeout(() => {
  212. $('#loginContainer').fadeOut(500, () => {
  213. $('#landingContainer').fadeIn(500)
  214. })
  215. }, 1000)
  216. }).catch((err) => {
  217. loginLoading(false)
  218. const errF = resolveError(err)
  219. setOverlayContent(errF.title, errF.desc, 'Try Again')
  220. setOverlayHandler(() => {
  221. formDisabled(false)
  222. toggleOverlay(false)
  223. })
  224. toggleOverlay(true)
  225. console.log(err)
  226. })
  227. })