Skip to content

Commit c174258

Browse files
committed
Use parsed data to login and signup
1 parent d613468 commit c174258

File tree

5 files changed

+31
-95
lines changed

5 files changed

+31
-95
lines changed

App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function Navigation() {
165165
}}
166166
>
167167
<Stack.Screen name='Splash' component={Splash} options={NO_ANIMATION} />
168-
<Stack.Screen name='Login' component={Login} options={SMOOTH_ANIMATION} />
168+
<Stack.Screen name='Login' component={Login} options={GestureEnabled} />
169169
<Stack.Screen name='Signup' component={Signup} options={GestureEnabled} />
170170
<Stack.Screen name='Verify' component={Verify} options={GestureEnabled} />
171171
<Stack.Screen name='Home' component={Home} options={NO_ANIMATION} />

src/rpc/hcWithType.d.ts

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ declare const client: {
3434
$post: {
3535
input: {
3636
form: {
37-
username: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
3837
password: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
38+
username: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
3939
deviceName: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
4040
deviceOs: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
4141
};
@@ -51,8 +51,8 @@ declare const client: {
5151
} | {
5252
input: {
5353
form: {
54-
username: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
5554
password: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
55+
username: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
5656
deviceName: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
5757
deviceOs: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
5858
};
@@ -78,10 +78,10 @@ declare const client: {
7878
$post: {
7979
input: {
8080
form: {
81+
password: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
8182
name: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
8283
username: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
8384
email: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
84-
password: import("hono/types").ParsedFormValue | import("hono/types").ParsedFormValue[];
8585
};
8686
};
8787
output: {
@@ -152,67 +152,6 @@ declare const client: {
152152
};
153153
}>;
154154
};
155-
} & {
156-
api: {
157-
redis: import("hono/client").ClientRequest<{
158-
$get: {
159-
input: {};
160-
output: {
161-
message: string;
162-
status: boolean;
163-
};
164-
outputFormat: "json";
165-
status: import("hono/utils/http-status").StatusCode;
166-
};
167-
}>;
168-
};
169-
} & {
170-
api: {
171-
redis: {
172-
":key": import("hono/client").ClientRequest<{
173-
$get: {
174-
input: {
175-
param: {
176-
key: string;
177-
};
178-
};
179-
output: {
180-
message: string;
181-
data: any;
182-
};
183-
outputFormat: "json";
184-
status: import("hono/utils/http-status").StatusCode;
185-
};
186-
}>;
187-
};
188-
};
189-
} & {
190-
api: {
191-
redis: {
192-
":key": {
193-
":value": import("hono/client").ClientRequest<{
194-
$get: {
195-
input: {
196-
param: {
197-
key: string;
198-
} & {
199-
value: string;
200-
};
201-
};
202-
output: {
203-
message: string;
204-
data: {
205-
key: string;
206-
value: string;
207-
};
208-
};
209-
outputFormat: "json";
210-
status: import("hono/utils/http-status").StatusCode;
211-
};
212-
}>;
213-
};
214-
};
215-
};
216155
} & {
217156
api: {
218157
profile: import("hono/client").ClientRequest<{

src/screens/auth/Login.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { useMutation } from '@tanstack/react-query'
1111
import { client, updateClientHeader } from '@utils/client'
1212
import { Bold, SemiBold } from '@utils/fonts'
1313
import type { NavProp } from '@utils/types'
14-
import React, { useEffect, useMemo, useState } from 'react'
15-
import { Alert, Platform, View } from 'react-native'
14+
import React, { useMemo, useState } from 'react'
15+
import { Alert, Platform, ToastAndroid, View } from 'react-native'
1616
import DeviceInfo from 'react-native-device-info'
1717
import { PasswordEye } from './components/PasswordEye'
1818

@@ -26,6 +26,13 @@ function getOs() {
2626
// + ', Tech Triangle v' + APP_VERSION
2727
}
2828

29+
type LoginParams = {
30+
username: string
31+
password: string
32+
deviceName: string
33+
deviceOs: string
34+
}
35+
2936
export default function Login({ navigation }: NavProp) {
3037
const { setToken } = authStore()
3138
const [username, setUsername] = useState('')
@@ -34,34 +41,17 @@ export default function Login({ navigation }: NavProp) {
3441
const deviceOs = useMemo(() => getOs(), [])
3542
const deviceName = useMemo(() => DeviceInfo.getModel(), [])
3643

37-
useEffect(() => {
38-
console.log(deviceName)
39-
console.log(deviceOs)
40-
})
41-
4244
const { mutate, isPending } = useMutation({
4345
mutationKey: ['login'],
44-
mutationFn: async () =>
45-
await (
46-
await client.api.auth.login.$post({
47-
form: { password, username, deviceName, deviceOs },
48-
})
49-
).json(),
46+
mutationFn: async (data: LoginParams) => await (await client.api.auth.login.$post({ form: { ...data } })).json(),
5047
onSuccess: (data) => {
51-
console.log(data)
52-
5348
if (data.verificationRequired) return navigation.replace('Verify', { username })
54-
5549
if (!data.status) return Alert.alert('Error', data.message)
56-
5750
if (data.data?.token) {
5851
// Navigate to home screen
5952
setToken(data.data.token)
6053
updateClientHeader(data.data.token)
61-
navigation.reset({
62-
index: 0,
63-
routes: [{ name: 'Home' }],
64-
})
54+
navigation.reset({ index: 0, routes: [{ name: 'Home' }] })
6555
return
6656
}
6757
},
@@ -72,12 +62,12 @@ export default function Login({ navigation }: NavProp) {
7262
})
7363

7464
function handelSubmit() {
75-
const { error } = loginZodValidator.safeParse({ username, password, deviceName, deviceOs })
65+
const { error, data } = loginZodValidator.safeParse({ username, password, deviceName, deviceOs })
7666
if (error) {
7767
Alert.alert('Error', error.errors[0]?.message || '')
7868
return
7969
}
80-
mutate()
70+
mutate(data)
8171
}
8272

8373
return (
@@ -135,7 +125,7 @@ export default function Login({ navigation }: NavProp) {
135125
/>
136126
<SettOption
137127
title='Forgot Password?'
138-
onPress={() => {}}
128+
onPress={() => ToastAndroid.show('Relax and try to remember your password.', ToastAndroid.SHORT)}
139129
arrow
140130
Icon={<RoundedIcon Icon={HelpCircleSolidIcon} className='bg-rose-500' />}
141131
/>

src/screens/auth/Signup.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ import React, { useEffect, useState } from 'react'
2424
import { ActivityIndicator, Alert, ToastAndroid, View } from 'react-native'
2525
import { PasswordEye } from './components/PasswordEye'
2626

27+
type SignupParams = {
28+
name: string
29+
username: string
30+
email: string
31+
password: string
32+
}
33+
2734
enum NameStatus {
2835
Available,
2936
Checking,
@@ -40,8 +47,7 @@ export default function Signup({ navigation }: NavProp) {
4047

4148
const { mutate, isPending } = useMutation({
4249
mutationKey: ['login'],
43-
mutationFn: async () =>
44-
await (await client.api.auth.signup.$post({ form: { password, username, email, name } })).json(),
50+
mutationFn: async (data: SignupParams) => await (await client.api.auth.signup.$post({ form: { ...data } })).json(),
4551
onSuccess: (data) => {
4652
console.log(data)
4753
if (!data.status) return Alert.alert('Error', data.message)
@@ -80,16 +86,16 @@ export default function Signup({ navigation }: NavProp) {
8086
checkUsername({ u: data.username })
8187
}, 500)
8288
return () => clearTimeout(timer)
83-
}, [checkUsername, email, username])
89+
}, [checkUsername, username])
8490

8591
function handelSubmit() {
86-
const { error } = signupZodValidator.safeParse({ username, password, email, name })
92+
const { error, data } = signupZodValidator.safeParse({ username, password, email, name })
8793
console.log(error)
8894
if (error) {
8995
Alert.alert('Error', error.errors[0]?.message || '')
9096
return
9197
}
92-
mutate()
98+
mutate(data)
9399
}
94100

95101
return (
@@ -161,7 +167,7 @@ export default function Signup({ navigation }: NavProp) {
161167
/>
162168
<SettOption
163169
title='Forgot Password?'
164-
onPress={() => {}}
170+
onPress={() => ToastAndroid.show('Relax and try to remember your password.', ToastAndroid.SHORT)}
165171
arrow
166172
Icon={<RoundedIcon Icon={HelpCircleSolidIcon} className='bg-rose-500' />}
167173
/>

src/zod/auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const otp = z
4040
const usernameOrEmail = z
4141
.string({ required_error: 'Username or email is required.' })
4242
.trim()
43+
.toLowerCase()
4344
.min(3, 'Username or email must contain at least 3 characters')
4445
.max(255, 'Username or email must contain at most 255 characters')
4546

0 commit comments

Comments
 (0)