-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.tsx
More file actions
99 lines (88 loc) · 3.32 KB
/
index.tsx
File metadata and controls
99 lines (88 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useRouter } from 'expo-router';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { LoginFormProps } from '@/app/login/login-form';
import { ServerUrlBottomSheet } from '@/components/settings/server-url-bottom-sheet';
import { FocusAwareStatusBar } from '@/components/ui';
import { Button, ButtonText } from '@/components/ui/button';
import { Modal, ModalBackdrop, ModalBody, ModalContent, ModalFooter, ModalHeader } from '@/components/ui/modal';
import { Text } from '@/components/ui/text';
import { useAnalytics } from '@/hooks/use-analytics';
import { useAuth } from '@/lib/auth';
import { logger } from '@/lib/logging';
import { LoginForm } from './login-form';
export default function Login() {
const [isErrorModalVisible, setIsErrorModalVisible] = useState(false);
const [showServerUrl, setShowServerUrl] = useState(false);
const { t } = useTranslation();
const { trackEvent } = useAnalytics();
const router = useRouter();
const { login, status, error, isAuthenticated } = useAuth();
// Track when login view is rendered
useEffect(() => {
trackEvent('login_view_rendered', {
hasError: !!error,
status: status,
});
}, [trackEvent, error, status]);
useEffect(() => {
if (status === 'signedIn' && isAuthenticated) {
logger.info({ message: 'Login successful, redirecting to home' });
router.push('/(app)');
}
}, [status, isAuthenticated, router]);
useEffect(() => {
if (status === 'error') {
logger.error({ message: 'Login failed', context: { error } });
setIsErrorModalVisible(true);
}
}, [status, error]);
// ── Local login ───────────────────────────────────────────────────────────
const onSubmit: LoginFormProps['onSubmit'] = async (data) => {
logger.info({ message: 'Starting Login (button press)' });
await login({ username: data.username, password: data.password });
};
return (
<>
<FocusAwareStatusBar />
<LoginForm
onSubmit={onSubmit}
isLoading={status === 'loading'}
error={error ?? undefined}
onServerUrlPress={() => setShowServerUrl(true)}
onSsoPress={() => router.push('/login/sso')}
/>
<Modal
isOpen={isErrorModalVisible}
onClose={() => {
setIsErrorModalVisible(false);
}}
size="full"
{...({} as any)}
>
<ModalBackdrop />
<ModalContent className="m-4 w-full max-w-3xl rounded-2xl">
<ModalHeader>
<Text className="text-xl font-semibold">{t('login.errorModal.title')}</Text>
</ModalHeader>
<ModalBody>
<Text>{t('login.errorModal.message')}</Text>
</ModalBody>
<ModalFooter>
<Button
variant="solid"
size="sm"
action="primary"
onPress={() => {
setIsErrorModalVisible(false);
}}
>
<ButtonText>{t('login.errorModal.confirmButton')}</ButtonText>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
<ServerUrlBottomSheet isOpen={showServerUrl} onClose={() => setShowServerUrl(false)} />
</>
);
}