Bird
Raised Fist0
React Nativemobile~20 mins

Authentication (email, Google, Apple) in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: LoginScreen
This screen lets users sign in using email/password or with Google or Apple accounts.
Target UI
-------------------------
|       Login Screen     |
|-----------------------|
| Email: [___________]  |
| Password: [_______]   |
| [Login Button]        |
|                       |
| --- OR ---            |
| [Sign in with Google] |
| [Sign in with Apple]  |
-------------------------
Text inputs for email and password with labels
A Login button that prints entered email and password to console
A button to sign in with Google that prints 'Google Sign-In pressed' to console
A button to sign in with Apple that prints 'Apple Sign-In pressed' to console
Use React Native functional components and hooks
Basic styling for layout and spacing
Accessible labels for inputs and buttons
Starter Code
React Native
import React, { useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet } from 'react-native';

export default function LoginScreen() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  // TODO: Add handlers for login, Google sign-in, and Apple sign-in

  return (
    <View style={styles.container}>
      <Text style={styles.label}>Email:</Text>
      <TextInput
        style={styles.input}
        keyboardType="email-address"
        autoCapitalize="none"
        value={email}
        onChangeText={setEmail}
        accessibilityLabel="Email input"
      />

      <Text style={styles.label}>Password:</Text>
      <TextInput
        style={styles.input}
        secureTextEntry
        value={password}
        onChangeText={setPassword}
        accessibilityLabel="Password input"
      />

      {/* TODO: Add Login button */}

      {/* TODO: Add separator text "--- OR ---" */}

      {/* TODO: Add Google sign-in button */}

      {/* TODO: Add Apple sign-in button */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: 'center',
    backgroundColor: '#fff'
  },
  label: {
    fontSize: 16,
    marginBottom: 4
  },
  input: {
    borderWidth: 1,
    borderColor: '#ccc',
    padding: 8,
    marginBottom: 12,
    borderRadius: 4
  },
  separator: {
    textAlign: 'center',
    marginVertical: 12,
    color: '#666'
  },
  button: {
    marginVertical: 6
  }
});
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
React Native
import React, { useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet } from 'react-native';

export default function LoginScreen() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const handleLogin = () => {
    console.log(`Login pressed with email: ${email} and password: ${password}`);
  };

  const handleGoogleSignIn = () => {
    console.log('Google Sign-In pressed');
  };

  const handleAppleSignIn = () => {
    console.log('Apple Sign-In pressed');
  };

  return (
    <View style={styles.container}>
      <Text style={styles.label}>Email:</Text>
      <TextInput
        style={styles.input}
        keyboardType="email-address"
        autoCapitalize="none"
        value={email}
        onChangeText={setEmail}
        accessibilityLabel="Email input"
      />

      <Text style={styles.label}>Password:</Text>
      <TextInput
        style={styles.input}
        secureTextEntry
        value={password}
        onChangeText={setPassword}
        accessibilityLabel="Password input"
      />

      <View style={styles.button}>
        <Button title="Login" onPress={handleLogin} accessibilityLabel="Login button" />
      </View>

      <Text style={styles.separator}>--- OR ---</Text>

      <View style={styles.button}>
        <Button title="Sign in with Google" onPress={handleGoogleSignIn} accessibilityLabel="Sign in with Google button" />
      </View>

      <View style={styles.button}>
        <Button title="Sign in with Apple" onPress={handleAppleSignIn} accessibilityLabel="Sign in with Apple button" />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: 'center',
    backgroundColor: '#fff'
  },
  label: {
    fontSize: 16,
    marginBottom: 4
  },
  input: {
    borderWidth: 1,
    borderColor: '#ccc',
    padding: 8,
    marginBottom: 12,
    borderRadius: 4
  },
  separator: {
    textAlign: 'center',
    marginVertical: 12,
    color: '#666'
  },
  button: {
    marginVertical: 6
  }
});

This solution uses React Native functional components with hooks to manage email and password state.

We added three button handlers: one for login that prints the entered email and password, and two others for Google and Apple sign-in that print messages to the console.

The UI includes labeled TextInput fields for email and password with accessibility labels for screen readers.

Buttons are spaced with margin for clarity, and a separator text "--- OR ---" visually divides the login methods.

This simple setup helps beginners understand how to handle user input and button presses in React Native.

Final Result
Completed Screen
-------------------------
|       Login Screen     |
|-----------------------|
| Email: [user@example] |
| Password: [•••••••]   |
| [Login Button]        |
|                       |
| --- OR ---            |
| [Sign in with Google] |
| [Sign in with Apple]  |
-------------------------
Typing in email and password updates the input fields
Pressing Login button logs email and password to console
Pressing 'Sign in with Google' logs 'Google Sign-In pressed' to console
Pressing 'Sign in with Apple' logs 'Apple Sign-In pressed' to console
Stretch Goal
Add a loading spinner that shows when Login button is pressed and hides after 2 seconds
💡 Hint
Use useState to track loading state and setTimeout to simulate delay

