Skip to content

Commit 592e274

Browse files
author
Guillermo Machado
committed
feat: sign in
1 parent eb28cb9 commit 592e274

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

src/api/auth/use-login.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { createMutation } from 'react-query-kit';
33
import { client } from '../common';
44

55
type Variables = {
6-
email?: string; // optional because API doesn't require email
7-
username: string;
6+
email: string;
87
password: string;
98
expiresInMins?: number;
109
};
@@ -19,11 +18,13 @@ type Response = {
1918

2019
const login = async (variables: Variables) => {
2120
const { data } = await client({
22-
url: 'auth/login',
21+
url: '/v1/users/sign_in',
2322
method: 'POST',
2423
data: {
25-
username: variables.username,
26-
password: variables.password,
24+
user: {
25+
email: variables.email,
26+
password: variables.password,
27+
},
2728
},
2829
headers: {
2930
'Content-Type': 'application/json',

src/app/(app)/_layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function TabLayout() {
3030
return <Redirect href="/onboarding" />;
3131
}
3232
if (status === 'signOut') {
33-
return <Redirect href="/login" />;
33+
return <Redirect href="/sign-in" />;
3434
}
3535
return (
3636
<Tabs>

src/app/_layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function RootLayout() {
3333
<Stack>
3434
<Stack.Screen name="(app)" options={{ headerShown: false }} />
3535
<Stack.Screen name="onboarding" options={{ headerShown: false }} />
36-
<Stack.Screen name="login" options={{ headerShown: false }} />
36+
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
3737
</Stack>
3838
</Providers>
3939
);

src/app/onboarding.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function Onboarding() {
3838
label="Let's Get Started "
3939
onPress={() => {
4040
setIsFirstTime(false);
41-
router.replace('/login');
41+
router.replace('/sign-in');
4242
}}
4343
/>
4444
</SafeAreaView>

src/app/sign-in.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useRouter } from 'expo-router';
2+
import { showMessage } from 'react-native-flash-message';
3+
4+
import { useLogin } from '@/api/auth/use-login';
5+
import type { LoginFormProps } from '@/components/login-form';
6+
import { LoginForm } from '@/components/login-form';
7+
import { useAuth } from '@/core';
8+
import { FocusAwareStatusBar } from '@/ui';
9+
10+
export default function Login() {
11+
const router = useRouter();
12+
const signIn = useAuth.use.signIn();
13+
const { mutate: login } = useLogin({
14+
onSuccess: (data) => {
15+
signIn({ access: data.accessToken, refresh: data.refreshToken });
16+
router.push('/');
17+
},
18+
onError: (error) => showMessage({ message: error.message, type: 'danger' }),
19+
});
20+
21+
const onSubmit: LoginFormProps['onSubmit'] = (data) => {
22+
login(data);
23+
};
24+
return (
25+
<>
26+
<FocusAwareStatusBar />
27+
<LoginForm onSubmit={onSubmit} />
28+
</>
29+
);
30+
}

src/components/login-form.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import { Button, ControlledInput, Text, View } from '@/ui';
88

99
const MIN_CHARS = 6;
1010
const schema = z.object({
11-
username: z.string({
12-
required_error: 'Username is required',
13-
}),
14-
email: z.string().email('Invalid email format').optional(),
11+
email: z.string().email('Invalid email format'),
1512
password: z
1613
.string({
1714
required_error: 'Password is required',
@@ -44,13 +41,7 @@ export const LoginForm = ({ onSubmit = () => {} }: LoginFormProps) => {
4441
testID="email-input"
4542
control={control}
4643
name="email"
47-
label="Email (optional)"
48-
/>
49-
<ControlledInput
50-
testID="username-input"
51-
control={control}
52-
name="username"
53-
label="Username"
44+
label="Email"
5445
/>
5546
<ControlledInput
5647
testID="password-input"

0 commit comments

Comments
 (0)