spacedeck_users.js 8.29 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
  SpacedeckUsers
  This module contains functions dealing with Users and Authentication.
*/

SpacedeckUsers = {
  data: {
    user_forms_email: "",
    user_forms_name: "",
    invitation_token: null,
    login_email: "",
    login_password: "",
    signup_password: "",
14
    signup_invite_code: "",
mntmn's avatar
mntmn committed
15
16
17
18
    signup_password_confirmation: "",
    account_remove_error: null,
    loading_user: false,
    password_reset_confirm_error: "",
19
    password_reset_error: "",
mntmn's avatar
mntmn committed
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
  },
  methods:{
    load_user: function(on_success, on_error) {
      this.loading_user = true;

      load_current_user(function(user) {
        this.user = user;
        this.loading_user = false;
        this.logged_in = true;

        if (on_success) {
          on_success(user);
        }
      }.bind(this), function() {
        // error
        this.loading_user = false;
        this.logout();

        if (on_error) {
          on_error();
        }
      }.bind(this));
    },

    finalize_login: function(session_token, on_success) {
      this.load_user(function(user) {
        if (this.invitation_token) {
          accept_invitation(this.invitation_token, function(memberships){
Wolfgang Knopki's avatar
Wolfgang Knopki committed
48
            this.redirect_to(ENV.prefix+"/spaces/"+memberships.space_id);
mntmn's avatar
mntmn committed
49
50
51
          }.bind(this), function(xhr){
            console.error(xhr);
            alert("Could not accept invitation. Maybe it was already accepted?");
Wolfgang Knopki's avatar
Wolfgang Knopki committed
52
            this.redirect_to(ENV.prefix+"/spaces");
mntmn's avatar
mntmn committed
53
54
55
56
57
58
          }.bind(this));
        } else {
          if (on_success) {
            on_success(this.user);
          } else {
            if (get_query_param("space_id") && get_query_param("space_id").length==24) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
59
              this.redirect_to(ENV.prefix+"/spaces/"+get_query_param("space_id"));
mntmn's avatar
mntmn committed
60
            } else {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
61
              this.redirect_to(ENV.prefix+"/spaces", function() {});
mntmn's avatar
mntmn committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            }
          }
        }
      }.bind(this));
    },

    login_with_token: function(token) {
      create_session_for_oauthtoken(token, function(session) {
        this.session = session;
        this.finalize_login(session.token);
      }.bind(this), function(xhr){
        // FIXME: handle error
      }.bind(this));
    },

    login_submit: function(email, password, $event, on_success) {
      this.loading_user = true;
      this.login_error = null;

      if ($event) {
        $event.preventDefault();
        $event.stopPropagation();
      }

      create_session(email, password, function(session) {
        console.log("session: ", session);
        this.loading_user = false;
        this.session = session;
        this.finalize_login(session.token, on_success);

      }.bind(this), function(req) {
        this.loading_user = false;
        var msg = "";

        if (req.status>=403) {
          var msg = "error_unknown_email";
        } else {
          try {
            var msg = "error_"+(JSON.parse(req.responseText).error);
          } catch (e) {
            var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
          }
        }
        this.login_error = __(msg);

      }.bind(this));
    },

    login_submit_modal: function(email, password) {
      this.login_submit(email, password, null, function() {
        location.reload();
      });
    },

    signup_guest: function(on_success) {
    },

119
    signup_submit: function($event, name, email, password, password_confirmation, invite_code, on_success) {
mntmn's avatar
mntmn committed
120
121
122
123
124
125
126
127
128
129
130
131
132
      this.creating_user = true;
      this.signup_error = null;

      if (("localStorage" in window) && localStorage) {
        localStorage["sd_api_token"] = null;
      }
      api_token = null;

      if ($event) {
        $event.preventDefault();
        $event.stopPropagation();
      }

133
      create_user(name, email, password, password_confirmation, invite_code, function(session) {
mntmn's avatar
mntmn committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        this.creating_user = false;
        this.login_submit(email, password, null, on_success);
      }.bind(this), function(req) {
        this.creating_user = false;
        try {
          var msg = "error_"+(JSON.parse(req.responseText).error);
        } catch (e) {
          var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
        }

        var msg = __(msg);
        this.signup_error = msg;
      }.bind(this));
    },

149
150
    signup_submit_modal: function($event, name, email, password, password_confirmation, invite_code) {
      this.signup_submit($event, name, email, password, password_confirmation, invite_code, function() {
mntmn's avatar
mntmn committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        alert("Success.");
        location.reload();
      });
    },

    password_reset_submit: function(evt, email) {
      if (evt) {
        evt.preventDefault();
        evt.stopPropagation();
      }

      this.password_reset_error = null;
      this.password_reset_send = false;

      if (email === undefined || email.length < 3) {
        this.password_reset_error = "This is not a valid email address";
        return;
      }

      create_password_reset(email, function(parsed,req) {
        if(req.status==201) {
          this.password_reset_send = true;
        }
      }.bind(this), function(req) {
        console.log(req.status);
        if (req.status==404) {
          var msg = "error_unknown_email";
        } else {
          try {
            var msg = "error_"+(JSON.parse(req.responseText).error);
          } catch (e) {
            var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
          }
        }
        this.password_reset_error = __(msg);
      }.bind(this));
    },

    password_reset_confirm: function(evt, password, password_confirmation) {
      if (evt) {
        evt.preventDefault();
        evt.stopPropagation();
      }

      this.password_reset_confirm_error = null;
      this.password_reset_send = false;

mntmn's avatar
mntmn committed
198
      if (password != password_confirmation) {
mntmn's avatar
mntmn committed
199
200
201
202
        this.password_reset_confirm_error = "Passwords do not match.";
        return;
      }

mntmn's avatar
mntmn committed
203
      if (password.length < 5) {
mntmn's avatar
mntmn committed
204
205
206
207
208
        this.password_reset_confirm_error = "Password too short (must have at least 5 characters).";
        return;
      }

      confirm_password_reset(password, this.reset_token, function(parsed,req) {
mntmn's avatar
mntmn committed
209
210
        if (req.status==201) {
          alert("New password set successfully.");
mntmn's avatar
mntmn committed
211
          this.active_view = "login";
mntmn's avatar
mntmn committed
212
213
        } else {
          alert("An unknown error occured.");
mntmn's avatar
mntmn committed
214
215
216
        }
      }.bind(this), function(req) {
        if (req.status==404) {
mntmn's avatar
mntmn committed
217
          alert("Error: Unknown user.");
mntmn's avatar
mntmn committed
218
        } else {
mntmn's avatar
mntmn committed
219
          alert("Error: "+req.statusText);
mntmn's avatar
mntmn committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        }
      }.bind(this));
    },

    logout: function() {
      this.active_view="login";
      this.logged_in = false;
      delete_session(function() {

        this.active_space = {advanced:{}};
        this.active_space_loaded = false;
        this.active_sidebar_item = "none";
        this.sidebar_state = "closed";
        this.loading_user = false;
        api_token = null;
        this.user = {};
        this.active_content_type = "login";
Wolfgang Knopki's avatar
Wolfgang Knopki committed
237
        this.redirect_to(ENV.prefix+"/");
mntmn's avatar
mntmn committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

      }.bind(this));
    },

    send_feedback: function(text) {
      if (text.length>0) {
        create_feedback(this.user, text, function(xhr) {
          alert(__("feedback_sent"));
          this.close_modal()
        }.bind(this), function(xhr) {
          console.error(xhr);
        });
      }
    },

    remove_account: function(password, reason) {
      this.account_remove_error = null;

      if (reason && reason.length && (reason.length > 1)) {
        create_feedback(this.user, reason, function(xhr) {
          console.log("feedback sent");
        }, function(xhr){});
      }

      if (!password) {
        this.account_remove_error = "Password not correct";
        return;
      }

      delete_user(this.user, password, function(xhr) {
        alert("Sorry to see you go. Goodbye!");
        this.logout();
      }.bind(this), function(xhr) {
        this.account_remove_error = "Password not correct ("+xhr.status+")";
      }.bind(this));
    },

    user_avatar_image: function(user) {
      return user.avatar_thumb_uri;
    },

    user_initials: function(user) {
      var parts = (user?(user.nickname||user.email):"anonymous").replace(/[^a-zA-Z]/g,' ').replace(/ +/g,' ').split(' ');
      if (parts.length>1) {
        return parts[0][0]+parts[1][0];
      }
      return parts[0].substring(0,2);
    },

    has_avatar_image: function(user) {
      return !!(user && user.avatar_thumb_uri && user.avatar_thumb_uri.length>0);
    },

    is_pro: function(user) {
      return true;
    },
  }
}