Practice

(1/5)
1. Which method is commonly used to authenticate users with their email and password in React Native apps?
easy
A. Using Firebase Authentication's signInWithEmailAndPassword method
B. Directly storing passwords in AsyncStorage
C. Using Google Sign-In without email
D. Using Apple Sign-In only

Solution

  1. Step 1: Understand email/password authentication

    Email/password authentication requires a secure backend or service like Firebase to verify credentials.
  2. Step 2: Identify the correct method in React Native

    Firebase provides signInWithEmailAndPassword to handle this securely and easily.
  3. Final Answer:

    Using Firebase Authentication's signInWithEmailAndPassword method -> Option A
  4. Quick Check:

    Email/password auth = Firebase signInWithEmailAndPassword [OK]
Hint: Email login usually uses Firebase's signInWithEmailAndPassword [OK]
Common Mistakes:
  • Storing passwords locally in AsyncStorage
  • Confusing social sign-in with email/password
  • Using Apple Sign-In for email login only
2. Which of the following is the correct syntax to import Google Sign-In in a React Native app?
easy
A. import { GoogleSignin } from '@react-native-google-signin/google-signin';
B. import GoogleSignIn from 'react-native-google-signin';
C. import Google from 'google-signin-react-native';
D. import { GoogleSignIn } from 'react-native-google-auth';

Solution

  1. 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'.
  2. Step 2: Check the correct import syntax

    The correct import uses named import { GoogleSignin } from that package.
  3. Final Answer:

    import { GoogleSignin } from '@react-native-google-signin/google-signin'; -> Option A
  4. Quick Check:

    Google Sign-In import = import { GoogleSignin } from '@react-native-google-signin/google-signin'; [OK]
Hint: Official Google Sign-In uses named import from '@react-native-google-signin/google-signin' [OK]
Common Mistakes:
  • Using default import instead of named import
  • Wrong package names
  • Confusing Google Sign-In with Apple Sign-In imports
3. What will be the output of this React Native code snippet when a user successfully signs in with Google?
async function onGoogleSignIn() {
  try {
    await GoogleSignin.hasPlayServices();
    const userInfo = await GoogleSignin.signIn();
    console.log(userInfo.user.email);
  } catch (error) {
    console.log('Error:', error.message);
  }
}
medium
A. Throws a syntax error due to missing await
B. Does nothing because signIn is not awaited
C. Logs 'Error: user cancelled the login flow' always
D. Logs the signed-in user's email to the console

Solution

  1. Step 1: Analyze the async function flow

    The function awaits Google Play services check and then awaits signIn, which returns user info.
  2. Step 2: Understand console.log output

    On success, userInfo.user.email is logged, so the user's email appears in the console.
  3. Final Answer:

    Logs the signed-in user's email to the console -> Option D
  4. Quick Check:

    Successful Google sign-in logs email [OK]
Hint: Await signIn returns user info; console logs email [OK]
Common Mistakes:
  • Forgetting to await signIn causing unresolved promise
  • Assuming error always occurs
  • Confusing userInfo structure
4. You wrote this code to sign in with Apple but get an error: "Apple Authentication is not configured properly." What is the likely cause?
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);
  }
}
medium
A. Missing await keyword before performRequest
B. Apple Sign-In is not enabled in the Apple Developer portal for the app
C. requestedScopes array is empty
D. The import statement is incorrect

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Apple Sign-In is not enabled in the Apple Developer portal for the app -> Option B
  4. Quick Check:

    Configuration error = missing Apple Sign-In capability [OK]
Hint: Check Apple Developer portal for Sign-In capability [OK]
Common Mistakes:
  • Assuming code import is wrong
  • Ignoring Apple Developer portal setup
  • Forgetting to add requested scopes
5. You want to implement a unified sign-in button that lets users choose between Email, Google, or Apple sign-in in React Native. Which approach best handles this scenario?
hard
A. Use a single button that calls all sign-in methods simultaneously
B. Use only Google Sign-In and ignore others for simplicity
C. Create separate buttons for each method and handle their success/failure states individually
D. Store user passwords locally to switch between methods

Solution

  1. 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.
  2. Step 2: Manage success and failure states separately

    Each method has different flows and error handling, so handling them individually is best practice.
  3. Final Answer:

    Create separate buttons for each method and handle their success/failure states individually -> Option C
  4. Quick Check:

    Separate buttons + individual handling = best practice [OK]
Hint: Separate buttons for each sign-in method handle flows cleanly [OK]
Common Mistakes:
  • Trying to call all sign-ins at once
  • Ignoring Apple or Email sign-in options
  • Storing passwords insecurely on device
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard