You can try "double-submit" technique.
In short: on first submit cancel it, but trigger it again after successful auth and do not cancel it.
More detailed:
You need to rewrite your form like this:
<form id="loginForm" onSubmit="return App.Auth.login();" method="POST" action="/back-redirector">
/back-redirector
just redirects back to the same page or to the main page. I am redirecting back to the same page.
And your App.Auth.login()
function must return false
when it is called first time, and return true
if the first call has resulted in successful login.
Like this:
letTheLoginFormDoTheSubmit = false;
// flag to show that login was successful
// and we need to trigger password save prompt.
login: function() {
if (letTheLoginFormDoTheSubmit) {
return true;
}
// Do the login
// Start ajax
ajax(login, password, function() {
// This function is called when AJAX has completed
if (loginIsGood) {
letTheLoginFormDoTheSubmit = true;
loginForm.submit(); // trigger submit again
});
return false;
}
So what happens?
- When your login-function is called the first time it starts login sequence, and returns false to stop the usual form submit.
- Login function receives data, and if login is good it saves it on browser side, raises the flag "all good, trigger password save prompt" and submits the form again.
- But because flag is flying login-function does not do the login again it just lets the browser do form submit normally, and this triggers password save prompt.
Form is being submitted to the URL that just redirects browser back, and does not do any work.
Some caveats:
- Page will be reloaded, so save your auth cookie somehow locally, or let the redirector know which is it, so redirector can include it in a redirect.
- All form data may go through this second submit. To avoid this remove the relevant fields from the form, or remove the name attributes.
- Redirector is needed on the server side.
But in overall it is not a major change to the login process and should trigger password-save prompt in every browser.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…