-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#34): add login functionality with email and password for secure…
… account access (#35) - adding login form - adding login screen - adding screen access from not found (debugging purposes) - adding login endpoint in api
- Loading branch information
1 parent
6da6685
commit d19a5b1
Showing
9 changed files
with
290 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import Router from '@koa/router'; | ||
import { registerDriver } from '../controllers/driver-controller'; | ||
import { loginDriver, registerDriver } from '../controllers/driver-controller'; | ||
|
||
const router = new Router(); | ||
|
||
router.post('/register', registerDriver); | ||
router.post('/login', loginDriver); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import KeyboardDismiss from "@/src/components/keyboard-dismiss"; | ||
import LoginForm from "@/src/components/login-form"; | ||
import { KeyboardAvoidingView, Platform, StyleSheet, View } from "react-native"; | ||
import { ScrollView } from "react-native-gesture-handler"; | ||
import { SafeAreaView } from "react-native-safe-area-context"; | ||
|
||
export default function Login() { | ||
return ( | ||
<SafeAreaView style={{ flex: 1 }}> | ||
<KeyboardAvoidingView | ||
behavior={Platform.OS === "ios" ? "padding" : "height"} | ||
style={{ flex: 1 }} | ||
> | ||
<ScrollView | ||
contentContainerStyle={{ | ||
flexGrow: 1, | ||
}} | ||
> | ||
<KeyboardDismiss> | ||
<View style={styles.container}> | ||
<LoginForm /> | ||
</View> | ||
</KeyboardDismiss> | ||
</ScrollView> | ||
</KeyboardAvoidingView> | ||
</SafeAreaView> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import InputTextField from '@/src/components/input-text-field'; | ||
import RoundedButton from '@/src/components/rounded-button'; | ||
import { useAppDispatch } from '@/src/hooks/use-app-dispatch'; | ||
import { useLoginDriverMutation } from '@/src/store/slices/api-slice'; | ||
import { setTokens } from '@/src/store/slices/auth-slice'; | ||
import React, { useState } from 'react'; | ||
import { Keyboard, Text, View } from 'react-native'; | ||
import { styles } from './styles'; | ||
|
||
const LoginForm: React.FC = () => { | ||
const [email, setEmail] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
|
||
const [emailError, setEmailError] = useState<string | undefined>(undefined); | ||
const [passwordError, setPasswordError] = useState<string | undefined>( | ||
undefined | ||
); | ||
const [loginError, setLoginError] = useState<string | null>(null); | ||
|
||
const dispatch = useAppDispatch(); | ||
const [login, { isLoading, isError, error, isSuccess }] = | ||
useLoginDriverMutation(); | ||
|
||
const isValidEmail = (email: string) => | ||
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); | ||
|
||
const handleLogin = async () => { | ||
let isValid = true; | ||
|
||
if (!isValidEmail(email)) { | ||
setEmailError('Please enter a valid email address'); | ||
isValid = false; | ||
} else { | ||
setEmailError(undefined); | ||
} | ||
|
||
if (password.length < 8) { | ||
setPasswordError('Password must be at least 8 characters'); | ||
isValid = false; | ||
} else { | ||
setPasswordError(undefined); | ||
} | ||
|
||
if (isValid) { | ||
setLoginError(null); | ||
try { | ||
const result = await login({ email, password }).unwrap(); | ||
dispatch( | ||
setTokens({ | ||
accessToken: result.accessToken, | ||
refreshToken: result.refreshToken, | ||
}) | ||
); | ||
} catch (err: any) { | ||
const errorMessage = | ||
err?.data?.error || 'Login failed. Please try again.'; | ||
setLoginError(errorMessage); | ||
console.error('Login failed:', errorMessage); | ||
} | ||
} | ||
}; | ||
|
||
const handleEmailChange = (text: string) => { | ||
setEmail(text); | ||
setEmailError( | ||
!isValidEmail(text) ? 'Please enter a valid email address' : undefined | ||
); | ||
}; | ||
|
||
const handlePasswordChange = (text: string) => { | ||
setPassword(text); | ||
setPasswordError( | ||
text.length < 8 ? 'Password must be at least 8 characters' : undefined | ||
); | ||
}; | ||
|
||
const handleSubmitEditing = () => { | ||
Keyboard.dismiss(); | ||
}; | ||
|
||
const isButtonDisabled = () => { | ||
return ( | ||
isLoading || Boolean(emailError || passwordError) || !email || !password | ||
); | ||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<Text style={styles.title}>Login</Text> | ||
|
||
<View style={styles.inputContainer}> | ||
<InputTextField | ||
label="Email" | ||
placeholder="[email protected]" | ||
fullWidth | ||
autoComplete="email" | ||
autoCorrect={false} | ||
autoCapitalize="none" | ||
onChangeText={handleEmailChange} | ||
errorText={emailError} | ||
testID="email-input" | ||
errorTestId="email-input-error" | ||
/> | ||
<InputTextField | ||
label="Password" | ||
placeholder="Enter your password" | ||
fullWidth | ||
autoCorrect={false} | ||
autoCapitalize="none" | ||
onSubmitEditing={handleSubmitEditing} | ||
securedEntry | ||
onChangeText={handlePasswordChange} | ||
errorText={passwordError} | ||
testID="password-input" | ||
errorTestId="password-input-error" | ||
/> | ||
</View> | ||
|
||
<View style={styles.buttonContainer}> | ||
<View style={styles.feedbackContainer}> | ||
{loginError && <Text style={styles.feedbackError}>{loginError}</Text>} | ||
{isSuccess && ( | ||
<Text style={styles.feedbackSuccess}>Login successful!</Text> | ||
)} | ||
</View> | ||
<RoundedButton | ||
disabled={isButtonDisabled()} | ||
text={isLoading ? 'Logging in...' : 'Login'} | ||
onPress={handleLogin} | ||
testID="login-button" | ||
/> | ||
</View> | ||
</View> | ||
); | ||
}; | ||
|
||
export default LoginForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { StyleSheet, TextStyle, ViewStyle } from "react-native"; | ||
|
||
interface ILoginFormStyles { | ||
container: ViewStyle; | ||
title: TextStyle; | ||
inputContainer: ViewStyle; | ||
buttonContainer: ViewStyle; | ||
feedbackContainer: ViewStyle; | ||
feedbackSuccess: TextStyle; | ||
feedbackError: TextStyle; | ||
} | ||
|
||
export const styles = StyleSheet.create<ILoginFormStyles>({ | ||
container: { | ||
paddingHorizontal: 20, | ||
paddingVertical: 24, | ||
justifyContent: "space-between", | ||
alignItems: "stretch", | ||
flex: 1, | ||
}, | ||
title: { | ||
fontFamily: "Poppins-Bold", | ||
fontSize: 30, | ||
lineHeight: 30 * 1.3, | ||
color: "#000000", | ||
marginBottom: 14, | ||
textAlign: "center", | ||
}, | ||
inputContainer: { | ||
gap: 22, | ||
}, | ||
buttonContainer: { | ||
marginTop: 14, | ||
}, | ||
feedbackContainer: { | ||
minHeight: 34, | ||
}, | ||
feedbackSuccess: { | ||
color: "green", | ||
marginVertical: 8, | ||
}, | ||
feedbackError: { | ||
color: "red", | ||
marginVertical: 8, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters