When a username/password auth fails, restore the username/password dialog instead...
[citadel.git] / webcit / static / authmethods.js
1 /*
2  * Copyright 2010-2011, the Citadel Team
3  * Licensed under the GPL V3
4  *
5  * JavaScript functions which handle various authentication methods.
6  */
7
8
9
10 /****************** COMMON CODE ***********************/
11
12
13 /*
14  * Are we logged in right now?
15  */
16 function IsLoggedIn() {
17         if ($('is_logged_in').innerHTML == "yes") {
18                 return 1;
19         }
20         else {
21                 return 0;
22         }
23 }
24
25
26 /*
27  * Wrapper script to require logging in before completing an action
28  */
29 function GetLoggedInFirst(destination_url) {
30
31         /* If logged in already, go directly to the destination. */
32         if (IsLoggedIn()) {
33                 window.location = decodeURIComponent(destination_url);
34                 return;
35         }
36
37         p = 'push?url=' + destination_url;
38         new Ajax.Request(p, { method: 'get' } );
39
40         /* If not logged in, go modal and ask the user to log in first. */
41         new Ajax.Updater(
42                 'md-content',
43                 'do_template?template=get_logged_in',
44                 {
45                         method: 'get',
46                         onSuccess: function() {
47                                 toggleModal(1);
48                         }
49                 }
50         );
51 }
52
53
54 /*
55  * tab handler for the login box
56  */
57 function authtoggle(show_which_div) {
58         $('authbox_userpass').style.display = 'none';
59         $('authbox_newuser').style.display = 'none';
60         $('authbox_openid').style.display = 'none';
61         $('authbox_google').style.display = 'none';
62         $('authbox_yahoo').style.display = 'none';
63         $('authbox_aol').style.display = 'none';
64         $('authbox_success').style.display = 'none';
65         $(show_which_div).style.display = 'block';
66 }
67
68
69 /*
70  * Pop out a window for external auth methods
71  * (most of them don't handle inline auth very well)
72  */
73 function do_auth_popout(popout_url) {
74         window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
75 }
76
77
78
79
80 /****************** USERNAME AND PASSWORD ***********************/
81
82 /*
83  * Attempt login with username/password, called from modal dialog
84  */
85 function ajax_try_username_and_password() {
86
87         $('login_errmsg').innerHTML = "";
88         authtoggle('authbox_success');
89         $('ajax_username_password_form').request({
90                 onSuccess: function(ctdlresult) {
91                         if (ctdlresult.responseText.substr(0,1) == '2') {
92                                 window.location = 'pop';
93                         }
94                         else {
95                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
96                                 authtoggle('authbox_userpass');
97                         }
98                 }
99         });
100 }
101
102
103 /*
104  * The user pressed a key while in the username or password box.
105  * Is it the enter/return key?  Submit the form.
106  */
107 function username_and_password_onkeypress(e) {
108         if (window.event) {             /* IE */
109                 keynum = e.keyCode
110         }
111         else if (e.which) {             /* real browsers */
112                 keynum = e.which
113         }
114         if (keynum == 13) {             /* enter/return key */
115                 ajax_try_username_and_password();
116         }
117 }
118
119
120 /****************** REGISTER NEW USER ***********************/
121
122 /*
123  * Attempt to create a new local username/password, called from modal dialog
124  */
125 function ajax_try_newuser() {
126
127         $('login_errmsg').innerHTML = "";
128         $('ajax_newuser_form').request({
129                 onSuccess: function(ctdlresult) {
130                         if (ctdlresult.responseText.substr(0,1) == '2') {
131                                 authtoggle('authbox_success');
132                                 window.location = 'pop';
133                         }
134                         else {
135                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
136                         }
137                 }
138         });
139 }
140
141
142 /*
143  * The user pressed a key while in the newuser or newpassword box.
144  * Is it the enter/return key?  Submit the form.
145  */
146 function newuser_onkeypress(e) {
147         if (window.event) {             /* IE */
148                 keynum = e.keyCode;
149         }
150         else if (e.which) {             /* real browsers */
151                 keynum = e.which;
152         }
153         if (keynum == 13) {             /* enter/return key */
154                 ajax_try_newuser();
155         }
156 }
157
158
159
160
161 /****************** OPENID ***********************/
162
163 /*
164  * Attempt login with OpenID, called from modal dialog
165  */
166 function ajax_try_openid() {
167         $('login_errmsg').innerHTML = "";
168         openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
169         do_auth_popout("openid_login?openid_url=" + openid_url);
170 }
171
172
173 /*
174  * The user pressed a key while in the openid login box.
175  * Is it the enter/return key?  Submit the form.
176  */
177 function openid_onkeypress(e) {
178         if (window.event) {             /* IE */
179                 keynum = e.keyCode;
180         }
181         else if (e.which) {             /* real browsers */
182                 keynum = e.which;
183         }
184         if (keynum == 13) {             /* enter/return key */
185                 ajax_try_openid();
186                 return false;
187         }
188         return true;
189 }
190
191
192 /****************** GOOGLE ***********************/
193
194 /*
195  * Attempt login with Google, called from modal dialog
196  */
197 function ajax_try_google() {
198         $('login_errmsg').innerHTML = "";
199         openid_url = encodeURI("https://www.google.com/accounts/o8/id");
200         do_auth_popout("openid_login?openid_url=" + openid_url);
201 }
202
203
204 /****************** GOOGLE ***********************/
205
206 /*
207  * Attempt login with Yahoo, called from modal dialog
208  */
209 function ajax_try_yahoo() {
210         $('login_errmsg').innerHTML = "";
211         openid_url = encodeURI("http://yahoo.com");
212         do_auth_popout("openid_login?openid_url=" + openid_url);
213 }
214
215
216 /****************** AOL ***********************/
217
218 /*
219  * Attempt login with AOL, called from modal dialog
220  */
221 function ajax_try_aol() {
222         $('login_errmsg').innerHTML = "";
223         openid_url = encodeURI($('ajax_aol_form').elements["aol_screenname"].value);
224         do_auth_popout("openid_login?openid_url=http://openid.aol.com/" + openid_url);
225 }
226
227
228 /*
229  * The user pressed a key while in the AOL login box.
230  * Is it the enter/return key?  Submit the form.
231  */
232 function aol_onkeypress(e) {
233         if (window.event) {             /* IE */
234                 keynum = e.keyCode;
235         }
236         else if (e.which) {             /* real browsers */
237                 keynum = e.which;
238         }
239         if (keynum == 13) {             /* enter/return key */
240                 ajax_try_aol();
241                 return false;
242         }
243         return true;
244 }
245
246