forked from matrix/element-web
Get login working on logout
This commit is contained in:
parent
5c3f11a7c7
commit
330aa285e6
|
@ -35,6 +35,7 @@ import {_t, _td, newTranslatableError} from 'matrix-react-sdk/src/languageHandle
|
||||||
import AutoDiscoveryUtils from 'matrix-react-sdk/src/utils/AutoDiscoveryUtils';
|
import AutoDiscoveryUtils from 'matrix-react-sdk/src/utils/AutoDiscoveryUtils';
|
||||||
import {AutoDiscovery} from "matrix-js-sdk/src/autodiscovery";
|
import {AutoDiscovery} from "matrix-js-sdk/src/autodiscovery";
|
||||||
import * as Lifecycle from "matrix-react-sdk/src/Lifecycle";
|
import * as Lifecycle from "matrix-react-sdk/src/Lifecycle";
|
||||||
|
import dis from "matrix-react-sdk/src/dispatcher";
|
||||||
|
|
||||||
import url from 'url';
|
import url from 'url';
|
||||||
|
|
||||||
|
@ -279,7 +280,9 @@ export async function loadApp() {
|
||||||
// load dendrite, if available
|
// load dendrite, if available
|
||||||
const vectorDendriteWorkerScript = document.body.dataset.vectorDendriteWorkerScript;
|
const vectorDendriteWorkerScript = document.body.dataset.vectorDendriteWorkerScript;
|
||||||
if (vectorDendriteWorkerScript && 'serviceWorker' in navigator) {
|
if (vectorDendriteWorkerScript && 'serviceWorker' in navigator) {
|
||||||
window.addEventListener('load', ()=>{
|
console.log("dendrite code exec... ", document.readyState);
|
||||||
|
const loadDendriteSw = ()=>{
|
||||||
|
console.log("Registering dendrite sw...");
|
||||||
navigator.serviceWorker.register(vectorDendriteWorkerScript, { scope: "/" }).then(function(registration) {
|
navigator.serviceWorker.register(vectorDendriteWorkerScript, { scope: "/" }).then(function(registration) {
|
||||||
// Registration was successful
|
// Registration was successful
|
||||||
console.log('ServiceWorker sw.js registration successful with scope: ', registration.scope);
|
console.log('ServiceWorker sw.js registration successful with scope: ', registration.scope);
|
||||||
|
@ -303,23 +306,28 @@ export async function loadApp() {
|
||||||
// periodically check for updates
|
// periodically check for updates
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
registration.update();
|
registration.update();
|
||||||
}, 1000 * 60) // once a minute
|
}, 1000 * 60 * 30) // once every 30 minutes
|
||||||
}, (err)=>{
|
}, (err)=>{
|
||||||
// registration failed :(
|
// registration failed :(
|
||||||
console.log('ServiceWorker registration failed: ', err)
|
console.log('ServiceWorker registration failed: ', err)
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
if (document.readyState === "loading") {
|
||||||
|
window.addEventListener('DOMContentLoaded', loadDendriteSw);
|
||||||
|
} else {
|
||||||
|
loadDendriteSw();
|
||||||
|
}
|
||||||
|
|
||||||
// check if we are logged in and if not, register.
|
// check if we are logged in and if not, register.
|
||||||
const owner = Lifecycle.getStoredSessionOwner();
|
const autoRegister = async () => {
|
||||||
if (!owner) {
|
console.log("dendrite: Auto-registration in progress");
|
||||||
console.log("Auto-registration in progress");
|
|
||||||
const cli = Matrix.createClient({
|
const cli = Matrix.createClient({
|
||||||
baseUrl: "https://p2p.riot.im",
|
baseUrl: "https://p2p.riot.im",
|
||||||
});
|
});
|
||||||
const password = Math.random() + "-" + Math.random();
|
const password = Math.random() + "-" + Math.random();
|
||||||
|
|
||||||
const response = await cli.register("p2p", password, "", { type: "m.login.dummy" });
|
const response = await cli.register("p2p", password, "", { type: "m.login.dummy" });
|
||||||
console.log("Auto-registration done ", response);
|
console.log("dendrite: Auto-registration done ", response);
|
||||||
await Lifecycle.setLoggedIn({
|
await Lifecycle.setLoggedIn({
|
||||||
userId: response.user_id,
|
userId: response.user_id,
|
||||||
deviceId: response.device_id,
|
deviceId: response.device_id,
|
||||||
|
@ -327,10 +335,30 @@ export async function loadApp() {
|
||||||
accessToken: response.access_token,
|
accessToken: response.access_token,
|
||||||
guest: cli.isGuest(),
|
guest: cli.isGuest(),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Auto-registration SET DONE>>>>>>>>");
|
|
||||||
// notify?
|
|
||||||
}
|
}
|
||||||
|
const owner = Lifecycle.getStoredSessionOwner();
|
||||||
|
if (!owner) {
|
||||||
|
console.log("dendrite: auto-registering, nothing in local storage");
|
||||||
|
await autoRegister()
|
||||||
|
console.log("dendrite: auto-registered because nothing in local storage");
|
||||||
|
}
|
||||||
|
dis.register((payload) => {
|
||||||
|
if (payload.action !== "on_logged_in") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("dendrite: on_logged_in, attaching logged out listener");
|
||||||
|
MatrixClientPeg.get().on('Session.logged_out', () => {
|
||||||
|
console.log("dendrite: Session.logged_out, re-registering");
|
||||||
|
// let the other listener for this clear the cache.
|
||||||
|
setTimeout(async () => {
|
||||||
|
await autoRegister();
|
||||||
|
console.log("dendrite: auto-registered due to logout, go to home");
|
||||||
|
dis.dispatch({
|
||||||
|
action: 'view_home_page',
|
||||||
|
});
|
||||||
|
}, 10)
|
||||||
|
})
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const validBrowser = checkBrowserFeatures();
|
const validBrowser = checkBrowserFeatures();
|
||||||
|
|
|
@ -151,13 +151,14 @@ async function sendRequestToGo(event) {
|
||||||
|
|
||||||
self.addEventListener('fetch', function(event) {
|
self.addEventListener('fetch', function(event) {
|
||||||
event.respondWith((async () => {
|
event.respondWith((async () => {
|
||||||
|
/*
|
||||||
// If this is a page refresh for the current page, then shunt in the new sw
|
// If this is a page refresh for the current page, then shunt in the new sw
|
||||||
// https://github.com/w3c/ServiceWorker/issues/1238
|
// https://github.com/w3c/ServiceWorker/issues/1238
|
||||||
if (event.request.mode === "navigate" && event.request.method === "GET" && registration.waiting && (await clients.matchAll()).length < 2) {
|
if (event.request.mode === "navigate" && event.request.method === "GET" && registration.waiting && (await clients.matchAll()).length < 2) {
|
||||||
console.log("Forcing new sw.js into page")
|
console.log("Forcing new sw.js into page")
|
||||||
registration.waiting.postMessage('skipWaiting');
|
registration.waiting.postMessage('skipWaiting');
|
||||||
return new Response("", {headers: {"Refresh": "0"}});
|
return new Response("", {headers: {"Refresh": "0"}});
|
||||||
}
|
} */
|
||||||
|
|
||||||
if (event.request.url.match(/\/_matrix\/client/)) {
|
if (event.request.url.match(/\/_matrix\/client/)) {
|
||||||
return await sendRequestToGo(event);
|
return await sendRequestToGo(event);
|
||||||
|
|
Loading…
Reference in New Issue