fix: 成功对接登录接口
This commit is contained in:
@@ -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 };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user