From 4ddf47f3daef144ce1951f965cef48d3bf2b607c Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 26 Sep 2019 05:28:52 -0700 Subject: [PATCH] feat: implement basic login mode (#1543) fixes #1542 --- src/routes/_actions/addInstance.js | 38 +++- src/routes/_components/InfoAside.html | 40 ++++ src/routes/_pages/settings/instances/add.html | 171 ++++++++++++------ src/routes/_pages/settings/wellness.html | 35 +--- tests/spec/002-login-spec.js | 16 +- tests/utils.js | 3 + 6 files changed, 209 insertions(+), 94 deletions(-) create mode 100644 src/routes/_components/InfoAside.html diff --git a/src/routes/_actions/addInstance.js b/src/routes/_actions/addInstance.js index 8e4344d3..1db0069b 100644 --- a/src/routes/_actions/addInstance.js +++ b/src/routes/_actions/addInstance.js @@ -8,14 +8,17 @@ import { updateCustomEmojiForInstance } from './emoji' import { database } from '../_database/database' import { DOMAIN_BLOCKS } from '../_static/blocks' -const REDIRECT_URI = process.browser && `${location.origin}/settings/instances/add` - function createKnownError (message) { const err = new Error(message) err.knownError = true return err } +function getRedirectUri () { + const { copyPasteMode } = store.get() + return copyPasteMode ? 'urn:ietf:wg:oauth:2.0:oob' : `${location.origin}/settings/instances/add` +} + async function redirectToOauth () { let { instanceNameInSearch, loggedInInstances } = store.get() instanceNameInSearch = instanceNameInSearch.replace(/^https?:\/\//, '').replace(/\/+$/, '').toLowerCase() @@ -26,7 +29,8 @@ async function redirectToOauth () { if (DOMAIN_BLOCKS.some(domain => new RegExp(`(?:\\.|^)${domain}$`, 'i').test(instanceHostname))) { throw createKnownError('This service is blocked') } - const registrationPromise = registerApplication(instanceNameInSearch, REDIRECT_URI) + const redirectUri = getRedirectUri() + const registrationPromise = registerApplication(instanceNameInSearch, redirectUri) const instanceInfo = await getInstanceInfo(instanceNameInSearch) await database.setInstanceInfo(instanceNameInSearch, instanceInfo) // cache for later const instanceData = await registrationPromise @@ -38,11 +42,16 @@ async function redirectToOauth () { const oauthUrl = generateAuthLink( instanceNameInSearch, instanceData.client_id, - REDIRECT_URI + redirectUri ) // setTimeout to allow the browser to *actually* save the localStorage data (fixes Safari bug apparently) + const { copyPasteMode } = store.get() setTimeout(() => { - document.location.href = oauthUrl + if (copyPasteMode) { + window.open(oauthUrl, '_blank', 'noopener') + } else { + document.location.href = oauthUrl + } }, 200) } @@ -72,12 +81,13 @@ export async function logInToInstance () { async function registerNewInstance (code) { const { currentRegisteredInstanceName, currentRegisteredInstance } = store.get() + const redirectUri = getRedirectUri() const instanceData = await getAccessTokenFromAuthCode( currentRegisteredInstanceName, currentRegisteredInstance.client_id, currentRegisteredInstance.client_secret, code, - REDIRECT_URI + redirectUri ) const { loggedInInstances, loggedInInstancesInOrder, instanceThemes } = store.get() instanceThemes[currentRegisteredInstanceName] = DEFAULT_THEME @@ -92,7 +102,8 @@ async function registerNewInstance (code) { loggedInInstances: loggedInInstances, currentInstance: currentRegisteredInstanceName, loggedInInstancesInOrder: loggedInInstancesInOrder, - instanceThemes: instanceThemes + instanceThemes: instanceThemes, + copyPasteMode: false }) store.save() const { enableGrayscale } = store.get() @@ -113,3 +124,16 @@ export async function handleOauthCode (code) { store.set({ logInToInstanceLoading: false }) } } + +export async function handleCopyPasteOauthCode (code) { + const { currentRegisteredInstanceName, currentRegisteredInstance } = store.get() + if (!currentRegisteredInstanceName || !currentRegisteredInstance) { + store.set({ + logInToInstanceError: 'You must log in to an instance first.', + logInToInstanceErrorForText: '', + instanceNameInSearch: '' + }) + } else { + await handleOauthCode(code) + } +} diff --git a/src/routes/_components/InfoAside.html b/src/routes/_components/InfoAside.html new file mode 100644 index 00000000..fae778a9 --- /dev/null +++ b/src/routes/_components/InfoAside.html @@ -0,0 +1,40 @@ + + + diff --git a/src/routes/_pages/settings/instances/add.html b/src/routes/_pages/settings/instances/add.html index 07324044..0f2fa9ae 100644 --- a/src/routes/_pages/settings/instances/add.html +++ b/src/routes/_pages/settings/instances/add.html @@ -1,65 +1,83 @@

Add instance

-
+
+ - {#if !hasIndexedDB || !hasLocalStorage} - + {#if !hasIndexedDB || !hasLocalStorage} + + {/if} + + {#if $logInToInstanceError && $logInToInstanceErrorForText === $instanceNameInSearch} + + {/if} + + + + + + + + + {#if $copyPasteMode } +
+ + + +
{/if} - - {#if $logInToInstanceError && $logInToInstanceErrorForText === $instanceNameInSearch} - - {/if} - - - - - - - +
{#if !$isUserLoggedIn} +

+ Don't have an + + ? + Join Mastodon! +

+ {/if}

- Don't have an - - ? - Join Mastodon! + {#if $copyPasteMode} + Switch back to + {:else} + Trouble logging in? Switch to + {/if} + .

+ {#if $copyPasteMode} + + In basic login mode, click "log in" to open a new window. Then copy the code and paste it above. + {/if}