login.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // DOM cache.
  6. const loginContainer = document.getElementById('loginContainer')
  7. const loginErrorTitle = document.getElementById('loginErrorTitle')
  8. const loginErrorDesc = document.getElementById('loginErrorDesc')
  9. const loginErrorAcknowledge = document.getElementById('loginErrorAcknowledge')
  10. const loginEmailError = document.getElementById('loginEmailError')
  11. const loginUsername = document.getElementById('loginUsername')
  12. const loginPasswordError = document.getElementById('loginPasswordError')
  13. const loginPassword = document.getElementById('loginPassword')
  14. const checkmarkContainer = document.getElementById('checkmarkContainer')
  15. const loginRememberOption = document.getElementById('loginRememberOption')
  16. const loginButton = document.getElementById('loginButton')
  17. // Control variables.
  18. let lu = false, lp = false
  19. // Show error element.
  20. function showError(element, value){
  21. element.innerHTML = value
  22. element.style.opacity = 1
  23. }
  24. // Shake error element.
  25. function shakeError(element){
  26. if(element.style.opacity == 1){
  27. element.classList.remove('shake')
  28. void element.offsetWidth
  29. element.classList.add('shake')
  30. }
  31. }
  32. // Validate email field is neither empty nor invalid.
  33. function validateEmail(value){
  34. if(value){
  35. if(!basicEmail.test(value) && !validUsername.test(value)){
  36. showError(loginEmailError, '* Invalid Value')
  37. loginDisabled(true)
  38. lu = false
  39. } else {
  40. loginEmailError.style.opacity = 0
  41. lu = true
  42. if(lp){
  43. loginDisabled(false)
  44. }
  45. }
  46. } else {
  47. lu = false
  48. showError(loginEmailError, '* Required')
  49. loginDisabled(true)
  50. }
  51. }
  52. // Validate password field is not empty.
  53. function validatePassword(value){
  54. if(value){
  55. loginPasswordError.style.opacity = 0
  56. lp = true
  57. if(lu){
  58. loginDisabled(false)
  59. }
  60. } else {
  61. lp = false
  62. showError(loginPasswordError, '* Required')
  63. loginDisabled(true)
  64. }
  65. }
  66. // Emphasize errors with shake when focus is lost.
  67. loginUsername.addEventListener('focusout', (e) => {
  68. validateEmail(e.target.value)
  69. shakeError(loginEmailError)
  70. })
  71. loginPassword.addEventListener('focusout', (e) => {
  72. validatePassword(e.target.value)
  73. shakeError(loginPasswordError)
  74. })
  75. // Validate input for each field.
  76. loginUsername.addEventListener('input', (e) => {
  77. validateEmail(e.target.value)
  78. })
  79. loginPassword.addEventListener('input', (e) => {
  80. validatePassword(e.target.value)
  81. })
  82. // Enable or disable login button.
  83. function loginDisabled(v){
  84. if(loginButton.disabled !== v){
  85. loginButton.disabled = v
  86. }
  87. }
  88. // Enable or disable loading elements.
  89. function loginLoading(v){
  90. if(v){
  91. loginButton.setAttribute('loading', v)
  92. loginButton.innerHTML = loginButton.innerHTML.replace('LOGIN', 'LOGGING IN')
  93. } else {
  94. loginButton.removeAttribute('loading')
  95. loginButton.innerHTML = loginButton.innerHTML.replace('LOGGING IN', 'LOGIN')
  96. }
  97. }
  98. // Disable or enable login form.
  99. function formDisabled(v){
  100. loginDisabled(v)
  101. loginUsername.disabled = v
  102. loginPassword.disabled = v
  103. if(v){
  104. checkmarkContainer.setAttribute('disabled', v)
  105. } else {
  106. checkmarkContainer.removeAttribute('disabled')
  107. }
  108. loginRememberOption.disabled = v
  109. }
  110. function resolveError(err){
  111. // Mojang Response => err.cause | err.error | err.errorMessage
  112. // Node error => err.code | err.message
  113. if(err.cause != null && err.cause === 'UserMigratedException') {
  114. return {
  115. title: 'Error During Login:<br>Invalid Credentials',
  116. desc: 'You\'ve attempted to login with a migrated account. Try again using the account email as the username.'
  117. }
  118. } else {
  119. if(err.error != null){
  120. if(err.error === 'ForbiddenOperationException'){
  121. if(err.errorMessage != null){
  122. if(err.errorMessage === 'Invalid credentials. Invalid username or password.'){
  123. return {
  124. title: 'Error During Login:<br>Invalid Credentials',
  125. desc: 'The email or password you\'ve entered is incorrect. Please try again.'
  126. }
  127. } else if(err.errorMessage === 'Invalid credentials.'){
  128. return {
  129. title: 'Error During Login:<br>Too Many Attempts',
  130. desc: 'There have been too many login attempts with this account recently. Please try again later.'
  131. }
  132. }
  133. }
  134. }
  135. } else {
  136. // Request errors (from Node).
  137. if(err.code != null){
  138. if(err.code === 'ENOENT'){
  139. // No Internet.
  140. return {
  141. title: 'Error During Login:<br>No Internet Connection',
  142. desc: 'You must be connected to the internet in order to login. Please connect and try again.'
  143. }
  144. } else if(err.code === 'ENOTFOUND'){
  145. // Could not reach server.
  146. return {
  147. title: 'Error During Login:<br>Authentication Server Offline',
  148. 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>.'
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if(err.message != null){
  155. // Unknown error with request.
  156. return {
  157. title: 'Error During Login:<br>Unknown Error',
  158. desc: err.message
  159. }
  160. } else {
  161. // Unknown Mojang error.
  162. return {
  163. title: err.error,
  164. desc: err.errorMessage
  165. }
  166. }
  167. }
  168. loginButton.addEventListener('click', () => {
  169. // Disable form.
  170. formDisabled(true)
  171. // Show loading stuff.
  172. loginLoading(true)
  173. AuthManager.addAccount(loginUsername.value, loginPassword.value).then((value) => {
  174. loginButton.innerHTML = loginButton.innerHTML.replace('LOGGING IN', 'SUCCESS')
  175. $('.circle-loader').toggleClass('load-complete')
  176. $('.checkmark').toggle()
  177. //console.log(value)
  178. setTimeout(() => {
  179. $('#loginContainer').fadeOut(500, () => {
  180. $('#landingContainer').fadeIn(500)
  181. })
  182. }, 1000)
  183. }).catch((err) => {
  184. loginLoading(false)
  185. const errF = resolveError(err)
  186. setOverlayContent(errF.title, errF.desc, 'Try Again')
  187. setOverlayHandler(() => {
  188. formDisabled(false)
  189. toggleOverlay(false)
  190. })
  191. toggleOverlay(true)
  192. console.log(err)
  193. })
  194. })