@@ -5,9 +5,9 @@ import { Logo } from "@follow/components/icons/logo.jsx"
5
5
import { Button } from "@follow/components/ui/button/index.js"
6
6
import { DEEPLINK_SCHEME } from "@follow/shared/constants"
7
7
import { SessionProvider , signIn , signOut , useSession } from "@hono/auth-js/react"
8
- import { useEffect , useState } from "react"
8
+ import { useCallback , useEffect , useRef , useState } from "react"
9
9
import { useTranslation } from "react-i18next"
10
- import { useLocation , useNavigate } from "react-router"
10
+ import { useLocation } from "react-router"
11
11
12
12
export function Component ( ) {
13
13
return (
@@ -19,7 +19,7 @@ export function Component() {
19
19
20
20
function Login ( ) {
21
21
const { status } = useSession ( )
22
- const navigate = useNavigate ( )
22
+
23
23
const [ redirecting , setRedirecting ] = useState ( false )
24
24
25
25
const location = useLocation ( )
@@ -39,13 +39,26 @@ function Login() {
39
39
}
40
40
} , [ status ] )
41
41
42
- const getCallbackUrl = async ( ) => {
42
+ const getCallbackUrl = useCallback ( async ( ) => {
43
43
const { data } = await apiClient [ "auth-app" ] [ "new-session" ] . $post ( { } )
44
44
return {
45
45
url : `${ DEEPLINK_SCHEME } auth?token=${ data . sessionToken } &userId=${ data . userId } ` ,
46
46
userId : data . userId ,
47
47
}
48
- }
48
+ } , [ ] )
49
+
50
+ const handleOpenApp = useCallback ( async ( ) => {
51
+ const { url } = await getCallbackUrl ( )
52
+ window . open ( url , "_top" )
53
+ } , [ getCallbackUrl ] )
54
+
55
+ const onceRef = useRef ( false )
56
+ useEffect ( ( ) => {
57
+ if ( isAuthenticated && ! onceRef . current ) {
58
+ handleOpenApp ( )
59
+ }
60
+ onceRef . current = true
61
+ } , [ handleOpenApp , isAuthenticated ] )
49
62
50
63
return (
51
64
< div className = "flex h-screen w-full flex-col items-center justify-center gap-10" >
@@ -80,19 +93,13 @@ function Login() {
80
93
variant = "text"
81
94
className = "h-14 text-base"
82
95
onClick = { ( ) => {
83
- navigate ( "/" )
96
+ window . location . href = "/"
84
97
} }
85
98
>
86
99
{ t ( "redirect.continueInBrowser" ) }
87
100
</ Button >
88
101
89
- < Button
90
- className = "h-14 !rounded-full px-5 text-lg"
91
- onClick = { async ( ) => {
92
- const { url } = await getCallbackUrl ( )
93
- window . open ( url , "_top" )
94
- } }
95
- >
102
+ < Button className = "h-14 !rounded-full px-5 text-lg" onClick = { handleOpenApp } >
96
103
{ t ( "redirect.openApp" , { app_name : APP_NAME } ) }
97
104
</ Button >
98
105
</ div >
0 commit comments