From 417fb843dc0e5a29f6feb97dafc741a131b7946f Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 9 Mar 2025 14:22:38 +0800 Subject: [PATCH] feat(mobile): add deviceToken --- apps/mobile/src/lib/device-token.ts | 16 ++++++++++++++++ apps/mobile/src/modules/login/email.tsx | 14 ++++++++++---- .../src/screens/(modal)/forget-password.tsx | 8 +++++++- apps/mobile/src/screens/(modal)/sign-up.tsx | 16 +++++++++++----- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 apps/mobile/src/lib/device-token.ts diff --git a/apps/mobile/src/lib/device-token.ts b/apps/mobile/src/lib/device-token.ts new file mode 100644 index 000000000..3fcc0f93b --- /dev/null +++ b/apps/mobile/src/lib/device-token.ts @@ -0,0 +1,16 @@ +import { getDeviceToken } from "react-native-device-info" + +export async function getDeviceTokenHeaders() { + let deviceToken = "" + try { + deviceToken = await getDeviceToken() + } catch (error) { + console.error(error) + } + + return deviceToken + ? { + "x-token": `d:${deviceToken}`, + } + : undefined +} diff --git a/apps/mobile/src/modules/login/email.tsx b/apps/mobile/src/modules/login/email.tsx index 17f267ff7..53dc0d07e 100644 --- a/apps/mobile/src/modules/login/email.tsx +++ b/apps/mobile/src/modules/login/email.tsx @@ -11,6 +11,7 @@ import { z } from "zod" import { SubmitButton } from "@/src/components/common/SubmitButton" import { PlainTextField } from "@/src/components/ui/form/TextField" import { signIn } from "@/src/lib/auth" +import { getDeviceTokenHeaders } from "@/src/lib/device-token" import { toast } from "@/src/lib/toast" import { accentColor } from "@/src/theme/colors" @@ -23,10 +24,15 @@ type FormValue = z.infer async function onSubmit(values: FormValue) { await signIn - .email({ - email: values.email, - password: values.password, - }) + .email( + { + email: values.email, + password: values.password, + }, + { + headers: await getDeviceTokenHeaders(), + }, + ) .then((res) => { if (res.error) { throw new Error(res.error.message) diff --git a/apps/mobile/src/screens/(modal)/forget-password.tsx b/apps/mobile/src/screens/(modal)/forget-password.tsx index dc6c17482..103c76971 100644 --- a/apps/mobile/src/screens/(modal)/forget-password.tsx +++ b/apps/mobile/src/screens/(modal)/forget-password.tsx @@ -8,6 +8,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context" import { SubmitButton } from "@/src/components/common/SubmitButton" import { PlainTextField } from "@/src/components/ui/form/TextField" import { forgetPassword } from "@/src/lib/auth" +import { getDeviceTokenHeaders } from "@/src/lib/device-token" import { toast } from "@/src/lib/toast" export default function ForgetPassword() { @@ -16,7 +17,12 @@ export default function ForgetPassword() { const forgetPasswordMutation = useMutation({ mutationFn: async (email: string) => { - const res = await forgetPassword({ email }) + const res = await forgetPassword( + { email }, + { + headers: await getDeviceTokenHeaders(), + }, + ) if (res.error) { throw new Error(res.error.message) } diff --git a/apps/mobile/src/screens/(modal)/sign-up.tsx b/apps/mobile/src/screens/(modal)/sign-up.tsx index 4eddbe3cf..43254e365 100644 --- a/apps/mobile/src/screens/(modal)/sign-up.tsx +++ b/apps/mobile/src/screens/(modal)/sign-up.tsx @@ -12,6 +12,7 @@ import { SubmitButton } from "@/src/components/common/SubmitButton" import { PlainTextField } from "@/src/components/ui/form/TextField" import { Logo } from "@/src/components/ui/logo" import { signUp } from "@/src/lib/auth" +import { getDeviceTokenHeaders } from "@/src/lib/device-token" import { toast } from "@/src/lib/toast" import { accentColor } from "@/src/theme/colors" @@ -30,11 +31,16 @@ type FormValue = z.infer async function onSubmit(values: FormValue) { await signUp - .email({ - email: values.email, - password: values.password, - name: values.email.split("@")[0] ?? "", - }) + .email( + { + email: values.email, + password: values.password, + name: values.email.split("@")[0] ?? "", + }, + { + headers: await getDeviceTokenHeaders(), + }, + ) .then((res) => { if (res.error?.message) { toast.error(res.error.message)