Skip to content

Commit accbf7f

Browse files
committed
feat: use expo-secure-store and better-auth buildin getCookie for auth
1 parent 0fc90c7 commit accbf7f

File tree

8 files changed

+47
-96
lines changed

8 files changed

+47
-96
lines changed

apps/mobile/app.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default ({ config }: ConfigContext): ExpoConfig => ({
9191
"expo-apple-authentication",
9292
[require("./scripts/with-follow-assets.js")],
9393
[require("./scripts/with-follow-app-delegate.js")],
94+
"expo-secure-store",
9495
],
9596
experiments: {
9697
typedRoutes: true,

apps/mobile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"expo-linear-gradient": "~14.0.1",
5252
"expo-linking": "~7.0.3",
5353
"expo-router": "4.0.11",
54+
"expo-secure-store": "^14.0.1",
5455
"expo-sharing": "~13.0.0",
5556
"expo-splash-screen": "~0.29.18",
5657
"expo-sqlite": "15.0.3",

apps/mobile/src/lib/api-fetch.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AppType } from "@follow/shared"
33
import { router } from "expo-router"
44
import { ofetch } from "ofetch"
55

6-
import { getSessionToken } from "./cookie"
6+
import { getCookie } from "./auth"
77
import { getApiUrl } from "./env"
88

99
const { hc } = require("hono/dist/cjs/client/client") as typeof import("hono/client")
@@ -13,20 +13,10 @@ export const apiFetch = ofetch.create({
1313

1414
baseURL: getApiUrl(),
1515
onRequest: async ({ options, request }) => {
16-
const header = new Headers(options.headers)
17-
18-
header.set("x-app-name", "Follow Mobile")
19-
20-
const sessionToken = await getSessionToken()
21-
if (sessionToken) {
22-
header.set("cookie", `__Secure-better-auth.session_token=${sessionToken}`)
23-
}
2416
if (__DEV__) {
2517
// Logger
2618
console.log(`---> ${options.method} ${request as string}`)
2719
}
28-
29-
options.headers = header
3020
},
3121
onRequestError: ({ error, request, options }) => {
3222
if (__DEV__) {
@@ -60,6 +50,7 @@ export const apiClient = hc<AppType>(getApiUrl(), {
6050
headers() {
6151
return {
6252
"X-App-Name": "Follow Mobile",
53+
cookie: getCookie(),
6354
}
6455
},
6556
})

apps/mobile/src/lib/auth.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { expoClient } from "@better-auth/expo/client"
22
import { useQuery } from "@tanstack/react-query"
33
import { createAuthClient } from "better-auth/react"
44
import type * as better_call from "better-call"
5-
import { parse } from "cookie-es"
5+
import * as SecureStore from "expo-secure-store"
66

77
import { getApiUrl } from "./env"
8-
import { kv } from "./kv"
9-
import { queryClient } from "./query-client"
108

119
const storagePrefix = "follow_auth"
1210
export const cookieKey = `${storagePrefix}_cookie`
@@ -22,17 +20,7 @@ const authClient = createAuthClient({
2220
expoClient({
2321
scheme: "follow",
2422
storagePrefix,
25-
storage: {
26-
setItem(key, value) {
27-
kv.setSync(key, value)
28-
if (key === cookieKey) {
29-
queryClient.invalidateQueries({ queryKey: ["cookie"] })
30-
}
31-
},
32-
getItem(key) {
33-
return kv.getSync(key)
34-
},
35-
},
23+
storage: SecureStore,
3624
}),
3725
],
3826
})
@@ -54,18 +42,6 @@ export const useAuthProviders = () => {
5442
})
5543
}
5644

57-
export const getSessionTokenFromCookie = () => {
58-
const cookie = getCookie()
59-
return cookie ? parse(cookie)[sessionTokenKey] : null
60-
}
61-
62-
export const useAuthToken = () => {
63-
return useQuery({
64-
queryKey: ["cookie"],
65-
queryFn: getSessionTokenFromCookie,
66-
})
67-
}
68-
6945
// eslint-disable-next-line unused-imports/no-unused-vars
7046
declare const authPlugins: {
7147
id: "getProviders"

apps/mobile/src/lib/cookie.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

apps/mobile/src/screens/(headless)/debug.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { sleep } from "@follow/utils"
22
import * as Clipboard from "expo-clipboard"
33
import * as FileSystem from "expo-file-system"
44
import { Sitemap } from "expo-router/build/views/Sitemap"
5+
import * as SecureStore from "expo-secure-store"
56
import type { FC } from "react"
67
import * as React from "react"
78
import { useRef, useState } from "react"
@@ -18,7 +19,7 @@ import {
1819
import { useSafeAreaInsets } from "react-native-safe-area-context"
1920

2021
import { getDbPath } from "@/src/database"
21-
import { clearSessionToken, getSessionToken, setSessionToken } from "@/src/lib/cookie"
22+
import { cookieKey, getCookie, sessionTokenKey, signOut } from "@/src/lib/auth"
2223
import { loading } from "@/src/lib/loading"
2324
import { toast } from "@/src/lib/toast"
2425

@@ -44,14 +45,14 @@ export default function DebugPanel() {
4445
{
4546
title: "Get Current Session Token",
4647
onPress: async () => {
47-
const token = await getSessionToken()
48-
Alert.alert(`Current Session Token: ${token?.value}`)
48+
const token = getCookie()
49+
Alert.alert(`Current Session Token: ${token}`)
4950
},
5051
},
5152
{
5253
title: "Clear Session Token",
5354
onPress: async () => {
54-
await clearSessionToken()
55+
await signOut()
5556
Alert.alert("Session Token Cleared")
5657
},
5758
},
@@ -178,7 +179,14 @@ const UserSessionSetting = () => {
178179
<TouchableOpacity
179180
className="ml-2"
180181
onPress={() => {
181-
setSessionToken(input)
182+
SecureStore.setItem(
183+
cookieKey,
184+
JSON.stringify({
185+
[sessionTokenKey]: {
186+
value: input,
187+
},
188+
}),
189+
)
182190
Alert.alert("Session Token Saved")
183191
}}
184192
>

apps/mobile/src/screens/(headless)/webview.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
import { Redirect } from "expo-router"
2-
import { useEffect, useRef, useState } from "react"
1+
import { useRef } from "react"
32
import { TouchableOpacity, View } from "react-native"
43
import { useSafeAreaInsets } from "react-native-safe-area-context"
54
import type { WebView } from "react-native-webview"
65

7-
import { FollowWebView } from "@/src/components/common/FollowWebView"
86
import { BugCuteReIcon } from "@/src/icons/bug_cute_re"
97
import { ExitCuteReIcon } from "@/src/icons/exit_cute_re"
108
import { Refresh2CuteReIcon } from "@/src/icons/refresh_2_cute_re"
119
import { World2CuteReIcon } from "@/src/icons/world_2_cute_re"
12-
import { signOut, useAuthToken } from "@/src/lib/auth"
13-
import { setSessionToken } from "@/src/lib/cookie"
10+
import { signOut } from "@/src/lib/auth"
1411

1512
export default function Index() {
1613
const webViewRef = useRef<WebView>(null)
1714
const insets = useSafeAreaInsets()
1815

19-
const { data: token, isPending } = useAuthToken()
16+
// const { data: token, isPending } = useAuthToken()
2017

21-
const [isCookieReady, setIsCookieReady] = useState(false)
22-
useEffect(() => {
23-
if (!token) {
24-
return
25-
}
18+
// const [isCookieReady, setIsCookieReady] = useState(false)
19+
// useEffect(() => {
20+
// if (!token) {
21+
// return
22+
// }
2623

27-
setSessionToken(token).then(() => {
28-
setIsCookieReady(true)
29-
})
30-
}, [token])
24+
// // setSessionToken(token).then(() => {
25+
// // setIsCookieReady(true)
26+
// // })
27+
// }, [token])
3128

32-
if (!token && !isPending) {
33-
return <Redirect href="/login" />
34-
}
29+
// if (!token && !isPending) {
30+
// return <Redirect href="/login" />
31+
// }
3532

3633
return (
3734
<View className="flex-1 items-center justify-center pt-safe dark:bg-[#121212]">
38-
{isCookieReady && <FollowWebView webViewRef={webViewRef} />}
35+
{/* {isCookieReady && <FollowWebView webViewRef={webViewRef} />} */}
3936

4037
{__DEV__ && (
4138
<View

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)