summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/login/signup.js
blob: f37696fe6a3cd8a854b28c88050c023796fe7ba4 (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
var SignupView = FormView.extend({
  
  el: "#signup",
  action: "/api/signup",
  method: "put",
  
  initialize: function(opt){
    this.__super__.initialize.call(this)
    $("body").removeClass("loading")
    this.$("[name=username]").focus()
    $("body").addClass("signup")
  },
  
  validate: function(){
    var errors = []
    if (! this.$("[name=username]").val().length) {
      errors.push("Please enter a username")
    }
    if (! this.$("[name=password]").val().length) {
      errors.push("Please enter a password")
    }
    if (this.$("[name=password]").val() !== this.$("[name=password2]").val()) {
      errors.push("Passwords don't match")
    }
    return errors.length ? errors : null
  },
  
  showErrors: function(errors){
    $(".errors").show().css({ opacity: 1 }).html(errors.join("<br>"))
  },
  
  success: function(data){
    console.log("LOGGED IN?", data)
    if (data.user) {
      auth.set_user(data.user)
    }
    else {
      this.showErrors()
      return
    }
    if (data.returnTo) {
      console.log(data.returnTo)
      window.location.href = data.returnTo
    }
    else {
      window.location.href = "/index"
    }
  },

})