/
hoodie.accountbar.bootstrap.js
145 lines (125 loc) · 4.19 KB
/
hoodie.accountbar.bootstrap.js
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
54
55
56
57
58
59
60
61
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
!function ($) {
'use strict';
// extend Hoodie with Hoodstrap module
Hoodie.extend(function(hoodie) {
// Constructor
function Hoodstrap(hoodie) {
this.hoodie = hoodie;
// all about authentication and stuff
this.hoodifyAccountBar();
}
Hoodstrap.prototype = {
//
hoodifyAccountBar: function() {
this.subscribeToHoodieEvents();
this.hoodie.account.authenticate().then(this.handleUserAuthenticated.bind(this), this.handleUserUnauthenticated.bind(this));
},
subscribeToHoodieEvents : function() {
this.hoodie.account.on('signin reauthenticated', this.handleUserAuthenticated.bind(this));
this.hoodie.account.on('signout', this.handleUserUnauthenticated.bind(this));
this.hoodie.on('account:error:unauthenticated remote:error:unauthenticated', this.handleUserAuthenticationError.bind(this));
},
//
handleUserAuthenticated: function(username) {
$('html').attr('data-hoodie-account-status', 'signedin');
$('.hoodie-accountbar').find('.hoodie-username').text(username);
},
//
handleUserUnauthenticated: function() {
$('html').attr('data-hoodie-account-status', 'signedout');
},
handleUserAuthenticationError: function() {
$('.hoodie-accountbar').find('.hoodie-username').text(this.hoodie.account.username);
$('html').attr('data-hoodie-account-status', 'error');
}
};
new Hoodstrap(hoodie);
});
/* Hoodie DATA-API
* =============== */
$(function () {
// bind to click events
$('body').on('click.hoodie.data-api', '[data-hoodie-action]', function(event) {
var $element = $(event.target),
action = $element.data('hoodie-action'),
$form;
switch(action) {
case 'signup':
$form = $.modalForm({
fields: [ 'username', 'password', 'password_confirmation' ],
submit: 'Sign Up'
});
break;
case 'signin':
$form = $.modalForm({
fields: [ 'username', 'password' ],
submit: 'Sign in'
});
break;
case 'resetpassword':
$form = $.modalForm({
fields: [ 'username' ],
submit: 'Reset Password'
});
break;
case 'changepassword':
$form = $.modalForm({
fields: [ 'current_password', 'new_password' ],
submit: 'Reset Password'
});
break;
case 'changeusername':
$form = $.modalForm({
fields: [ 'current_password', 'new_username' ],
submit: 'Reset Password'
});
break;
case 'signout':
window.hoodie.account.signOut();
break;
case 'destroy':
if( window.confirm('you sure?') ) {
window.hoodie.account.destroy();
}
break;
}
if ($form) {
$form.on('submit', handleSubmit( action ));
}
});
var handleSubmit = function(action) {
return function(event, inputs) {
var $modal = $(event.target);
var magic;
switch(action) {
case 'signin':
magic = window.hoodie.account.signIn(inputs.username, inputs.password);
break;
case 'signup':
magic = window.hoodie.account.signUp(inputs.username, inputs.password);
break;
case 'changepassword':
magic = window.hoodie.account.changePassword(null, inputs.new_password);
break;
case 'changeusername':
magic = window.hoodie.account.changeUsername(inputs.current_password, inputs.new_username);
break;
case 'resetpassword':
magic = window.hoodie.account.resetPassword(inputs.email)
.done(function() {
window.alert('send new password to ' + inputs.email);
});
break;
}
magic.done(function() {
$modal.find('.alert').remove();
$modal.modal('hide');
});
magic.fail(function(error) {
$modal.find('.alert').remove();
$modal.trigger('error', error);
});
};
};
});
}( window.jQuery )