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