diff --git a/locales/en/translation.json b/locales/en/translation.json index a26c6b02..bf929d29 100644 --- a/locales/en/translation.json +++ b/locales/en/translation.json @@ -23,7 +23,10 @@ "title": "Add account", "domain": "Domain", "sign_in": "Sign in", - "authorize": "Authorize" + "authorize": "Authorize", + "authorization_code": "Authorization Code", + "authorization_helper": "Please paste the authorization code from your browser", + "without_code_authorize": "Please approve Fedistar in your browser, and after you approve it please authorize" }, "remove": "Remove" }, diff --git a/renderer/components/accounts/New.tsx b/renderer/components/accounts/New.tsx index c1f37e4b..959ca17e 100644 --- a/renderer/components/accounts/New.tsx +++ b/renderer/components/accounts/New.tsx @@ -1,5 +1,5 @@ import { Label, Modal, TextInput, Button } from 'flowbite-react' -import generator, { MegalodonInterface, detector } from 'megalodon' +import generator, { MegalodonInterface, OAuth, detector } from 'megalodon' import { useState } from 'react' import { db } from '@/db' import { FormattedMessage } from 'react-intl' @@ -13,15 +13,13 @@ export default function New(props: NewProps) { const [sns, setSNS] = useState<'mastodon' | 'pleroma' | 'firefish' | 'friendica' | null>(null) const [domain, setDomain] = useState('') const [client, setClient] = useState() - const [clientId, setClientId] = useState() - const [clientSecret, setClientSecret] = useState() + const [appData, setAppData] = useState() const close = () => { setSNS(null) setDomain('') setClient(undefined) - setClientId(undefined) - setClientSecret(undefined) + setAppData(undefined) props.close() } @@ -34,15 +32,20 @@ export default function New(props: NewProps) { const client = generator(sns, url) setClient(client) const appData = await client.registerApp('Whalebird', {}) - setClientId(appData.client_id) - setClientSecret(appData.client_secret) + setAppData(appData) global.ipc.invoke('open-browser', appData.url) } const authorize = async () => { + if (!client || !appData) return const input = document.getElementById('authorization') as HTMLInputElement - if (!client || !clientId || !clientSecret) return - const tokenData = await client.fetchAccessToken(clientId, clientSecret, input.value) + let authorizationCode = null + if (appData.session_token) { + authorizationCode = appData.session_token + } else { + authorizationCode = input.value + } + const tokenData = await client.fetchAccessToken(appData.client_id, appData.client_secret, authorizationCode) if (!sns) return const cli = generator(sns, `https://${domain}`, tokenData.access_token, 'Whalebird') const acct = await cli.verifyAccountCredentials() @@ -50,8 +53,8 @@ export default function New(props: NewProps) { username: acct.data.username, account_id: acct.data.id, avatar: acct.data.avatar, - client_id: clientId, - client_secret: clientSecret, + client_id: appData.client_id, + client_secret: appData.client_secret, access_token: tokenData.access_token, refresh_token: tokenData.refresh_token, url: `https://${domain}`, @@ -82,12 +85,28 @@ export default function New(props: NewProps) { )} - {sns && ( + {sns && appData && ( <> -
-
- + {appData.session_token ? ( + <> +
+ +
+ + ) : ( + <> +
+ +

+ +

+
+ + + )} +