Authentication lets users prove who they are to your app. It keeps their data safe and personal.
Authentication (email, Google, Apple) in React Native
import { GoogleSignin } from '@react-native-google-signin/google-signin'; import { appleAuth } from '@invertase/react-native-apple-authentication'; // Email sign-in example const signInWithEmail = async (email, password) => { // Use your backend or Firebase to authenticate }; // Google sign-in example const signInWithGoogle = async () => { await GoogleSignin.hasPlayServices(); const userInfo = await GoogleSignin.signIn(); return userInfo; }; // Apple sign-in example const signInWithApple = async () => { const appleAuthRequestResponse = await appleAuth.performRequest({ requestedOperation: appleAuth.Operation.LOGIN, requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME], }); return appleAuthRequestResponse; };
Use libraries like @react-native-google-signin/google-signin and @invertase/react-native-apple-authentication for easy integration.
Email sign-in usually requires a backend or Firebase to verify credentials.
const signInWithEmail = async (email, password) => { // Call backend API to check email and password const response = await fetch('https://example.com/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const data = await response.json(); return data; };
import { GoogleSignin } from '@react-native-google-signin/google-signin'; GoogleSignin.configure({ webClientId: 'your-web-client-id.apps.googleusercontent.com', }); const signInWithGoogle = async () => { await GoogleSignin.hasPlayServices(); const userInfo = await GoogleSignin.signIn(); return userInfo; };
import { appleAuth } from '@invertase/react-native-apple-authentication'; const signInWithApple = async () => { const response = await appleAuth.performRequest({ requestedOperation: appleAuth.Operation.LOGIN, requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME], }); return response; };
This React Native app shows three ways to sign in: email, Google, and Apple. It uses simple checks for email and password, and real libraries for Google and Apple sign-in. The app shows messages to tell if sign-in worked or failed.
import React, { useState } from 'react'; import { View, Button, Text, TextInput, StyleSheet } from 'react-native'; import { GoogleSignin } from '@react-native-google-signin/google-signin'; import { appleAuth } from '@invertase/react-native-apple-authentication'; GoogleSignin.configure({ webClientId: 'your-web-client-id.apps.googleusercontent.com', }); export default function AuthExample() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [message, setMessage] = useState(''); const signInWithEmail = () => { if(email === 'user@example.com' && password === '123456') { setMessage('Email sign-in successful!'); } else { setMessage('Email sign-in failed.'); } }; const signInWithGoogle = async () => { try { await GoogleSignin.hasPlayServices(); const userInfo = await GoogleSignin.signIn(); setMessage(`Google sign-in successful! Welcome ${userInfo.user.name}`); } catch (error) { setMessage('Google sign-in failed.'); } }; const signInWithApple = async () => { try { const response = await appleAuth.performRequest({ requestedOperation: appleAuth.Operation.LOGIN, requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME], }); setMessage('Apple sign-in successful!'); } catch (error) { setMessage('Apple sign-in failed.'); } }; return ( <View style={styles.container}> <TextInput style={styles.input} placeholder="Email" value={email} onChangeText={setEmail} keyboardType="email-address" autoCapitalize="none" accessibilityLabel="Email input" /> <TextInput style={styles.input} placeholder="Password" value={password} onChangeText={setPassword} secureTextEntry accessibilityLabel="Password input" /> <Button title="Sign in with Email" onPress={signInWithEmail} accessibilityLabel="Sign in with Email button" /> <View style={{ height: 10 }} /> <Button title="Sign in with Google" onPress={signInWithGoogle} accessibilityLabel="Sign in with Google button" /> <View style={{ height: 10 }} /> <Button title="Sign in with Apple" onPress={signInWithApple} accessibilityLabel="Sign in with Apple button" /> <Text style={styles.message} accessibilityLiveRegion="polite">{message}</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, backgroundColor: '#fff' }, input: { borderWidth: 1, borderColor: '#ccc', padding: 10, marginBottom: 10, borderRadius: 5 }, message: { marginTop: 20, fontSize: 16, color: 'green' }, });
Always test authentication on real devices because Google and Apple sign-in need device features.
Make sure to set up your app in Google and Apple developer consoles to get correct keys.
Use accessibility labels on inputs and buttons so screen readers can help users.
Authentication lets users securely access your app.
You can use email/password or social sign-ins like Google and Apple.
Use libraries to simplify integration and always handle success and failure states.
Practice
Solution
Step 1: Understand email/password authentication
Email/password authentication requires a secure backend or service like Firebase to verify credentials.Step 2: Identify the correct method in React Native
Firebase provides signInWithEmailAndPassword to handle this securely and easily.Final Answer:
Using Firebase Authentication's signInWithEmailAndPassword method -> Option AQuick Check:
Email/password auth = Firebase signInWithEmailAndPassword [OK]
- Storing passwords locally in AsyncStorage
- Confusing social sign-in with email/password
- Using Apple Sign-In for email login only
Solution
Step 1: Recall the official Google Sign-In package
The official package for React Native Google Sign-In is '@react-native-google-signin/google-signin'.Step 2: Check the correct import syntax
The correct import uses named import { GoogleSignin } from that package.Final Answer:
import { GoogleSignin } from '@react-native-google-signin/google-signin'; -> Option AQuick Check:
Google Sign-In import = import { GoogleSignin } from '@react-native-google-signin/google-signin'; [OK]
- Using default import instead of named import
- Wrong package names
- Confusing Google Sign-In with Apple Sign-In imports
async function onGoogleSignIn() {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
console.log(userInfo.user.email);
} catch (error) {
console.log('Error:', error.message);
}
}Solution
Step 1: Analyze the async function flow
The function awaits Google Play services check and then awaits signIn, which returns user info.Step 2: Understand console.log output
On success, userInfo.user.email is logged, so the user's email appears in the console.Final Answer:
Logs the signed-in user's email to the console -> Option DQuick Check:
Successful Google sign-in logs email [OK]
- Forgetting to await signIn causing unresolved promise
- Assuming error always occurs
- Confusing userInfo structure
import { appleAuth } from '@invertase/react-native-apple-authentication';
async function onAppleSignIn() {
try {
const appleAuthRequestResponse = await appleAuth.performRequest({
requestedOperation: appleAuth.Operation.LOGIN,
requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
});
console.log(appleAuthRequestResponse);
} catch (error) {
console.error(error);
}
}Solution
Step 1: Understand Apple Sign-In setup requirements
Apple Sign-In requires enabling the capability in Apple Developer portal for your app's bundle ID.Step 2: Identify the cause of configuration error
The error indicates the app is not configured properly, usually meaning the capability is missing or misconfigured.Final Answer:
Apple Sign-In is not enabled in the Apple Developer portal for the app -> Option BQuick Check:
Configuration error = missing Apple Sign-In capability [OK]
- Assuming code import is wrong
- Ignoring Apple Developer portal setup
- Forgetting to add requested scopes
Solution
Step 1: Understand user experience for multiple sign-in options
Users expect clear choices; separate buttons let them pick Email, Google, or Apple sign-in explicitly.Step 2: Manage success and failure states separately
Each method has different flows and error handling, so handling them individually is best practice.Final Answer:
Create separate buttons for each method and handle their success/failure states individually -> Option CQuick Check:
Separate buttons + individual handling = best practice [OK]
- Trying to call all sign-ins at once
- Ignoring Apple or Email sign-in options
- Storing passwords insecurely on device
Ready to prove it?
Pass the test and this pattern moves unknown
