Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/backend/go_modules-sec…
Browse files Browse the repository at this point in the history
…urity-group-cf2ebfe372
  • Loading branch information
DOOduneye authored Mar 16, 2024
2 parents 40a8e04 + 8bcb193 commit 4c7074e
Show file tree
Hide file tree
Showing 20 changed files with 1,165 additions and 648 deletions.
4 changes: 2 additions & 2 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
4 changes: 1 addition & 3 deletions frontend/sac-mobile/app/(app)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const Home = () => {
const { logout } = useAuthStore();
return (
<View className="items-center justify-center flex-1">
<Button onPress={logout} size={'sm'}>
Logout
</Button>
<Button onPress={logout}>Logout</Button>
<Text>Home</Text>
</View>
);
Expand Down
118 changes: 118 additions & 0 deletions frontend/sac-mobile/app/(auth)/_components/login-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Controller, useForm } from 'react-hook-form';
import { Alert, Text, View } from 'react-native';

import { router } from 'expo-router';

import { ZodError, z } from 'zod';

import { Button } from '@/components/button';
import Error from '@/components/error';
import Input from '@/components/input';
import { useAuthStore } from '@/hooks/use-auth';
import { loginByEmail } from '@/services/auth';

type LoginFormData = {
email: string;
password: string;
};

const loginSchema = z.object({
email: z.string().email({ message: 'Invalid email' }),
password: z
.string()
.min(8, { message: 'Password must be at least 8 characters long' })
});

const LoginForm = () => {
const {
control,
handleSubmit,
formState: { errors }
} = useForm<LoginFormData>();
const { login } = useAuthStore();

const onSubmit = async (data: LoginFormData) => {
try {
loginSchema.parse(data);
const { user, tokens } = await loginByEmail(
data.email.toLowerCase(),
data.password
);
login(tokens, user);
router.push('/(app)/');
} catch (e: unknown) {
if (e instanceof ZodError) {
Alert.alert('Validation Error', e.errors[0].message); // use a better way to display errors
} else {
console.error('An unexpected error occurred:', e);
}
}
};

return (
<>
<View>
<Controller
control={control}
render={({ field: { onChange, value } }) => (
<Input
title="Email"
autoCorrect={false}
placeholder="[email protected]"
onChangeText={onChange}
value={value}
onSubmitEditing={handleSubmit(onSubmit)}
error={!!errors.email}
/>
)}
name="email"
rules={{ required: 'Email is required' }}
/>
{errors.email && <Error message={errors.email.message} />}
</View>
<View className="w-full mt-[8%] mb-[3%]">
<Controller
control={control}
render={({ field: { onChange, value } }) => (
<Input
title="Password"
autoCorrect={false}
placeholder="Password"
onChangeText={onChange}
value={value}
secureTextEntry={true}
onSubmitEditing={handleSubmit(onSubmit)}
error={!!errors.password}
/>
)}
name="password"
rules={{ required: 'Password is required' }}
/>
{errors.password && <Error message={errors.password.message} />}
</View>

<View className="pb-[8%] flex-row justify-end">
<Text>Forgot password?</Text>
</View>

<View className="flex-row justify-between">
<Button
size="lg"
variant="outline"
onPress={() => router.push('/(auth)/register')}
>
Sign up
</Button>
<Button
size="lg"
variant="default"
onPress={handleSubmit(onSubmit)}
>
Log in
</Button>
</View>
</>
);
};

export default LoginForm;
Loading

0 comments on commit 4c7074e

Please sign in to comment.