#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Authentication

Setting Up

Before a React app can sign users up or log them in, it needs a small foundation: a form that collects an email and password, a way to switch between login and signup modes, and a single place that stores the address of the authentication server. Setting this up first keeps the later steps, sending the request and storing the token, simple and focused.

Planning what the auth screen needs

A typical authentication page holds only a little state:

  • the email and password the user types,
  • a flag that says whether the form is in login or signup mode,
  • a loading flag while a request is in progress,
  • an error message to show if something goes wrong.

Using one component for both modes avoids two almost identical forms. Only the button label, the heading and the endpoint change between them.

Building the form component

import { useState } from 'react';

export default function AuthForm() {
  const [isLogin, setIsLogin] = useState(true);
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  function toggleMode() {
    setIsLogin((current) => !current);
  }

  function handleSubmit(event) {
    event.preventDefault();
    // request logic is added in the next step
  }

  return (
    <form onSubmit={handleSubmit}>
      <h2>{isLogin ? 'Log in' : 'Create account'}</h2>
      <label htmlFor="email">Email</label>
      <input id="email" type="email" required
        value={email} onChange={(e) => setEmail(e.target.value)} />
      <label htmlFor="password">Password</label>
      <input id="password" type="password" required minLength={8}
        value={password} onChange={(e) => setPassword(e.target.value)} />
      <button type="submit">{isLogin ? 'Log in' : 'Sign up'}</button>
      <button type="button" onClick={toggleMode}>
        {isLogin ? 'Need an account? Sign up' : 'Have an account? Log in'}
      </button>
    </form>
  );
}

The inputs are controlled: their values live in state, so the submit handler can read them directly. Notice the toggle button has type="button"; without it, a button inside a form defaults to submit and would send the form instead of switching modes.

Keeping the server address in configuration

Hard-coding a URL such as http://localhost:5000 inside components makes deployment painful. Put it in an environment file instead. With Vite, variables must start with VITE_:

# .env.local
VITE_API_URL=http://localhost:5000/api

Read it in code with import.meta.env.VITE_API_URL. Projects created with Create React App use the REACT_APP_ prefix and process.env instead. Restart the dev server after editing the file, because the values are read at start-up.

A pitfall: secrets in the front end

Anything bundled into a React app can be read by anyone who opens the browser's developer tools. Environment variables in a front-end build are not secret; they are simply text substituted into your JavaScript. A public URL or a public client key is fine there. A database password, a signing secret for tokens or an admin API key is not. Those belong on the server, which verifies passwords and issues tokens. Also add .env.local to .gitignore so local settings are not committed by accident.

Where to place the pieces

  • src/components/AuthForm.jsx for the form itself.
  • src/pages/AuthPage.jsx if you use routing, rendering the form at a path like /auth.
  • src/api/auth.js later, for the functions that call the server.

Ready for the next step

At this point you have a working two-mode form with controlled inputs and a configurable server address, but no network calls yet. That separation is intentional: the UI is easy to test on its own, and the signup request can now be added in one place without touching the layout. Before moving on, type into both fields, flip between the two modes a few times and check that the heading and button labels change while the typed values stay put. If they do, the state wiring is correct and you can build on it with confidence.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course