Start a Project

How to Create Authentication with Bagisto Headless eCommerce

In this guide, we’ll build a simple authentication system for a Next.js storefront using Bagisto Headless Commerce.

This authentication system allows users to:

The guide follows a clear, step-by-step structure with practical examples, and you can download the full working code from here.

What You Need

Step 0: Create Your Next.js Project

If you don’t have a Next.js project yet, let’s create one!

0.1 Install Node.js

First, make sure you have Node.js installed. Follow these steps:

  1. Go to nodejs.org
  2. Download the latest version (v18 or higher)
  3. Install it on your computer
  4. Open your terminal and type node --version to check

0.2 Create a New Next.js Project

Open your terminal. Then type these commands:

This creates a new Next.js project. The project is called bagisto-auth-tutorial.

0.3 Install TypeScript (Optional but Recommended)

If you want to use TypeScript, run this command. TypeScript helps catch errors:

0.4 Start Your Project

Now start your project with this command:

Your Next.js website should now be running. Open your browser and visit http://localhost:3000.

Step 1: Install Login Tools

Open your computer’s terminal. Then type these commands:

What this does:

These tools work together. They create a secure login system for your store.

Step 2: Set Up Your Keys

Create a file named .env.local in your project folder. Add this content:

Replace with your actual values:

Important: Never share these keys with anyone. Keep them private and secure.

Step 3: Create an Authentication System

3.1 Make the Login Route

Create a file at this location: src/app/api/auth/[...nextauth]/route.ts

Then add this code:

 

What this code does:

This code sets up NextAuth. It uses credentials (email and password) for login. Users stay logged in for 30 days.

3.2 Create Authentication Helper

Create a file at this location: src/utils/auth.ts

Then add this code:

What this code does:

This function talks to Bagisto. It sends the email and password. Then Bagisto checks if they’re correct. If yes, it returns user data. If no, it shows an error.

Step 4: Add GraphQL Proxy

Next.js needs a way to talk to Bagisto without CORS errors. Create this API route.

Create a file: src/app/api/graphql/route.ts

Step 5: Wrap Your App with Providers

Step 6: Create Login Form

Create a file at this location: src/components/customer/LoginForm.tsx

Then add this code:

What this code does:

This creates a login form. Users enter their email and password. Then they click “Login”. The form sends data to Bagisto.

If the login succeeds, users go to their account page.

Step 7: Create Registration Form

Create a file at this location: src/components/customer/RegistrationForm.tsx

Then add this code:

What this code does:

This creates a registration form. New users enter their details. They provide first name, last name, email, and password. Then they confirm their password.

After registration succeeds, they go to the login page.

Step 8: Create Account Page

Create a file at this location: src/app/account/page.tsx

Then add this code:

What this code does:

This creates the account page. Only logged-in users can see this page. They see a welcome message. They can view orders, edit profile, or logout.

When they logout, they go back to the home page.

Step 9: Test Your Login

How to Test:

Follow these steps to test your login system:

  1. Start your website – Run npm run dev in your terminal
  2. Go to /login – Open your browser and visit http://localhost:3000/login
  3. Try to login – Use an email and password from your Bagisto store
  4. Check if it works – You should see the account page

Troubleshooting List:

Here are solutions to common issues:

• Invalid email or password

→ Check your Bagisto URL and Storefront Key

• Page not found

→ Make sure you created all the required files

• Something went wrong

→ Check your terminal or console for error messages

Need more help? Look at the error messages in your browser console. They tell you exactly what’s wrong.

Final Results

You now have a working login system for your Bagisto store! Here’s what you built:

Login Page – Users can sign in

Registration Page – Users can create accounts

Account Page – Users see their account

Logout Button – Users can sign out

Security Checks – Forms are validated

30-Day Login – Users stay logged in for a month

Great job! You’ve successfully created a complete authentication system. Your users can now login, register, and manage their accounts.

Exit mobile version