forked from acemtp/meteor-accounts-passwordless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts-passwordless-ui.js
118 lines (104 loc) · 3.51 KB
/
accounts-passwordless-ui.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
// set to false to skip the form asking the username
var loginPasswordlessAskUsername = true;
Session.setDefault('loginPasswordlessMessage', '');
Session.setDefault('loginPasswordlessState', 'loginPasswordlessLogin');
Tracker.autorun(function () {
var user = Meteor.user();
if(user) {
if(user.username || !loginPasswordlessAskUsername)
Session.set('loginPasswordlessState', 'loginPasswordlessLogout');
else
Session.set('loginPasswordlessState', 'loginPasswordlessAskUsername');
}
});
///
Template.registerHelper('loginPasswordlessMessage', function () {
return Session.get('loginPasswordlessMessage');
});
Template.loginPasswordless.helpers({
loginPasswordlessState: function () {
return Session.get('loginPasswordlessState');
}
});
Template.loginPasswordlessLogin.events({
'submit #loginPasswordlessLogin': function (event) {
var selector = event.target.selector.value;
console.log('login', selector);
Meteor.sendVerificationCode(selector, function (err, res) {
console.log('sendVerificationCode answered', arguments);
if(err)
Session.set('loginPasswordlessMessage', err.error);
else {
Session.set('loginPasswordlessMessage', '');
Session.set('loginPasswordlessState', 'loginPasswordlessVerify');
}
});
return false;
}
});
///
Template.loginPasswordlessVerify.events({
'submit #loginPasswordlessVerify': function (event) {
var code = event.target.code.value;
console.log('verify', code);
Meteor.loginWithPasswordless({ code: code }, function (err, res) {
console.log('loginWithPasswordless answered', arguments);
if(err)
Session.set('loginPasswordlessMessage', err.error);
else {
Session.set('loginPasswordlessMessage', '');
if(loginPasswordlessAskUsername && !Meteor.user().username)
Session.set('loginPasswordlessState', 'loginPasswordlessAskUsername');
else
Session.set('loginPasswordlessState', 'loginPasswordlessLogout');
}
});
return false;
},
'click #loginPasswordlessVerifyBack': function (event) {
Session.set('loginPasswordlessMessage', '');
Session.set('loginPasswordlessState', 'loginPasswordlessLogin');
},
});
///
Template.loginPasswordlessAskUsername.events({
'submit #loginPasswordlessAskUsername': function (event) {
var username = event.target.username.value;
console.log('username', username);
Meteor.setUsername(username, function (err, res) {
console.log('setUsername answered', arguments);
if(err)
Session.set('loginPasswordlessMessage', err.error);
else {
Session.set('loginPasswordlessMessage', '');
Session.set('loginPasswordlessState', 'loginPasswordlessLogout');
}
});
return false;
}
});
///
Template.loginPasswordlessLogout.helpers({
loginPasswordlessUserInfo: function () {
var user = Meteor.user();
if(!user) return '';
if(user.username) return user.username;
else if(user.emails && user.emails.length > 0) return user.emails[0].address;
else return user._id;
}
});
Template.loginPasswordlessLogout.events({
'submit #loginPasswordlessLogout': function (event) {
console.log('logout');
Meteor.logout(function (err, res) {
console.log('logout answered', arguments);
if(err)
Session.set('loginPasswordlessMessage', err.error);
else {
Session.set('loginPasswordlessMessage', '');
Session.set('loginPasswordlessState', 'loginPasswordlessLogin');
}
});
return false;
}
});