feat(external): move login and redirect route to external
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { useWhoami } from "@client/atoms/user"
|
||||
import { Avatar, AvatarFallback, AvatarImage } from "@follow/components/ui/avatar/index.jsx"
|
||||
import { getBackgroundGradient } from "@follow/utils/color"
|
||||
import { useMemo } from "react"
|
||||
|
||||
export const UserAvatar = () => {
|
||||
let user = useWhoami()
|
||||
|
||||
const colors = useMemo(
|
||||
() => getBackgroundGradient(user?.name || user?.image || ""),
|
||||
[user?.name, user?.image],
|
||||
)
|
||||
const colorfulStyle: React.CSSProperties = useMemo(() => {
|
||||
const [, , , bgAccent, bgAccentLight, bgAccentUltraLight] = colors
|
||||
return {
|
||||
// Create a bottom-left to top-right avatar fallback background gradient
|
||||
backgroundImage: `linear-gradient(to top right, ${bgAccent} 20%, ${bgAccentLight} 90%, ${bgAccentUltraLight} 150%)`,
|
||||
}
|
||||
}, [colors])
|
||||
|
||||
if (!user) {
|
||||
if (import.meta.env.DEV) {
|
||||
user = {
|
||||
name: "Innei",
|
||||
image: "https://avatars-githubusercontent.webp.se/u/41265413?v=4",
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
const { name, image } = user
|
||||
|
||||
return (
|
||||
<div className="flex h-20 items-center justify-center gap-8 px-10 py-4 text-2xl font-medium text-zinc-600 dark:text-zinc-300">
|
||||
<Avatar className="border">
|
||||
<AvatarImage
|
||||
className="aspect-square size-full duration-200 animate-in fade-in-0"
|
||||
src={image!}
|
||||
/>
|
||||
<AvatarFallback style={colorfulStyle} className="text-sm">
|
||||
{name?.slice(0, 2)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
|
||||
<div>{name}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { IN_ELECTRON, WEB_URL } from "@follow/shared/constants"
|
||||
import { signIn } from "@hono/auth-js/react"
|
||||
|
||||
export const LOGIN_CALLBACK_URL = `${WEB_URL}/redirect?app=follow`
|
||||
export type LoginRuntime = "browser" | "app"
|
||||
export const loginHandler = (provider: string, runtime: LoginRuntime = "app") => {
|
||||
if (IN_ELECTRON) {
|
||||
window.open(`${WEB_URL}/login?provider=${provider}`)
|
||||
} else {
|
||||
signIn(provider, {
|
||||
callbackUrl: runtime === "app" ? LOGIN_CALLBACK_URL : WEB_URL,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { setWhoami } from "@client/atoms/user"
|
||||
import { setIntegrationIdentify } from "@client/initialize/helper"
|
||||
import { useSession } from "@client/query/auth"
|
||||
import { useEffect } from "react"
|
||||
import { Outlet } from "react-router-dom"
|
||||
|
||||
export function Component() {
|
||||
return (
|
||||
<>
|
||||
<UserProvider />
|
||||
<Outlet />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const UserProvider = () => {
|
||||
const { session } = useSession()
|
||||
|
||||
useEffect(() => {
|
||||
if (!session?.user) return
|
||||
setWhoami(session.user)
|
||||
|
||||
setIntegrationIdentify(session.user)
|
||||
}, [session?.user])
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import { UserAvatar } from "@client/components/ui/user-avatar"
|
||||
import { LOGIN_CALLBACK_URL, loginHandler } from "@client/lib/auth"
|
||||
import { Logo } from "@follow/components/icons/logo.jsx"
|
||||
import { Button } from "@follow/components/ui/button/index.js"
|
||||
import { SessionProvider, signIn, signOut, useSession } from "@hono/auth-js/react"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useLocation, useNavigate } from "react-router-dom"
|
||||
|
||||
export function Component() {
|
||||
return (
|
||||
<SessionProvider>
|
||||
<Login />
|
||||
</SessionProvider>
|
||||
)
|
||||
}
|
||||
|
||||
function Login() {
|
||||
const { status } = useSession()
|
||||
const navigate = useNavigate()
|
||||
const [redirecting, setRedirecting] = useState(false)
|
||||
|
||||
const location = useLocation()
|
||||
const urlParams = new URLSearchParams(location.search)
|
||||
const provider = urlParams.get("provider")
|
||||
|
||||
const isAuthenticated = status === "authenticated"
|
||||
const onOpenInWebApp = () => {
|
||||
if (isAuthenticated) {
|
||||
navigate("/")
|
||||
}
|
||||
}
|
||||
|
||||
const { t } = useTranslation("external")
|
||||
|
||||
useEffect(() => {
|
||||
if (provider) {
|
||||
if (status === "authenticated") {
|
||||
signOut()
|
||||
}
|
||||
if (status === "unauthenticated") {
|
||||
signIn(provider, {
|
||||
callbackUrl: LOGIN_CALLBACK_URL,
|
||||
})
|
||||
}
|
||||
setRedirecting(true)
|
||||
}
|
||||
}, [status])
|
||||
|
||||
return (
|
||||
<div className="flex h-screen w-full flex-col items-center justify-center gap-10">
|
||||
<Logo className="size-20" />
|
||||
{!isAuthenticated ? (
|
||||
<h1 className="text-3xl font-bold">
|
||||
{t("login.logInTo")}
|
||||
{` ${APP_NAME}`}
|
||||
</h1>
|
||||
) : (
|
||||
<h1 className="-mb-6 text-3xl font-bold">
|
||||
{t("login.welcomeTo")}
|
||||
{` ${APP_NAME}`}
|
||||
</h1>
|
||||
)}
|
||||
{redirecting ? (
|
||||
<div>{t("login.redirecting")}</div>
|
||||
) : (
|
||||
<div className="flex flex-col gap-3">
|
||||
{isAuthenticated ? (
|
||||
<>
|
||||
<div className="center flex">
|
||||
<UserAvatar />
|
||||
<Button variant="ghost" onClick={() => signOut()}>
|
||||
<i className="i-mingcute-exit-line" />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex items-center justify-center gap-4">
|
||||
<Button variant="outline" onClick={onOpenInWebApp}>
|
||||
{t("login.backToWebApp")}
|
||||
</Button>
|
||||
<Button
|
||||
variant="primary"
|
||||
onClick={() => {
|
||||
navigate("/redirect?app=follow")
|
||||
}}
|
||||
>
|
||||
{t("login.openApp")}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Button
|
||||
className="h-[48px] w-[320px] rounded-[8px] !bg-black font-sans text-base text-white hover:!bg-black/80 focus:!border-black/80 focus:!ring-black/80"
|
||||
onClick={() => {
|
||||
loginHandler("github")
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-github-cute-fi mr-2 text-xl" /> {t("login.continueWithGitHub")}
|
||||
</Button>
|
||||
<Button
|
||||
className="h-[48px] w-[320px] rounded-[8px] bg-blue-500 font-sans text-base text-white hover:bg-blue-500/90 focus:!border-blue-500/80 focus:!ring-blue-500/80"
|
||||
onClick={() => {
|
||||
loginHandler("google")
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-google-cute-fi mr-2 text-xl" /> {t("login.continueWithGoogle")}
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { UserAvatar } from "@client/components/ui/user-avatar"
|
||||
import { apiClient } from "@client/lib/api-fetch"
|
||||
import { PoweredByFooter } from "@follow/components/common/PoweredByFooter.jsx"
|
||||
import { Button } from "@follow/components/ui/button/index.js"
|
||||
import { DEEPLINK_SCHEME } from "@follow/shared/constants"
|
||||
import { useEffect, useRef } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
|
||||
export function Component() {
|
||||
const navigate = useNavigate()
|
||||
const { t } = useTranslation("external")
|
||||
|
||||
const getCallbackUrl = async () => {
|
||||
const { data } = await apiClient["auth-app"]["new-session"].$post({})
|
||||
return {
|
||||
url: `${DEEPLINK_SCHEME}auth?token=${data.sessionToken}&userId=${data.userId}`,
|
||||
userId: data.userId,
|
||||
}
|
||||
}
|
||||
|
||||
const onceRef = useRef(false)
|
||||
useEffect(() => {
|
||||
if (onceRef.current) return
|
||||
onceRef.current = true
|
||||
getCallbackUrl().then(({ url }) => {
|
||||
window.open(url, "_top")
|
||||
|
||||
// If you are in development, you can use the following code to open the app
|
||||
console.info("Open in development app", url.replace(DEEPLINK_SCHEME, "follow-dev://"))
|
||||
})
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="flex h-screen w-full flex-col items-center justify-center gap-10 px-4 pb-12 pt-[30vh]">
|
||||
<UserAvatar />
|
||||
<h2 className="text-center">
|
||||
{t("redirect.successMessage", { app_name: APP_NAME })} <br />
|
||||
<br />
|
||||
{t("redirect.instruction", { app_name: APP_NAME })}
|
||||
</h2>
|
||||
<div className="center flex flex-col gap-4 sm:flex-row">
|
||||
<Button
|
||||
variant="text"
|
||||
className="h-14 px-10 text-base"
|
||||
onClick={() => {
|
||||
navigate("/")
|
||||
}}
|
||||
>
|
||||
{t("redirect.continueInBrowser")}
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
className="h-14 !rounded-full px-5 text-lg"
|
||||
onClick={async () => {
|
||||
const { url } = await getCallbackUrl()
|
||||
window.open(url, "_top")
|
||||
}}
|
||||
>
|
||||
{t("redirect.openApp", { app_name: APP_NAME })}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="grow" />
|
||||
<PoweredByFooter />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export const Component = () => {
|
||||
return <div>(*‘ v`*) Hello, Follow</div>
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { Link } from "react-router-dom"
|
||||
|
||||
export const Component = () => {
|
||||
if (!import.meta.env.DEV) {
|
||||
window.location.href = "/"
|
||||
return null
|
||||
}
|
||||
return (
|
||||
<Link to={"/share/feeds/41223694984583197"} className="text-red-500">
|
||||
To Feed
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
"@fastify/middie": "9.0.2",
|
||||
"@fastify/request-context": "6.0.1",
|
||||
"@fontsource/sn-pro": "5.1.0",
|
||||
"@hono/auth-js": "1.0.11",
|
||||
"@openpanel/web": "1.0.1",
|
||||
"@resvg/resvg-js": "2.6.2",
|
||||
"@sentry/react": "8.34.0",
|
||||
|
||||
Generated
+5
-2
@@ -652,6 +652,9 @@ importers:
|
||||
'@fontsource/sn-pro':
|
||||
specifier: 5.1.0
|
||||
version: 5.1.0
|
||||
'@hono/auth-js':
|
||||
specifier: 1.0.11
|
||||
version: 1.0.11(@auth/core@0.37.0)(hono@4.6.5(patch_hash=hhch3aogyxhxjttgmn52vtceyy))(react@18.3.1)
|
||||
'@openpanel/web':
|
||||
specifier: 1.0.1
|
||||
version: 1.0.1
|
||||
@@ -3608,7 +3611,7 @@ packages:
|
||||
'@radix-ui/react-slot@1.1.0':
|
||||
resolution: {integrity: sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==}
|
||||
peerDependencies:
|
||||
'@types/react': npm:types-react@19.0.0-rc.1
|
||||
'@types/react': '*'
|
||||
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||
peerDependenciesMeta:
|
||||
'@types/react':
|
||||
@@ -9475,7 +9478,7 @@ packages:
|
||||
'@microsoft/api-extractor': ^7.36.0
|
||||
'@swc/core': ^1
|
||||
postcss: ^8.4.12
|
||||
typescript: '>=4.5.0'
|
||||
typescript: 5.6.3
|
||||
peerDependenciesMeta:
|
||||
'@microsoft/api-extractor':
|
||||
optional: true
|
||||
|
||||
Reference in New Issue
Block a user