Skip to content

Commit 38596f6

Browse files
committed
docs(demo): Handle login errors
1 parent 30f4352 commit 38596f6

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

examples/github-app/src/navigation/LoginForm.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1+
import { useLoading } from '@data-client/react';
12
import { Button, Checkbox, Form, Input } from 'antd';
23

34
export default function LoginForm({
45
onFinish,
56
}: {
6-
onFinish: (values: any) => void;
7+
onFinish: (values: any) => Promise<void>;
78
}) {
8-
const onFinishFailed = (errorInfo: any) => {
9-
console.log('Failed:', errorInfo);
10-
};
9+
const [handleFinish, loading, error] = useLoading(onFinish, [onFinish]);
1110

1211
return (
1312
<Form
1413
name="basic"
1514
labelCol={{ span: 6 }}
1615
wrapperCol={{ span: 16 }}
1716
initialValues={{ remember: true }}
18-
onFinish={onFinish}
19-
onFinishFailed={onFinishFailed}
20-
autoComplete="off"
17+
onFinish={handleFinish}
2118
>
2219
<Form.Item
2320
label="Username"
2421
name="login"
2522
rules={[{ required: true, message: 'Please input your username!' }]}
23+
validateStatus={error ? 'error' : ''}
24+
help={
25+
(error as any)?.status === 401
26+
? 'Username and token were not valid'
27+
: ''
28+
}
2629
>
2730
<Input autoFocus />
2831
</Form.Item>
@@ -44,7 +47,7 @@ export default function LoginForm({
4447
</Form.Item>
4548

4649
<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
47-
<Button type="primary" htmlType="submit">
50+
<Button type="primary" htmlType="submit" disabled={loading}>
4851
Submit
4952
</Button>
5053
</Form.Item>

examples/github-app/src/navigation/LoginModal.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ export default function LoginModal({
1818
const handleCancel = useCallback(() => {
1919
handleClose();
2020
}, []);
21-
const onFinish = useCallback((data: { login: string; token: string }) => {
22-
login(data);
23-
handleClose();
24-
}, []);
21+
const onFinish = useCallback(
22+
async (data: { login: string; token: string }) => {
23+
await login(data);
24+
handleClose();
25+
},
26+
[handleClose, login],
27+
);
2528

2629
return (
2730
<Modal

examples/github-app/src/navigation/authdContext.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { useController } from '@data-client/react';
22
import React, { createContext, useCallback, useMemo } from 'react';
33

44
import { setAuth, unAuth } from '@/resources/Auth';
5-
import UserResource from '@/resources/User';
5+
import UserResource, { User } from '@/resources/User';
66

77
export const authdContext = createContext({
8-
login(data: { login: string; token: string }): void {
8+
async login(data: { login: string; token: string }): Promise<User> {
99
throw new Error('context not set for auth');
1010
},
1111
logout(): void {
@@ -21,9 +21,9 @@ export function AuthdProvider({ children }: { children: React.ReactNode }) {
2121
ctrl.invalidate(UserResource.current);
2222
}, [ctrl]);
2323
const login = useCallback(
24-
(data: { login: string; token: string }) => {
25-
ctrl.fetch(UserResource.current);
24+
async (data: { login: string; token: string }) => {
2625
setAuth(data);
26+
return await ctrl.fetch(UserResource.current);
2727
},
2828
[ctrl],
2929
);

0 commit comments

Comments
 (0)