summaryrefslogtreecommitdiff
path: root/users/app/assets/javascripts/users.js.coffee
blob: d0ec32fe7fa66e673131c2e4b1de243b34a7de4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
preventDefault = (event) ->
  event.preventDefault()

validOrAbort = (event) ->
  errors = {}
  
  abortIfErrors = ->
    return if $.isEmptyObject(errors)
    # we're relying on client_side_validations here instead of printing
    # our own errors. This gets us translatable error messages.
    $('.control-group.error input, .control-group.error select, control-group.error textarea').first().focus()
    event.stopImmediatePropagation()
    
  validatePassword = ->
    password = $('#srp_password').val()
    confirmation = $('#srp_password_confirmation').val()
    login = $('#srp_username').val()
  
    if password != confirmation
      errors.password_confirmation = "Confirmation does not match!"
    if password == login
      errors.password = "Password and Login may not match!"
    if password.length < 8
      errors.password = "Password needs to be at least 8 characters long!"

  validatePassword()
  abortIfErrors()
  
  

srp.session = new srp.Session()
srp.signedUp = ->
  window.location = '/'

srp.loggedIn = ->
  window.location = '/'

srp.error = (message) ->
  if $.isPlainObject(message) && message.errors
    for field, error of message.errors
      element = $('form input[name="session['+field+']"]')
      next unless element
      element.trigger('element:validate:fail.ClientSideValidations', error).data('valid', false)
  else
    alert(message)

$(document).ready ->
  $('#new_user').submit preventDefault
  $('#new_user').submit validOrAbort
  $('#new_user').submit srp.signup
  $('#new_session').submit preventDefault
  $('#new_session').submit srp.login