Skip to content

Commit 5fc7584

Browse files
author
Guillermo Machado
committed
feat: sign in
1 parent b89999f commit 5fc7584

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
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/_layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ 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 }} />
3736
<Stack.Screen name="forgot-password" />
37+
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
3838
</Stack>
3939
</Providers>
4040
);

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',
@@ -46,13 +43,7 @@ export const LoginForm = ({ onSubmit = () => {} }: LoginFormProps) => {
4643
autoComplete="email"
4744
control={control}
4845
name="email"
49-
label="Email (optional)"
50-
/>
51-
<ControlledInput
52-
testID="username-input"
53-
control={control}
54-
name="username"
55-
label="Username"
46+
label="Email"
5647
/>
5748
<ControlledInput
5849
testID="password-input"

0 commit comments

Comments
 (0)