TiddlyWiki / TiddlyWiki5

A self-contained JavaScript wiki for the browser, Node.js, AWS Lambda etc.
https://tiddlywiki.com/
Other
8.09k stars 1.19k forks source link

How to trigger SyncAdaptor.login #3937

Closed linonetwo closed 5 years ago

linonetwo commented 5 years ago

https://github.com/Jermolene/TiddlyWiki5/blob/6b03ba9876c4f626648de41af7cbd34ef38ae7a8/core/modules/syncer.js#L311-L316

Does this mean that login() is useless for other sync adaptor developers?

When will this method get called? How can a user call this method?

Jermolene commented 5 years ago

Hi @linonetwo no, any sync adaptor can implement a login method. The syncer calls it when it receives the tm-login event, and at startup.

linonetwo commented 5 years ago

Thank you!

I will use a button in my plugin's about page to let user trigger login:

<$button>
  <$action-sendmessage $message="tm-login"/>
  Login
</$button>
Jermolene commented 5 years ago

Hi @linonetwo you've been asking interesting questions, may I ask what you're working on?

linonetwo commented 5 years ago

I'm reimplementing solid-syncadaptor, using bourgeoa's tiddlywiki-node-solid-server as reference.

SoLiD is TimBL's BaaS with OpenIDConnect and many metadata (RDF) related stuff.

@bourgeoa don't seem to have time continue his work recently, so I'm taking it on, refactoring it to store RDF back to the backend.

After refactoring, this adaptor can make a single tiddler choose to be public or private (due to WebACL inside SoLiD, need to enter username and password to get auth token to view some contents, while some content is public to everyone).

And ideally I think it can work just like TiddlyWeb, you can use it on mobile and desktop, after login.

Though I still have many confusions, I will ask them out in https://groups.google.com/forum/#!forum/tiddlywikidev .

linonetwo commented 5 years ago

When I click on:

<$button>
  <$action-sendmessage $message="tm-login"/>
  Login
</$button>

Nothing prompt out, why? ---update--- Because I return true in getStatus, so it won't login again.

But it shows Login to TiddlySpace, how to control what title to show up?

And can I pass it with parameter so I can use my own inputs?

In SoLiD, it will pop up a small window to choose your identity, just like "use Google Account" pop window. So I may just want to trigger SyncAdaptor.login, but not prompts the user for a username and password from TW as https://tiddlywiki.com/#WidgetMessage%3A%20tm-login said.

linonetwo commented 5 years ago

https://github.com/Jermolene/TiddlyWiki5/blob/07198b9cda12da82fc66dcf0589d6a9caab1cdf6/core/modules/syncer.js#L293-L298

This is bound to TiddlySpace, can't be reused by other adaptor.

And I can't simply override

$tw.rootWidget.addEventListener("tm-login",function() {
            self.handleLoginEvent();
});

Since it calls self.syncFromServer(); in the callback.

linonetwo commented 5 years ago

Well, I add my custom events:

  constructor(options: { wiki: Wiki }) {
    this.wiki = options.wiki;
    $tw.rootWidget.addEventListener('tm-login-solid', this.login);
  }

And dispatch tm-server-refresh after login.

  async login() {
    let session = await solidAuthClient.currentSession();
    const popupUri = 'https://solid.community/common/popup.html';
    if (!session) {
      session = await solidAuthClient.popupLogin({ popupUri });
      (session: SoLiDSession);
    }
    $tw.rootWidget.dispatchEvent({ type: 'tm-server-refresh' });
  }

For logout, in the syncer.js it is independent from any backend unlike the login, so I can just:

  logout(callback: Function) {
    solidAuthClient.logout().then(callback);
  }