Tulir Asokan
4 years ago
8 changed files with 158 additions and 29 deletions
-
15sticker/server/api/setup.py
-
5sticker/server/database/access_token.py
-
73web/src/setup/App.js
-
24web/src/setup/LoginView.js
-
4web/src/setup/index.js
-
14web/src/setup/matrix-api.js
-
34web/src/setup/sticker-api.js
-
18web/src/setup/tryGet.js
@ -0,0 +1,73 @@ |
|||
// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
|
|||
// Copyright (C) 2020 Tulir Asokan
|
|||
//
|
|||
// This program is free software: you can redistribute it and/or modify
|
|||
// it under the terms of the GNU Affero General Public License as published by
|
|||
// the Free Software Foundation, either version 3 of the License, or
|
|||
// (at your option) any later version.
|
|||
//
|
|||
// This program is distributed in the hope that it will be useful,
|
|||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
// GNU Affero General Public License for more details.
|
|||
//
|
|||
// You should have received a copy of the GNU Affero General Public License
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
import { useEffect, useState } from "../../lib/preact/hooks.js" |
|||
import { html } from "../../lib/htm/preact.js" |
|||
|
|||
import LoginView from "./LoginView.js" |
|||
import Spinner from "../Spinner.js" |
|||
import * as matrix from "./matrix-api.js" |
|||
import * as sticker from "./sticker-api.js" |
|||
|
|||
const App = () => { |
|||
const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.mxAccessToken)) |
|||
const [widgetSecret, setWidgetSecret] = useState(null) |
|||
const [loading, setLoading] = useState(false) |
|||
const [error, setError] = useState(null) |
|||
|
|||
if (!loggedIn) { |
|||
return html`
|
|||
<${LoginView} |
|||
onLoggedIn=${() => setLoggedIn(Boolean(localStorage.mxAccessToken))} |
|||
/>` |
|||
} |
|||
|
|||
useEffect(() => { |
|||
if (widgetSecret === null) { |
|||
setLoading(true) |
|||
const whoamiReceived = data => { |
|||
setLoading(false) |
|||
setWidgetSecret(data.widget_secret) |
|||
} |
|||
const reauth = async () => { |
|||
const openIDToken = await matrix.requestOpenIDToken( |
|||
localStorage.mxHomeserver, localStorage.mxUserID, localStorage.mxAccessToken) |
|||
const integrationData = await matrix.requestIntegrationToken(openIDToken) |
|||
localStorage.stickerSetupAccessToken = integrationData.token |
|||
return await sticker.whoami() |
|||
} |
|||
const whoamiErrored = err => { |
|||
console.error("Setup API whoami returned", err) |
|||
if (err.code === "NET.MAUNIUM_TOKEN_EXPIRED" || err.code === "M_UNKNOWN_TOKEN") { |
|||
return reauth().then(whoamiReceived) |
|||
} else { |
|||
throw err |
|||
} |
|||
} |
|||
sticker.whoami().then(whoamiReceived, whoamiErrored).catch(err => { |
|||
setLoading(false) |
|||
setError(err.message) |
|||
}) |
|||
} |
|||
}, []) |
|||
|
|||
if (loading) { |
|||
return html`<${Spinner} size=80 green />` |
|||
} |
|||
|
|||
return html`${widgetSecret}` |
|||
} |
|||
|
|||
export default App |
@ -0,0 +1,34 @@ |
|||
// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
|
|||
// Copyright (C) 2020 Tulir Asokan
|
|||
//
|
|||
// This program is free software: you can redistribute it and/or modify
|
|||
// it under the terms of the GNU Affero General Public License as published by
|
|||
// the Free Software Foundation, either version 3 of the License, or
|
|||
// (at your option) any later version.
|
|||
//
|
|||
// This program is distributed in the hope that it will be useful,
|
|||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
// GNU Affero General Public License for more details.
|
|||
//
|
|||
// You should have received a copy of the GNU Affero General Public License
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
|
|||
import { tryFetch as tryFetchDefault, setupPrefix } from "./tryGet.js" |
|||
|
|||
const service = "setup API" |
|||
|
|||
const tryFetch = (url, options, reqInfo) => { |
|||
if (!options.headers?.Authorization) { |
|||
if (!options.headers) { |
|||
options.headers = {} |
|||
} |
|||
options.headers.Authorization = `Bearer ${localStorage.stickerSetupAccessToken}` |
|||
} |
|||
return tryFetchDefault(url, options, reqInfo) |
|||
} |
|||
|
|||
export const whoami = () => tryFetch( |
|||
`${setupPrefix}/whoami`, |
|||
{}, { service, requestType: "whoami" }, |
|||
) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue