fix: 成功对接登录接口
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,7 +8,7 @@ node_modules/
|
|||||||
dist/
|
dist/
|
||||||
web-build/
|
web-build/
|
||||||
expo-env.d.ts
|
expo-env.d.ts
|
||||||
|
tmpclaude-*
|
||||||
# Native
|
# Native
|
||||||
.kotlin/
|
.kotlin/
|
||||||
*.orig.*
|
*.orig.*
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { View, TextInput, ActivityIndicator } from "react-native";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { ActivityIndicator, TextInput } from "react-native";
|
||||||
|
import { authClient, setAuthToken, useSession } from "../../lib/auth";
|
||||||
|
import { Block } from "../ui";
|
||||||
import { Button } from "../ui/button";
|
import { Button } from "../ui/button";
|
||||||
import Text from "../ui/Text";
|
import Text from "../ui/Text";
|
||||||
import { authClient, useSession } from "../../lib/auth";
|
|
||||||
import { Block } from "../ui";
|
|
||||||
|
|
||||||
type AuthMode = "login" | "register";
|
type AuthMode = "login" | "register";
|
||||||
|
|
||||||
type AuthResponse = { data: unknown; error: { message: string; code?: string } | null };
|
const signIn = authClient.signIn
|
||||||
type SignInFn = (params: { username: string; password: string }) => Promise<AuthResponse>;
|
const signUp = authClient.signUp
|
||||||
type SignUpFn = (params: { email: string; password: string; name: string }) => Promise<AuthResponse>;
|
|
||||||
|
|
||||||
const signIn = authClient.signIn as unknown as { username: SignInFn };
|
|
||||||
const signUp = authClient.signUp as unknown as { email: SignUpFn };
|
|
||||||
|
|
||||||
interface AuthFormProps {
|
interface AuthFormProps {
|
||||||
mode?: AuthMode;
|
mode?: AuthMode;
|
||||||
@@ -60,17 +56,31 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (isLogin) {
|
if (isLogin) {
|
||||||
const res = await signIn.username({ username, password });
|
await signIn.username({ username, password }, {
|
||||||
if (res.error) {
|
onSuccess: async (ctx) => {
|
||||||
setError(getErrorMessage(res.error));
|
const authToken = ctx.response.headers.get('set-auth-token')
|
||||||
return;
|
if (authToken) {
|
||||||
|
setAuthToken(authToken)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onError: (ctx) => {
|
||||||
|
setError(getErrorMessage(ctx.error));
|
||||||
|
console.error(`[LOGIN] username login error`, ctx)
|
||||||
|
},
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const res = await signUp.email({ email, password, name: username });
|
await signUp.email({ email, password, name: username }, {
|
||||||
if (res.error) {
|
onSuccess: async (ctx) => {
|
||||||
setError(getErrorMessage(res.error));
|
const authToken = ctx.response.headers.get('set-auth-token')
|
||||||
return;
|
if (authToken) {
|
||||||
|
setAuthToken(authToken)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onError: (ctx) => {
|
||||||
|
setError(getErrorMessage(ctx.error));
|
||||||
|
console.error(`[LOGIN] username login error`, ctx)
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
onSuccess?.();
|
onSuccess?.();
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
@@ -158,3 +168,4 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
|
|||||||
}
|
}
|
||||||
|
|
||||||
export { useSession };
|
export { useSession };
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import 'reflect-metadata'
|
import 'reflect-metadata'
|
||||||
|
|
||||||
import Constants from 'expo-constants'
|
|
||||||
import { expoClient } from '@better-auth/expo/client'
|
import { expoClient } from '@better-auth/expo/client'
|
||||||
import { createSkerClientPlugin } from '@repo/sdk'
|
import { createSkerClientPlugin } from '@repo/sdk'
|
||||||
import {
|
import {
|
||||||
@@ -13,6 +12,7 @@ import {
|
|||||||
usernameClient,
|
usernameClient,
|
||||||
} from 'better-auth/client/plugins'
|
} from 'better-auth/client/plugins'
|
||||||
import { createAuthClient } from 'better-auth/react'
|
import { createAuthClient } from 'better-auth/react'
|
||||||
|
import Constants from 'expo-constants'
|
||||||
|
|
||||||
import { fetchWithLogger, TOKEN_KEY } from './fetch-logger'
|
import { fetchWithLogger, TOKEN_KEY } from './fetch-logger'
|
||||||
import type { Subscription } from './plugins/stripe'
|
import type { Subscription } from './plugins/stripe'
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
/c/Users/imeep/Desktop/shopify/expo-popcore-app
|
|
||||||
Reference in New Issue
Block a user