create-signet-app 0.8.3 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/api/api/app.ts +15 -1
- package/base/api/api/env.ts +4 -0
- package/base/api/api/services.ts +55 -1
- package/package.json +3 -2
- package/parts/auth/api/api/routes/auth.ts +31 -8
- package/parts/mobile/api/api/routes/mobile.ts +71 -0
- package/parts/mobile/api/test/mobile.test.ts +104 -0
- package/parts/mobile/part.ts +11 -0
- package/parts/pwa/web/public/icon-192.png +0 -0
- package/parts/pwa/web/public/icon-512.png +0 -0
- package/parts/pwa/web/public/icon-maskable-512.png +0 -0
- package/parts/pwa/web/public/manifest.webmanifest +11 -1
- package/parts/pwa/web/public/sw.js +8 -1
- package/src/compose.ts +15 -4
- package/src/index.ts +53 -13
- package/src/mobile.ts +61 -0
- package/src/picker.ts +21 -8
- package/template-mobile/README.md +142 -0
- package/template-mobile/_github/workflows/ci.yml +38 -0
- package/template-mobile/_gitignore +1 -0
- package/template-mobile/mobile/_gitignore +16 -0
- package/template-mobile/mobile/app.config.ts +77 -0
- package/template-mobile/mobile/assets/adaptive-icon.png +0 -0
- package/template-mobile/mobile/assets/icon.png +0 -0
- package/template-mobile/mobile/assets/notification-icon.png +0 -0
- package/template-mobile/mobile/assets/splash-icon.png +0 -0
- package/template-mobile/mobile/babel.config.js +4 -0
- package/template-mobile/mobile/biome.json +8 -0
- package/template-mobile/mobile/eas.json +27 -0
- package/template-mobile/mobile/expo-env.d.ts +1 -0
- package/template-mobile/mobile/jest.config.js +9 -0
- package/template-mobile/mobile/jest.setup.js +32 -0
- package/template-mobile/mobile/src/__tests__/auth-gate.test.tsx +40 -0
- package/template-mobile/mobile/src/__tests__/sign-in.test.tsx +41 -0
- package/template-mobile/mobile/src/app/(app)/_layout.tsx +48 -0
- package/template-mobile/mobile/src/app/(app)/devices/[id].tsx +62 -0
- package/template-mobile/mobile/src/app/(app)/devices/_layout.tsx +19 -0
- package/template-mobile/mobile/src/app/(app)/devices/index.tsx +40 -0
- package/template-mobile/mobile/src/app/(app)/index.tsx +69 -0
- package/template-mobile/mobile/src/app/(app)/settings.tsx +116 -0
- package/template-mobile/mobile/src/app/(auth)/_layout.tsx +19 -0
- package/template-mobile/mobile/src/app/(auth)/sign-in.tsx +49 -0
- package/template-mobile/mobile/src/app/(auth)/verify.tsx +62 -0
- package/template-mobile/mobile/src/app/_layout.tsx +95 -0
- package/template-mobile/mobile/src/components/ui.tsx +230 -0
- package/template-mobile/mobile/src/lib/analytics.ts +20 -0
- package/template-mobile/mobile/src/lib/api.ts +49 -0
- package/template-mobile/mobile/src/lib/auth.ts +43 -0
- package/template-mobile/mobile/src/lib/config.ts +14 -0
- package/template-mobile/mobile/src/lib/devices.ts +18 -0
- package/template-mobile/mobile/src/lib/outbox.ts +84 -0
- package/template-mobile/mobile/src/lib/polyfills.ts +19 -0
- package/template-mobile/mobile/src/lib/push.ts +97 -0
- package/template-mobile/mobile/src/lib/session.ts +39 -0
- package/template-mobile/mobile/src/lib/storage.ts +17 -0
- package/template-mobile/mobile/src/lib/theme.tsx +64 -0
- package/template-mobile/mobile/tsconfig.json +11 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The native modules the app touches, as in-memory stand-ins: tests run in Node.
|
|
2
|
+
jest.mock("expo-secure-store", () => {
|
|
3
|
+
const m = new Map()
|
|
4
|
+
return {
|
|
5
|
+
getItemAsync: async (k) => m.get(k) ?? null,
|
|
6
|
+
setItemAsync: async (k, v) => void m.set(k, v),
|
|
7
|
+
deleteItemAsync: async (k) => void m.delete(k),
|
|
8
|
+
}
|
|
9
|
+
})
|
|
10
|
+
jest.mock("expo-sqlite/kv-store", () => {
|
|
11
|
+
const m = new Map()
|
|
12
|
+
return {
|
|
13
|
+
__esModule: true,
|
|
14
|
+
default: {
|
|
15
|
+
getItem: async (k) => m.get(k) ?? null,
|
|
16
|
+
setItem: async (k, v) => void m.set(k, v),
|
|
17
|
+
removeItem: async (k) => void m.delete(k),
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
jest.mock("expo-network", () => ({
|
|
22
|
+
addNetworkStateListener: () => ({ remove() {} }),
|
|
23
|
+
}))
|
|
24
|
+
jest.mock("expo-notifications", () => ({
|
|
25
|
+
setNotificationHandler: () => {},
|
|
26
|
+
useLastNotificationResponse: () => null,
|
|
27
|
+
getPermissionsAsync: async () => ({ granted: false, canAskAgain: true }),
|
|
28
|
+
requestPermissionsAsync: async () => ({ granted: false, canAskAgain: true }),
|
|
29
|
+
setNotificationChannelAsync: async () => {},
|
|
30
|
+
getExpoPushTokenAsync: async () => ({ data: "ExponentPushToken[test]" }),
|
|
31
|
+
AndroidImportance: { DEFAULT: 3 },
|
|
32
|
+
}))
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { expect, test } from "@jest/globals"
|
|
2
|
+
import { act, screen } from "@testing-library/react-native"
|
|
3
|
+
import { Slot } from "expo-router"
|
|
4
|
+
import { renderRouter } from "expo-router/testing-library"
|
|
5
|
+
import { Text } from "react-native"
|
|
6
|
+
import RootLayout from "@/app/_layout"
|
|
7
|
+
import * as AuthLayout from "@/app/(auth)/_layout"
|
|
8
|
+
import { session } from "@/lib/session"
|
|
9
|
+
|
|
10
|
+
// The real root layout (the gate) over stand-in screens.
|
|
11
|
+
const routes = {
|
|
12
|
+
_layout: RootLayout,
|
|
13
|
+
"(auth)/_layout": AuthLayout,
|
|
14
|
+
"(auth)/sign-in": () => <Text>Sign-in screen</Text>,
|
|
15
|
+
"(auth)/verify": () => <Text>Verify screen</Text>,
|
|
16
|
+
"(app)/_layout": () => <Slot />,
|
|
17
|
+
"(app)/index": () => <Text>Home screen</Text>,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test("signed out: the sign-in screen, whatever the link", async () => {
|
|
21
|
+
await session.clear()
|
|
22
|
+
await renderRouter(routes, { initialUrl: "/" })
|
|
23
|
+
expect(await screen.findByText("Sign-in screen")).toBeTruthy()
|
|
24
|
+
expect(screen.queryByText("Home screen")).toBeNull()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("signed in: a sign-in link opens the app instead", async () => {
|
|
28
|
+
await session.set({ accessToken: "access", refreshToken: "refresh" })
|
|
29
|
+
await renderRouter(routes, { initialUrl: "/sign-in" })
|
|
30
|
+
expect(await screen.findByText("Home screen")).toBeTruthy()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test("signed in: the app, and back to sign-in once the session ends", async () => {
|
|
34
|
+
await session.set({ accessToken: "access", refreshToken: "refresh" })
|
|
35
|
+
await renderRouter(routes, { initialUrl: "/" })
|
|
36
|
+
expect(await screen.findByText("Home screen")).toBeTruthy()
|
|
37
|
+
// What a 401 the refresh could not fix does (lib/api.ts): the gate flips.
|
|
38
|
+
await act(() => session.clear())
|
|
39
|
+
expect(await screen.findByText("Sign-in screen")).toBeTruthy()
|
|
40
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { beforeEach, expect, jest, test } from "@jest/globals"
|
|
2
|
+
import { fireEvent, screen } from "@testing-library/react-native"
|
|
3
|
+
import { Slot } from "expo-router"
|
|
4
|
+
import { renderRouter } from "expo-router/testing-library"
|
|
5
|
+
import { Text } from "react-native"
|
|
6
|
+
import SignIn from "@/app/(auth)/sign-in"
|
|
7
|
+
import { requestCode } from "@/lib/auth"
|
|
8
|
+
import { ThemeProvider } from "@/lib/theme"
|
|
9
|
+
|
|
10
|
+
jest.mock("@/lib/auth", () => ({ requestCode: jest.fn(async () => "+1 ••• 0199") }))
|
|
11
|
+
const sent = requestCode as jest.MockedFunction<typeof requestCode>
|
|
12
|
+
|
|
13
|
+
const routes = {
|
|
14
|
+
_layout: () => (
|
|
15
|
+
<ThemeProvider>
|
|
16
|
+
<Slot />
|
|
17
|
+
</ThemeProvider>
|
|
18
|
+
),
|
|
19
|
+
"sign-in": SignIn,
|
|
20
|
+
verify: () => <Text>Verify screen</Text>,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
sent.mockClear()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("a number that cannot be a phone is caught before anything is sent", async () => {
|
|
28
|
+
await renderRouter(routes, { initialUrl: "/sign-in" })
|
|
29
|
+
await fireEvent.changeText(await screen.findByLabelText("Phone number"), "12")
|
|
30
|
+
await fireEvent.press(screen.getByRole("button", { name: "Send code" }))
|
|
31
|
+
expect(await screen.findByText("Enter your phone number with its country code.")).toBeTruthy()
|
|
32
|
+
expect(sent).not.toHaveBeenCalled()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test("a valid number sends the code and opens the code screen", async () => {
|
|
36
|
+
await renderRouter(routes, { initialUrl: "/sign-in" })
|
|
37
|
+
await fireEvent.changeText(await screen.findByLabelText("Phone number"), "+1 555 010 0199")
|
|
38
|
+
await fireEvent.press(screen.getByRole("button", { name: "Send code" }))
|
|
39
|
+
expect(await screen.findByText("Verify screen")).toBeTruthy()
|
|
40
|
+
expect(sent).toHaveBeenCalledWith("+15550100199")
|
|
41
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import Ionicons from "@expo/vector-icons/Ionicons"
|
|
2
|
+
import { Tabs } from "expo-router"
|
|
3
|
+
import type { ComponentProps } from "react"
|
|
4
|
+
import type { ColorValue } from "react-native"
|
|
5
|
+
import { useOutboxFlush } from "@/lib/outbox"
|
|
6
|
+
import { usePushRouting } from "@/lib/push"
|
|
7
|
+
import { useTheme } from "@/lib/theme"
|
|
8
|
+
|
|
9
|
+
type Icon = ComponentProps<typeof Ionicons>["name"]
|
|
10
|
+
const icon =
|
|
11
|
+
(name: Icon) =>
|
|
12
|
+
({ color, size }: { color: ColorValue; size: number }) => (
|
|
13
|
+
// The tab bar passes the tint colours set below, which are token strings.
|
|
14
|
+
<Ionicons name={name} color={String(color)} size={size} />
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
/** Signed in: the tabs, plus what only runs for a signed-in phone (push, the outbox). */
|
|
18
|
+
export default function AppLayout() {
|
|
19
|
+
const t = useTheme()
|
|
20
|
+
usePushRouting()
|
|
21
|
+
useOutboxFlush()
|
|
22
|
+
return (
|
|
23
|
+
<Tabs
|
|
24
|
+
screenOptions={{
|
|
25
|
+
headerStyle: { backgroundColor: t.colors.background },
|
|
26
|
+
headerTintColor: t.colors.foreground,
|
|
27
|
+
headerShadowVisible: false,
|
|
28
|
+
tabBarStyle: { backgroundColor: t.colors.card, borderTopColor: t.colors.border },
|
|
29
|
+
tabBarActiveTintColor: t.colors.primary,
|
|
30
|
+
tabBarInactiveTintColor: t.colors.mutedForeground,
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
33
|
+
<Tabs.Screen name="index" options={{ title: "Home", tabBarIcon: icon("home-outline") }} />
|
|
34
|
+
<Tabs.Screen
|
|
35
|
+
name="devices"
|
|
36
|
+
options={{
|
|
37
|
+
title: "Devices",
|
|
38
|
+
headerShown: false,
|
|
39
|
+
tabBarIcon: icon("phone-portrait-outline"),
|
|
40
|
+
}}
|
|
41
|
+
/>
|
|
42
|
+
<Tabs.Screen
|
|
43
|
+
name="settings"
|
|
44
|
+
options={{ title: "Settings", tabBarIcon: icon("settings-outline") }}
|
|
45
|
+
/>
|
|
46
|
+
</Tabs>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createDateTimeFormatter } from "@dapplelight/signet-lib/format"
|
|
2
|
+
import { useQueryClient } from "@tanstack/react-query"
|
|
3
|
+
import { router, useLocalSearchParams } from "expo-router"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import { Body, Button, Screen, StateView, Title } from "@/components/ui"
|
|
6
|
+
import { api, messageOf } from "@/lib/api"
|
|
7
|
+
import { signOut } from "@/lib/auth"
|
|
8
|
+
import { relative, useDevices } from "@/lib/devices"
|
|
9
|
+
|
|
10
|
+
const dateTime = createDateTimeFormatter()
|
|
11
|
+
|
|
12
|
+
/** The sample detail: one device, and signing it out. */
|
|
13
|
+
export default function DeviceDetail() {
|
|
14
|
+
const { id } = useLocalSearchParams<{ id: string }>()
|
|
15
|
+
const qc = useQueryClient()
|
|
16
|
+
const devices = useDevices()
|
|
17
|
+
const [busy, setBusy] = useState(false)
|
|
18
|
+
const [error, setError] = useState<string>()
|
|
19
|
+
const device = devices.data?.find((d) => d.id === id)
|
|
20
|
+
|
|
21
|
+
if (!device)
|
|
22
|
+
return (
|
|
23
|
+
<Screen>
|
|
24
|
+
<StateView
|
|
25
|
+
loading={devices.isPending}
|
|
26
|
+
error={devices.error}
|
|
27
|
+
empty="This device is no longer signed in."
|
|
28
|
+
onRetry={() => devices.refetch()}
|
|
29
|
+
/>
|
|
30
|
+
</Screen>
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
const revoke = async () => {
|
|
34
|
+
setBusy(true)
|
|
35
|
+
try {
|
|
36
|
+
if (device.current) return await signOut()
|
|
37
|
+
await api.delete(`/api/sessions/${encodeURIComponent(device.id)}`)
|
|
38
|
+
await qc.invalidateQueries({ queryKey: ["devices"] })
|
|
39
|
+
router.back()
|
|
40
|
+
} catch (e) {
|
|
41
|
+
setError(messageOf(e))
|
|
42
|
+
setBusy(false)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<Screen>
|
|
48
|
+
<Title>{device.device}</Title>
|
|
49
|
+
{device.current ? <Body muted>This phone.</Body> : null}
|
|
50
|
+
<Body>{`Signed in ${dateTime(device.createdAt)}`}</Body>
|
|
51
|
+
<Body>{`Last active ${relative(device.lastSeenAt)}`}</Body>
|
|
52
|
+
{device.ip ? <Body muted>{`From ${device.ip}`}</Body> : null}
|
|
53
|
+
<Button
|
|
54
|
+
label={device.current ? "Sign out" : "Sign this device out"}
|
|
55
|
+
variant="destructive"
|
|
56
|
+
onPress={revoke}
|
|
57
|
+
loading={busy}
|
|
58
|
+
/>
|
|
59
|
+
{error ? <Body>{error}</Body> : null}
|
|
60
|
+
</Screen>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Stack } from "expo-router"
|
|
2
|
+
import { useTheme } from "@/lib/theme"
|
|
3
|
+
|
|
4
|
+
export default function DevicesLayout() {
|
|
5
|
+
const t = useTheme()
|
|
6
|
+
return (
|
|
7
|
+
<Stack
|
|
8
|
+
screenOptions={{
|
|
9
|
+
headerStyle: { backgroundColor: t.colors.background },
|
|
10
|
+
headerTintColor: t.colors.foreground,
|
|
11
|
+
headerShadowVisible: false,
|
|
12
|
+
contentStyle: { backgroundColor: t.colors.background },
|
|
13
|
+
}}
|
|
14
|
+
>
|
|
15
|
+
<Stack.Screen name="index" options={{ title: "Devices" }} />
|
|
16
|
+
<Stack.Screen name="[id]" options={{ title: "Device" }} />
|
|
17
|
+
</Stack>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { router } from "expo-router"
|
|
2
|
+
import { FlatList, RefreshControl } from "react-native"
|
|
3
|
+
import { Row, StateView } from "@/components/ui"
|
|
4
|
+
import { relative, useDevices } from "@/lib/devices"
|
|
5
|
+
import { useTheme } from "@/lib/theme"
|
|
6
|
+
|
|
7
|
+
/** The sample list: pull to refresh, with loading, empty and error states. */
|
|
8
|
+
export default function Devices() {
|
|
9
|
+
const t = useTheme()
|
|
10
|
+
const devices = useDevices()
|
|
11
|
+
return (
|
|
12
|
+
<FlatList
|
|
13
|
+
style={{ flex: 1, backgroundColor: t.colors.background }}
|
|
14
|
+
data={devices.data ?? []}
|
|
15
|
+
keyExtractor={(d) => d.id}
|
|
16
|
+
refreshControl={
|
|
17
|
+
<RefreshControl
|
|
18
|
+
refreshing={devices.isRefetching}
|
|
19
|
+
onRefresh={() => void devices.refetch()}
|
|
20
|
+
tintColor={t.colors.primary}
|
|
21
|
+
/>
|
|
22
|
+
}
|
|
23
|
+
ListEmptyComponent={
|
|
24
|
+
<StateView
|
|
25
|
+
loading={devices.isPending}
|
|
26
|
+
error={devices.error}
|
|
27
|
+
empty="No devices yet."
|
|
28
|
+
onRetry={() => devices.refetch()}
|
|
29
|
+
/>
|
|
30
|
+
}
|
|
31
|
+
renderItem={({ item }) => (
|
|
32
|
+
<Row
|
|
33
|
+
title={item.current ? `${item.device} (this phone)` : item.device}
|
|
34
|
+
detail={`Active ${relative(item.lastSeenAt)}`}
|
|
35
|
+
onPress={() => router.push({ pathname: "/devices/[id]", params: { id: item.id } })}
|
|
36
|
+
/>
|
|
37
|
+
)}
|
|
38
|
+
/>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query"
|
|
2
|
+
import { useEffect, useState } from "react"
|
|
3
|
+
import { Body, Button, Screen, StateView, TextField, Title } from "@/components/ui"
|
|
4
|
+
import { api, messageOf, type User } from "@/lib/api"
|
|
5
|
+
import { sendOrQueue, usePendingWrites } from "@/lib/outbox"
|
|
6
|
+
|
|
7
|
+
/** Home: who is signed in, and one write that works offline (your name). */
|
|
8
|
+
export default function Home() {
|
|
9
|
+
const qc = useQueryClient()
|
|
10
|
+
const me = useQuery({
|
|
11
|
+
queryKey: ["me"],
|
|
12
|
+
queryFn: () => api.get<{ user: User }>("/api/account/me").then((r) => r.user),
|
|
13
|
+
})
|
|
14
|
+
const pending = usePendingWrites()
|
|
15
|
+
const [name, setName] = useState("")
|
|
16
|
+
const [note, setNote] = useState<string>()
|
|
17
|
+
const [error, setError] = useState<string>()
|
|
18
|
+
const [busy, setBusy] = useState(false)
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (me.data?.name) setName(me.data.name)
|
|
21
|
+
}, [me.data?.name])
|
|
22
|
+
|
|
23
|
+
if (!me.data)
|
|
24
|
+
return (
|
|
25
|
+
<Screen>
|
|
26
|
+
<StateView loading={me.isPending} error={me.error} onRetry={() => me.refetch()} />
|
|
27
|
+
</Screen>
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const save = async () => {
|
|
31
|
+
const value = name.trim()
|
|
32
|
+
if (!value) return setError("Enter a name.")
|
|
33
|
+
setError(undefined)
|
|
34
|
+
setBusy(true)
|
|
35
|
+
try {
|
|
36
|
+
const r = await sendOrQueue({
|
|
37
|
+
method: "PATCH",
|
|
38
|
+
path: "/api/account/me",
|
|
39
|
+
body: { name: value },
|
|
40
|
+
})
|
|
41
|
+
setNote(r === "sent" ? "Saved." : "You are offline. It will be sent when you are back.")
|
|
42
|
+
qc.setQueryData<User>(["me"], (u) => (u ? { ...u, name: value } : u))
|
|
43
|
+
} catch (e) {
|
|
44
|
+
setError(messageOf(e))
|
|
45
|
+
} finally {
|
|
46
|
+
setBusy(false)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<Screen form>
|
|
52
|
+
<Title>{me.data.name ? `Hello, ${me.data.name}` : "Hello"}</Title>
|
|
53
|
+
<Body muted>{`Signed in as ${me.data.phone}.`}</Body>
|
|
54
|
+
<TextField
|
|
55
|
+
label="Your name"
|
|
56
|
+
value={name}
|
|
57
|
+
onChangeText={setName}
|
|
58
|
+
error={error}
|
|
59
|
+
autoComplete="name"
|
|
60
|
+
textContentType="name"
|
|
61
|
+
returnKeyType="done"
|
|
62
|
+
onSubmitEditing={save}
|
|
63
|
+
/>
|
|
64
|
+
<Button label="Save" onPress={save} loading={busy} />
|
|
65
|
+
{note ? <Body muted>{note}</Body> : null}
|
|
66
|
+
{pending ? <Body muted>{`${pending} change(s) waiting to be sent.`}</Body> : null}
|
|
67
|
+
</Screen>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { THEME_NAMES } from "@dapplelight/signet-tokens/native"
|
|
2
|
+
import { useState } from "react"
|
|
3
|
+
import { Pressable, Text, View } from "react-native"
|
|
4
|
+
import { Body, Button, Screen, TOUCH } from "@/components/ui"
|
|
5
|
+
import { api, messageOf } from "@/lib/api"
|
|
6
|
+
import { signOut } from "@/lib/auth"
|
|
7
|
+
import { APP_VERSION } from "@/lib/config"
|
|
8
|
+
import { usePushStatus } from "@/lib/push"
|
|
9
|
+
import { APPEARANCES, useTheme } from "@/lib/theme"
|
|
10
|
+
|
|
11
|
+
const capital = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
|
|
12
|
+
|
|
13
|
+
/** One choice of several, as a row of buttons; screen readers hear it as radio buttons. */
|
|
14
|
+
function Choice<T extends string>({
|
|
15
|
+
label,
|
|
16
|
+
options,
|
|
17
|
+
value,
|
|
18
|
+
onChange,
|
|
19
|
+
}: {
|
|
20
|
+
label: string
|
|
21
|
+
options: readonly T[]
|
|
22
|
+
value: T
|
|
23
|
+
onChange: (v: T) => void
|
|
24
|
+
}) {
|
|
25
|
+
const t = useTheme()
|
|
26
|
+
return (
|
|
27
|
+
<View accessibilityRole="radiogroup" accessibilityLabel={label} style={{ gap: t.spacing * 2 }}>
|
|
28
|
+
<Text style={{ color: t.colors.foreground, fontSize: t.fontSize.sm, fontWeight: "500" }}>
|
|
29
|
+
{label}
|
|
30
|
+
</Text>
|
|
31
|
+
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: t.spacing * 2 }}>
|
|
32
|
+
{options.map((o) => {
|
|
33
|
+
const on = o === value
|
|
34
|
+
return (
|
|
35
|
+
<Pressable
|
|
36
|
+
key={o}
|
|
37
|
+
accessibilityRole="radio"
|
|
38
|
+
accessibilityLabel={capital(o)}
|
|
39
|
+
accessibilityState={{ selected: on }}
|
|
40
|
+
onPress={() => onChange(o)}
|
|
41
|
+
style={{
|
|
42
|
+
minHeight: TOUCH,
|
|
43
|
+
minWidth: TOUCH,
|
|
44
|
+
paddingHorizontal: t.spacing * 4,
|
|
45
|
+
justifyContent: "center",
|
|
46
|
+
borderRadius: t.radius.md,
|
|
47
|
+
borderWidth: 1,
|
|
48
|
+
borderColor: on ? t.colors.primary : t.colors.border,
|
|
49
|
+
backgroundColor: on ? t.colors.accent : t.colors.card,
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
<Text style={{ color: on ? t.colors.accentForeground : t.colors.cardForeground }}>
|
|
53
|
+
{capital(o)}
|
|
54
|
+
</Text>
|
|
55
|
+
</Pressable>
|
|
56
|
+
)
|
|
57
|
+
})}
|
|
58
|
+
</View>
|
|
59
|
+
</View>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default function Settings() {
|
|
64
|
+
const t = useTheme()
|
|
65
|
+
const push = usePushStatus()
|
|
66
|
+
const [note, setNote] = useState<string>()
|
|
67
|
+
const [busy, setBusy] = useState(false)
|
|
68
|
+
|
|
69
|
+
const testPush = async () => {
|
|
70
|
+
try {
|
|
71
|
+
await api.post("/api/mobile/push/test")
|
|
72
|
+
setNote("Sent. It should arrive in a few seconds.")
|
|
73
|
+
} catch (e) {
|
|
74
|
+
setNote(messageOf(e))
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<Screen form>
|
|
80
|
+
<Choice
|
|
81
|
+
label="Theme"
|
|
82
|
+
options={THEME_NAMES}
|
|
83
|
+
value={t.prefs.theme}
|
|
84
|
+
onChange={(theme) => t.setPrefs({ theme })}
|
|
85
|
+
/>
|
|
86
|
+
<Choice
|
|
87
|
+
label="Appearance"
|
|
88
|
+
options={APPEARANCES}
|
|
89
|
+
value={t.prefs.appearance}
|
|
90
|
+
onChange={(appearance) => t.setPrefs({ appearance })}
|
|
91
|
+
/>
|
|
92
|
+
{push.on === false ? (
|
|
93
|
+
<Button
|
|
94
|
+
label="Turn on notifications"
|
|
95
|
+
variant="secondary"
|
|
96
|
+
onPress={push.enable}
|
|
97
|
+
hint="Asks for permission to send notifications"
|
|
98
|
+
/>
|
|
99
|
+
) : null}
|
|
100
|
+
{push.on ? (
|
|
101
|
+
<Button label="Send a test notification" variant="secondary" onPress={testPush} />
|
|
102
|
+
) : null}
|
|
103
|
+
{note ? <Body muted>{note}</Body> : null}
|
|
104
|
+
<Button
|
|
105
|
+
label="Sign out"
|
|
106
|
+
variant="destructive"
|
|
107
|
+
loading={busy}
|
|
108
|
+
onPress={() => {
|
|
109
|
+
setBusy(true)
|
|
110
|
+
void signOut()
|
|
111
|
+
}}
|
|
112
|
+
/>
|
|
113
|
+
<Body muted>{`Version ${APP_VERSION}`}</Body>
|
|
114
|
+
</Screen>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Stack } from "expo-router"
|
|
2
|
+
import { useTheme } from "@/lib/theme"
|
|
3
|
+
|
|
4
|
+
export default function AuthLayout() {
|
|
5
|
+
const t = useTheme()
|
|
6
|
+
return (
|
|
7
|
+
<Stack
|
|
8
|
+
screenOptions={{
|
|
9
|
+
headerShadowVisible: false,
|
|
10
|
+
headerStyle: { backgroundColor: t.colors.background },
|
|
11
|
+
headerTintColor: t.colors.foreground,
|
|
12
|
+
contentStyle: { backgroundColor: t.colors.background },
|
|
13
|
+
}}
|
|
14
|
+
>
|
|
15
|
+
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
|
|
16
|
+
<Stack.Screen name="verify" options={{ title: "Enter the code" }} />
|
|
17
|
+
</Stack>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { router } from "expo-router"
|
|
2
|
+
import { useState } from "react"
|
|
3
|
+
import { Body, Button, Screen, TextField, Title } from "@/components/ui"
|
|
4
|
+
import { messageOf } from "@/lib/api"
|
|
5
|
+
import { requestCode } from "@/lib/auth"
|
|
6
|
+
|
|
7
|
+
/** Digits with an optional leading +, 6 to 15 of them: the server normalises the rest. */
|
|
8
|
+
const PHONE = /^\+?\d{6,15}$/
|
|
9
|
+
|
|
10
|
+
export default function SignIn() {
|
|
11
|
+
const [phone, setPhone] = useState("")
|
|
12
|
+
const [error, setError] = useState<string>()
|
|
13
|
+
const [busy, setBusy] = useState(false)
|
|
14
|
+
|
|
15
|
+
const submit = async () => {
|
|
16
|
+
const value = phone.replace(/[\s()-]/g, "")
|
|
17
|
+
if (!PHONE.test(value)) return setError("Enter your phone number with its country code.")
|
|
18
|
+
setError(undefined)
|
|
19
|
+
setBusy(true)
|
|
20
|
+
try {
|
|
21
|
+
const to = await requestCode(value)
|
|
22
|
+
router.push({ pathname: "/verify", params: { phone: value, to } })
|
|
23
|
+
} catch (e) {
|
|
24
|
+
setError(messageOf(e))
|
|
25
|
+
} finally {
|
|
26
|
+
setBusy(false)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<Screen form edges={["top", "bottom"]}>
|
|
32
|
+
<Title>Sign in</Title>
|
|
33
|
+
<Body muted>We will text you a code. No password needed.</Body>
|
|
34
|
+
<TextField
|
|
35
|
+
label="Phone number"
|
|
36
|
+
placeholder="+1 555 010 0199"
|
|
37
|
+
value={phone}
|
|
38
|
+
onChangeText={setPhone}
|
|
39
|
+
error={error}
|
|
40
|
+
keyboardType="phone-pad"
|
|
41
|
+
autoComplete="tel"
|
|
42
|
+
textContentType="telephoneNumber"
|
|
43
|
+
returnKeyType="send"
|
|
44
|
+
onSubmitEditing={submit}
|
|
45
|
+
/>
|
|
46
|
+
<Button label="Send code" onPress={submit} loading={busy} />
|
|
47
|
+
</Screen>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useLocalSearchParams } from "expo-router"
|
|
2
|
+
import { useState } from "react"
|
|
3
|
+
import { Body, Button, Screen, TextField } from "@/components/ui"
|
|
4
|
+
import { messageOf } from "@/lib/api"
|
|
5
|
+
import { requestCode, verifyCode } from "@/lib/auth"
|
|
6
|
+
|
|
7
|
+
export default function Verify() {
|
|
8
|
+
const { phone = "", to = "" } = useLocalSearchParams<{ phone: string; to: string }>()
|
|
9
|
+
const [code, setCode] = useState("")
|
|
10
|
+
const [error, setError] = useState<string>()
|
|
11
|
+
const [busy, setBusy] = useState(false)
|
|
12
|
+
const [resent, setResent] = useState(false)
|
|
13
|
+
|
|
14
|
+
const submit = async () => {
|
|
15
|
+
if (!/^\d{4,8}$/.test(code)) return setError("Enter the code from the text message.")
|
|
16
|
+
setError(undefined)
|
|
17
|
+
setBusy(true)
|
|
18
|
+
try {
|
|
19
|
+
// On success the session is set and the auth gate swaps in the app.
|
|
20
|
+
await verifyCode(phone, code)
|
|
21
|
+
} catch (e) {
|
|
22
|
+
setError(messageOf(e))
|
|
23
|
+
setBusy(false)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const resend = async () => {
|
|
28
|
+
setError(undefined)
|
|
29
|
+
try {
|
|
30
|
+
await requestCode(phone)
|
|
31
|
+
setResent(true)
|
|
32
|
+
} catch (e) {
|
|
33
|
+
setError(messageOf(e))
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<Screen form>
|
|
39
|
+
<Body muted>{`We sent a code to ${to || phone}.`}</Body>
|
|
40
|
+
<TextField
|
|
41
|
+
label="Code"
|
|
42
|
+
value={code}
|
|
43
|
+
onChangeText={setCode}
|
|
44
|
+
error={error}
|
|
45
|
+
keyboardType="number-pad"
|
|
46
|
+
autoComplete="sms-otp"
|
|
47
|
+
textContentType="oneTimeCode"
|
|
48
|
+
maxLength={8}
|
|
49
|
+
autoFocus
|
|
50
|
+
returnKeyType="done"
|
|
51
|
+
onSubmitEditing={submit}
|
|
52
|
+
/>
|
|
53
|
+
<Button label="Sign in" onPress={submit} loading={busy} />
|
|
54
|
+
<Button
|
|
55
|
+
label={resent ? "Code sent again" : "Send a new code"}
|
|
56
|
+
variant="secondary"
|
|
57
|
+
onPress={resend}
|
|
58
|
+
disabled={resent}
|
|
59
|
+
/>
|
|
60
|
+
</Screen>
|
|
61
|
+
)
|
|
62
|
+
}
|