1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add support for username/password parameters for tm-login message

This commit is contained in:
jeremy@jermolene.com 2020-10-25 16:33:44 +00:00
parent 5cc1600072
commit f7f55e8eff
3 changed files with 37 additions and 36 deletions

View File

@ -113,8 +113,16 @@ function Syncer(options) {
return confirmationMessage;
});
// Listen out for login/logout/refresh events in the browser
$tw.rootWidget.addEventListener("tm-login",function() {
self.handleLoginEvent();
$tw.rootWidget.addEventListener("tm-login",function(event) {
var username = event && event.paramObject && event.paramObject.username,
password = event && event.paramObject && event.paramObject.password;
if(username && password) {
// Login with username and password
self.login(username,password,function() {});
} else {
// No username and password, so we display a prompt
self.handleLoginEvent();
}
});
$tw.rootWidget.addEventListener("tm-logout",function() {
self.handleLogoutEvent();
@ -400,23 +408,31 @@ Syncer.prototype.handleLoginEvent = function() {
var self = this;
this.getStatus(function(err,isLoggedIn,username) {
if(!err && !isLoggedIn) {
var promptInfo = $tw.passwordPrompt.createPrompt({
serviceName: $tw.language.getString("LoginToTiddlySpace"),
callback: function(data) {
self.login(data.username,data.password,function(err,isLoggedIn) {
self.syncFromServer();
});
return true; // Get rid of the password prompt
}
});
// Let the sync adaptor adjust the prompt
if(self.syncadaptor && self.syncadaptor.customiseLoginPrompt) {
self.syncadaptor.customiseLoginPrompt(promptInfo);
if(self.syncadaptor && self.syncadaptor.displayLoginPrompt) {
self.syncadaptor.displayLoginPrompt(self);
} else {
self.displayLoginPrompt();
}
}
});
};
/*
Dispay a password prompt
*/
Syncer.prototype.displayLoginPrompt = function() {
var self = this;
var promptInfo = $tw.passwordPrompt.createPrompt({
serviceName: $tw.language.getString("LoginToTiddlySpace"),
callback: function(data) {
self.login(data.username,data.password,function(err,isLoggedIn) {
self.syncFromServer();
});
return true; // Get rid of the password prompt
}
});
};
/*
Attempt to login to TiddlyWeb.
username: username

View File

@ -1,5 +1,5 @@
created: 20130825162100000
modified: 20201014124049248
modified: 20201025162413404
tags: dev moduletypes
title: SyncAdaptorModules
type: text/vnd.tiddlywiki
@ -80,30 +80,14 @@ Attempts to login to the server with specified credentials. This method is optio
|password |Password |
|callback |Callback function invoked with parameter `err` |
!! `customiseLoginPrompt(promptInfo)`
!! `displayLoginPrompt(syncer)`
Provides an opportunity to customise the login prompt.
Invoked by the syncer to display a custom login promopt. This method is optional.
|!Parameter |!Description |
|promptInfo |The `promptInfo` object returned by `$tw.passwordPrompt.createPrompt()` |
|syncer |Reference to the syncer object making the call |
Here's an example of customising the login prompt to include a "forgotten password" button:
```
SyncAdaptor.prototype.customiseLoginPrompt = function(promptInfo) {
promptInfo.form.appendChild($tw.utils.domMaker("button",{
attributes: {type: "submit"},
text: "Forgot password",
eventListeners: [{
name: "click",
handlerFunction: function(event) {
promptInfo.owner.removePrompt(promptInfo);
alert("Forgot password");
}
}]
}));
};
```
The custom login prompt should send the widget message `tm-login` with the username and password in parameters ''username'' and ''password''.
!! `logout(callback)`

View File

@ -1,5 +1,5 @@
created: 20140811112445887
modified: 20140811113336694
modified: 20201025163134940
tags: Messages
title: WidgetMessage: tm-login
type: text/vnd.tiddlywiki
@ -9,3 +9,4 @@ The login message prompts the user for a username and password and attempts to l
The login message is handled by the TiddlyWiki core SyncMechanism which invokes the current [[SyncAdaptorModule|SyncAdaptorModules]] (typically the ''tiddlywiki/tiddlywebadaptor'' plugin).
<<.from-version "5.1.23">> The login message can optionally accept parameters called ''username'' and ''password'' that immediately attempts to login with the specified credentials without displaying the prompt