Removed legacy non-redirect Twitch auth code. Added "add bot to channel" button in user profile and relevant data to user type.
This commit is contained in:
parent
49a30dc237
commit
ee1a4662a7
|
@ -67,6 +67,7 @@ export type PrivateUser = {
|
||||||
twitchInfo?: {
|
twitchInfo?: {
|
||||||
twitchName: string
|
twitchName: string
|
||||||
controlToken: string
|
controlToken: string
|
||||||
|
botEnabled?: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,44 @@
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import React, { useState } from 'react'
|
import React, { MouseEventHandler, ReactNode, useState } from 'react'
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
|
|
||||||
import { copyToClipboard } from 'web/lib/util/copy'
|
import { copyToClipboard } from 'web/lib/util/copy'
|
||||||
import { linkTwitchAccount } from 'web/lib/twitch/link-twitch-account'
|
import { linkTwitchAccountRedirect } from 'web/lib/twitch/link-twitch-account'
|
||||||
import { LinkIcon } from '@heroicons/react/solid'
|
import { LinkIcon } from '@heroicons/react/solid'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { Button } from './../button'
|
import { Button, ColorType } from './../button'
|
||||||
import { LoadingIndicator } from './../loading-indicator'
|
import { LoadingIndicator } from './../loading-indicator'
|
||||||
import { Row } from './../layout/row'
|
import { Row } from './../layout/row'
|
||||||
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
||||||
|
import { updatePrivateUser } from 'web/lib/firebase/users'
|
||||||
|
import { deleteField } from 'firebase/firestore'
|
||||||
|
|
||||||
|
function BouncyButton(props: {
|
||||||
|
children: ReactNode
|
||||||
|
onClick?: MouseEventHandler<any>
|
||||||
|
color?: ColorType
|
||||||
|
}) {
|
||||||
|
const { children, onClick, color } = props
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
color={color}
|
||||||
|
size="lg"
|
||||||
|
onClick={onClick}
|
||||||
|
className="btn h-[inherit] flex-shrink-[inherit] border-none font-normal normal-case"
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export function TwitchPanel() {
|
export function TwitchPanel() {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const privateUser = usePrivateUser()
|
const privateUser = usePrivateUser()
|
||||||
|
|
||||||
|
const twitchInfo = privateUser?.twitchInfo
|
||||||
const twitchName = privateUser?.twitchInfo?.twitchName
|
const twitchName = privateUser?.twitchInfo?.twitchName
|
||||||
const twitchToken = privateUser?.twitchInfo?.controlToken
|
const twitchToken = privateUser?.twitchInfo?.controlToken
|
||||||
|
const twitchBotConnected = privateUser?.twitchInfo?.botEnabled
|
||||||
|
|
||||||
const linkIcon = <LinkIcon className="mr-2 h-6 w-6" aria-hidden="true" />
|
const linkIcon = <LinkIcon className="mr-2 h-6 w-6" aria-hidden="true" />
|
||||||
|
|
||||||
|
@ -34,13 +56,20 @@ export function TwitchPanel() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateBotConnected = (connected: boolean) => async () => {
|
||||||
|
if (user && twitchInfo) {
|
||||||
|
twitchInfo.botEnabled = connected
|
||||||
|
await updatePrivateUser(user.id, { twitchInfo })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [twitchLoading, setTwitchLoading] = useState(false)
|
const [twitchLoading, setTwitchLoading] = useState(false)
|
||||||
|
|
||||||
const createLink = async () => {
|
const createLink = async () => {
|
||||||
if (!user || !privateUser) return
|
if (!user || !privateUser) return
|
||||||
setTwitchLoading(true)
|
setTwitchLoading(true)
|
||||||
|
|
||||||
const promise = linkTwitchAccount(user, privateUser)
|
const promise = linkTwitchAccountRedirect(user, privateUser)
|
||||||
track('link twitch from profile')
|
track('link twitch from profile')
|
||||||
await promise
|
await promise
|
||||||
|
|
||||||
|
@ -81,12 +110,21 @@ export function TwitchPanel() {
|
||||||
)}
|
)}
|
||||||
data-tip="You must link your Twitch account first"
|
data-tip="You must link your Twitch account first"
|
||||||
>
|
>
|
||||||
<Button color="blue" size="lg" onClick={copyOverlayLink}>
|
<BouncyButton color="blue" onClick={copyOverlayLink}>
|
||||||
Copy overlay link
|
Copy overlay link
|
||||||
</Button>
|
</BouncyButton>
|
||||||
<Button color="indigo" size="lg" onClick={copyDockLink}>
|
<BouncyButton color="indigo" onClick={copyDockLink}>
|
||||||
Copy dock link
|
Copy dock link
|
||||||
</Button>
|
</BouncyButton>
|
||||||
|
{twitchBotConnected ? (
|
||||||
|
<BouncyButton color="red" onClick={updateBotConnected(false)}>
|
||||||
|
Remove bot from your channel
|
||||||
|
</BouncyButton>
|
||||||
|
) : (
|
||||||
|
<BouncyButton color="green" onClick={updateBotConnected(true)}>
|
||||||
|
Add bot to your channel
|
||||||
|
</BouncyButton>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -29,20 +29,14 @@ export async function initLinkTwitchAccount(
|
||||||
return [responseData.twitchAuthURL, responseFetch.then((r) => r.json())]
|
return [responseData.twitchAuthURL, responseFetch.then((r) => r.json())]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function linkTwitchAccount(user: User, privateUser: PrivateUser) {
|
export async function linkTwitchAccountRedirect(
|
||||||
|
user: User,
|
||||||
|
privateUser: PrivateUser
|
||||||
|
) {
|
||||||
const apiKey = privateUser.apiKey ?? (await generateNewApiKey(user.id))
|
const apiKey = privateUser.apiKey ?? (await generateNewApiKey(user.id))
|
||||||
if (!apiKey) throw new Error("Couldn't retrieve or create Manifold api key")
|
if (!apiKey) throw new Error("Couldn't retrieve or create Manifold api key")
|
||||||
|
|
||||||
const [twitchAuthURL, linkSuccessPromise] = await initLinkTwitchAccount(
|
const [twitchAuthURL] = await initLinkTwitchAccount(user.id, apiKey)
|
||||||
user.id,
|
|
||||||
apiKey
|
|
||||||
)
|
|
||||||
|
|
||||||
console.log('opening twitch link', twitchAuthURL)
|
|
||||||
window.location.href = twitchAuthURL
|
window.location.href = twitchAuthURL
|
||||||
|
|
||||||
const twitchInfo = await linkSuccessPromise
|
|
||||||
await updatePrivateUser(user.id, { twitchInfo })
|
|
||||||
|
|
||||||
console.log(`Successfully linked Twitch account '${twitchInfo.twitchName}'`)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import { track } from 'web/lib/service/analytics'
|
||||||
import { Row } from 'web/components/layout/row'
|
import { Row } from 'web/components/layout/row'
|
||||||
import { Button } from 'web/components/button'
|
import { Button } from 'web/components/button'
|
||||||
import { useTracking } from 'web/hooks/use-tracking'
|
import { useTracking } from 'web/hooks/use-tracking'
|
||||||
import { linkTwitchAccount } from 'web/lib/twitch/link-twitch-account'
|
import { linkTwitchAccountRedirect } from 'web/lib/twitch/link-twitch-account'
|
||||||
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
||||||
import { LoadingIndicator } from 'web/components/loading-indicator'
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
|
@ -26,7 +26,7 @@ export default function TwitchLandingPage() {
|
||||||
|
|
||||||
const callback =
|
const callback =
|
||||||
user && privateUser
|
user && privateUser
|
||||||
? () => linkTwitchAccount(user, privateUser)
|
? () => linkTwitchAccountRedirect(user, privateUser)
|
||||||
: async () => {
|
: async () => {
|
||||||
const result = await firebaseLogin()
|
const result = await firebaseLogin()
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ export default function TwitchLandingPage() {
|
||||||
const { user, privateUser } = await getUserAndPrivateUser(userId)
|
const { user, privateUser } = await getUserAndPrivateUser(userId)
|
||||||
if (!user || !privateUser) return
|
if (!user || !privateUser) return
|
||||||
|
|
||||||
await linkTwitchAccount(user, privateUser)
|
await linkTwitchAccountRedirect(user, privateUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
const [isLoading, setLoading] = useState(false)
|
const [isLoading, setLoading] = useState(false)
|
||||||
|
@ -49,7 +49,6 @@ export default function TwitchLandingPage() {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
toast.error('Failed to sign up. Please try again later.')
|
toast.error('Failed to sign up. Please try again later.')
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user