Building a React Login Page Template

Madison Evans
contact@eartho.io

Building a Secure Login Page for Your React Application: Manual vs Eartho

Building dynamic web applications often requires secure user authentication. This guide dives into creating a secure login page for your React application, encompassing authentication, input validation, and styling. We'll explore two approaches: manual implementation and Eartho integration.

Manual Implementation

While React excels at crafting user interfaces, building authentication from scratch can be complex. This section walks you through creating a custom login page:

  • Authentication Server: Set up an Express-based server utilizing JSON Web Tokens (JWT) for user authentication. JWTs are self-contained tokens carrying user information, offering a secure way to manage user sessions.
  • Input Validation: Implement robust input validation to ensure users enter data in the expected format. This includes validating email structure, password length, and potentially using reCaptcha to prevent bots.
  • Styling: Create a user-friendly login interface using CSS or a styling library like Material-UI or Bootstrap. This enhances the user experience and reflects your application's overall design.
  • Connecting Frontend and Backend: Establish communication between your React frontend and the Express backend server to handle login requests, validate credentials, and generate JWTs.

Eartho Integration

Eartho offers a streamlined solution for adding authentication to React applications. Here's how Eartho simplifies the process:

  • Reduced Development Time: Eartho provides pre-built components and functionalities, eliminating the need to build and maintain a custom authentication backend. This allows you to focus on developing your application's core features.
  • Simplified Integration: Integrating Eartho involves installing their SDK and wrapping your React app with the EarthoProvider component. Eartho's components handle user registration, login, password reset, and more.
  • Scalability and Security: Eartho takes care of user management infrastructure, ensuring scalability and adherence to security best practices. They handle password hashing, secure token storage, and user data protection.

Choosing the Right Approach

The choice between manual implementation and Eartho depends on your project's specific needs and resources:

  • Manual Approach: Ideal for projects requiring complete control over the authentication flow or for learning backend development intricacies. However, it requires more development time and expertise.
  • Eartho Integration: Perfect for projects where development speed and ease of use are priorities. Eartho is a great option if you don't want to manage the complexities of backend authentication.

Conclusion

React provides a powerful foundation for building dynamic web applications. Integrating secure authentication is crucial for user management and data protection. This guide has explored both manual implementation and Eartho for adding login functionality to your React app. By understanding the strengths and considerations of each approach, you can make an informed decision to enhance your application's security and user experience.

Implementing Eartho Login

This tutorial guides you through integrating Eartho One, a user management solution, to implement a secure login page in your React application. Eartho One streamlines the process, eliminating the need to build and maintain your own authentication backend.

Prerequisites:

  • A React project set up (use create-react-app for a quick start)
  • An Eartho One account (sign up for a free tier or choose a paid plan)

Steps:

Install Eartho One SDK:

Open your terminal and navigate to your React project directory. Install the Eartho One SDK using npm:

npm install @eartho/one-client-react


Obtain Eartho API Keys:

In your Eartho One dashboard, navigate to the "Settings" section and locate your API keys. You'll need the Client ID and Client Secret for integration.


Configure Eartho Provider:

Wrap your React application's root component with the EarthoProvider component from the Eartho One SDK. This provides authentication context throughout your app.

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { EarthoProvider } from '@eartho/one-client-react';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
 <React.StrictMode>
   <EarthoProvider
     clientId="YOUR_CLIENT_ID"
     clientSecret="YOUR_CLIENT_SECRET"
     // Optional: Additional Eartho configuration options
   >
     <App />
   </EarthoProvider>
 </React.StrictMode>
);


Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual Eartho API keys.


Utilize Eartho Login Component:

Eartho One provides a pre-built Login component for user login. Import this component from the SDK and use it within your React application. Here's an example:

// src/App.js
import React from 'react';
import { useEarthoOne } from '@eartho/one-client-react';

function App() {
 const {
   isLoading,
   isConnected,
   error,
   user,
   connectWithPopup,
   logout,
 } = useEarthoOne();

 // Handle loading, error, and logged-in states
 if (isLoading) {
   return <div>Loading...</div>;
 }
 if (error) {
   return <div>Oops... {error.message}</div>;
 }

 // Logged-in user view
 if (isConnected) {
   return (
     <div>
       Hello {user.displayName} {' '}
       <button onClick={() => logout({ returnTo: window.location.origin })}>
         Log out
       </button>
     </div>
   );
 }

 // Login button for non-logged-in users
 return (
   <button
     className="btn btn-outline-success"
     id="login"
     onClick={() => connectWithPopup({ accessId: "YOUR_EARTHO_ACCESS_ID" })}
   >
     Log in
   </button>
 );
}

export default App;

Explanation:

  1. We import useEarthoOne from the @eartho/one-client-react package. This hook provides access to Eartho's authentication state and functions.
  2. Inside the App component, we destructure the properties returned by useEarthoOne:
    • isLoading: Indicates if Eartho is still loading user data.
    • isConnected: Indicates if the user is currently logged in.
    • error: Holds any errors encountered during authentication.
    • user: Contains user information if logged in (e.g., display name).
    • connectWithPopup: Function to initiate login using a popup window.
    • logout: Function to log the user out.
  3. We conditionally render content based on the authentication state:
    • Loading: Display a loading message while Eartho retrieves user data.
    • Error: Display an error message if any issues occur during login.
    • Logged-in: Greet the user by name and provide a logout button.
    • Not logged in: Display a login button that triggers connectWithPopup.

Additional Notes:

  • Replace "YOUR_EARTHO_ACCESS_ID" with your actual Eartho access ID obtained from the dashboard.
  • Eartho offers more components and functionalities beyond login. Refer to their documentation (https://docs.eartho.io/) for details.

By following these steps, you can integrate Eartho's pre-built login functionality into your React application, streamlining the user authentication process. This approach reduces development time and ensures adherence to security best practices.