Developer Quick-Start: Learn how to receive and process inbound SMS messages in Next.js using Sinch webhooks with NextAuth authentication. Complete guide with HMAC-SHA256 verification, E.164 phone validation, and production deployment. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
Learn how to build a Next.js application that receives and processes inbound SMS messages using Sinch webhooks with NextAuth authentication. This comprehensive tutorial covers webhook security with HMAC-SHA256 verification, E.164 phone number validation, user authentication, and production deployment.
You'll create a secure web application where authenticated users receive SMS messages sent to a dedicated Sinch phone number, automatically linked to their account via their registered phone number.
What you'll build:
- NextAuth.js authentication system with email/password and phone numbers
- Secure Sinch webhook endpoint with HMAC-SHA256 signature verification
- E.164 phone number validation and user-message association
- Inbound SMS processing with PostgreSQL database storage
- Protected user dashboard displaying received messages
- Production-ready deployment configuration for Vercel or similar platforms
- Project Overview and Goals
- Set Up Your Next.js Project
- Create Next.js Application
- Install Required Dependencies
- Initialize Prisma
- Configure Environment Variables
- Define Database Schema
- Apply Database Migrations
- Project Structure Overview
- Implement User Authentication
- Create Prisma Client Instance
- NextAuth Base Configuration
- NextAuth Main Configuration
- Middleware for Route Protection
- Server Actions for Auth
- UI Components
- Build Sinch Webhook Handler
- Create Webhook Verification Utility
- Implement API Route
- Create Dashboard and Pages
- Dashboard Page
- Login Page
- Sign Up Page
- Production Deployment
- Environment Configuration
- Webhook URL Configuration
- Database Considerations
- Extension Ideas
- Frequently Asked Questions
Project Overview and Goals
Your application features:
- User authentication β Email/password credentials via NextAuth.js (Auth.js v5)
- Phone number association β E.164-formatted phone numbers linked to user profiles
- Inbound SMS webhooks β Receives SMS messages sent to your Sinch number
- Signature verification β HMAC-SHA256 authentication prevents unauthorized requests
- User identification β Matches sender phone numbers to registered users
- Message storage β Stores SMS content in PostgreSQL, linked to users
- Extensible foundation β Ready for automated replies and two-way conversations
Why receive inbound SMS with Next.js and Sinch?
Traditional web applications can't directly receive SMS messages from users. This guide shows you how to programmatically receive and process SMS messages through Sinch webhooks, linking them to authenticated user accounts within a modern Next.js framework. Use this foundation for:
- Healthcare applications: Appointment confirmations and prescription reminders
- Delivery services: Real-time order updates and driver communication
- Customer support systems: Instant query responses and issue tracking
- Two-factor authentication: SMS-based verification flows
- Notification systems: Critical alerts and time-sensitive communications
Technologies Used:
- Next.js (v15.5): React framework for building server-rendered applications (using App Router). As of October 2025, Next.js 15.5 is the latest version with React 19 support and Turbopack builds.
- NextAuth.js (v5 Beta / Auth.js): Authentication library for Next.js, simplifying login, logout, and
session management. (Disclaimer: This guide uses
next-auth@beta(v5.0.0-beta.25), now branded as Auth.js. While feature-complete and widely used, beta versions may contain bugs or breaking changes. For maximum stability in production, consider using the latest stable v4 release or carefully test v5 before deploying. NextAuth v5 requires Next.js 14.0 or higher.) - Sinch SMS API: Communications Platform as a Service (CPaaS) provider for SMS functionality (specifically receiving inbound messages via webhooks with HMAC-SHA256 signature verification).
- PostgreSQL: Relational database for storing user and message data.
- Prisma: Next-generation ORM for database access and migrations with support for optimized indexing strategies.
- TypeScript: For static typing and improved code quality.
- bcryptjs: For securely hashing user passwords (10 salt rounds recommended for production).
- Zod: For data validation (credentials, webhook payloads, E.164 phone format).
- Tailwind CSS: For styling (optional, included with create-next-app).
System Architecture:
graph TD
A[User Browser] -- HTTPS --> B(Next.js App / Vercel);
B -- Login Request --> C{NextAuth.js Middleware};
C -- Verify Credentials --> D[Database (PostgreSQL)];
D -- User Record --> C;
C -- Session Cookie --> A;
E[User's Phone] -- Sends SMS --> F(Sinch Platform);
F -- Inbound SMS Webhook (POST) --> G[Next.js API Route (/api/webhooks/sinch)];
G -- Verify Signature --> F;
G -- Lookup User by Phone (E.164) --> D;
G -- Store Message --> D;
D -- User/Message Data --> G;
G -- 200 OK --> F;
style G fill:#f9f,stroke:#333,stroke-width:2px
style F fill:#ccf,stroke:#333,stroke-width:2px
style D fill:#ff9,stroke:#333,stroke-width:2px
Prerequisites:
- Node.js (v18 or later required for Next.js 15; v20 LTS recommended for optimal compatibility) and npm/pnpm/yarn.
- Access to a PostgreSQL database instance (v12+ recommended).
- A Sinch account with API credentials and a provisioned SMS-enabled phone number.
- Basic understanding of React, TypeScript, and REST APIs.
- openssl command line tool for generating secrets. (Alternatives for Windows include Git Bash, Windows Subsystem for Linux (WSL), or online generation tools β use trusted tools for security-sensitive secrets).
- A tool for testing webhooks locally (e.g., ngrok, Vercel CLI, or Cloudflare Tunnel). Production alternatives include public IP addresses or other tunneling services.
Final Outcome:
A deployed Next.js application where users can register and login with phone number authentication. Incoming SMS messages sent to the configured Sinch number are securely received, verified via HMAC-SHA256, associated with the correct user based on their registered phone number (using consistent E.164 format), and stored in the PostgreSQL database.
1. Set Up Your Next.js Project
Initialize your Next.js application and install the necessary dependencies for receiving inbound SMS.
1.1 Create Next.js Application
Open your terminal and create a new Next.js project:
npx create-next-app@latest sinch-inbound-app --typescript --eslint --tailwind --src-dir --app --use-npm
cd sinch-inbound-app
Enable TypeScript, ESLint, Tailwind CSS, the src/ directory, and the App Router when prompted.
These options create a production-ready project structure with modern Next.js features.
1.2 Install Required Dependencies
Install authentication, database, and Sinch-related packages:
npm install next-auth@beta @next-auth/prisma-adapter prisma @prisma/client pg bcryptjs @types/bcryptjs zod @sinch/sdk-core
npm install --save-dev prisma
Package explanations:
- next-auth@beta (v5.0.0-beta.25) β Authentication library, now branded as Auth.js
- @next-auth/prisma-adapter β Connects NextAuth to your Prisma database
- prisma and @prisma/client β ORM for type-safe database access
- pg β PostgreSQL driver required by Prisma
- bcryptjs and @types/bcryptjs β Secure password hashing (10 salt rounds)
- zod β Runtime validation for credentials, webhooks, and phone numbers
- @sinch/sdk-core β Optional Sinch SDK for sending replies (not required for receiving)
1.3 Initialize Prisma
Set up Prisma in your project:
npx prisma init
This creates a prisma directory with schema.prisma and a .env file at
your project root.
1.4 Configure Environment Variables
Open the .env file created by Prisma and add these configuration variables. Never commit
this file to version control β it contains sensitive credentials.
.env
# .env
# Database Connection
# Replace with your actual PostgreSQL credentials
DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public
# NextAuth Secret
# Generate using: openssl rand -base64 32
# This secret encrypts session cookies and JWT tokens
AUTH_SECRET=YOUR_AUTH_SECRET_GENERATED_WITH_OPENSSL
# Sinch API Credentials
# Get these from your Sinch Dashboard: https://dashboard.sinch.com
# Required if you plan to send outbound SMS messages
SINCH_PROJECT_ID=YOUR_SINCH_PROJECT_ID
SINCH_APP_KEY=YOUR_SINCH_APP_KEY
SINCH_APP_SECRET=YOUR_SINCH_APP_SECRET
# Sinch Webhook Secret
# Create a strong secret YOU define: openssl rand -base64 32
# Configure this same secret in the Sinch portal for webhook signature verification
SINCH_WEBHOOK_SECRET=YOUR_STRONG_RANDOM_WEBHOOK_SECRET
# Application URL
# Development: http://localhost:3000
# Production: https://yourdomain.com
# Required for NextAuth redirects and callbacks
NEXTAUTH_URL=http://localhost:3000
Security requirements:
AUTH_SECRETβ Generate with openssl rand -base64 32. Never use a weak or hardcoded value. This protects all user sessions.SINCH_WEBHOOK_SECRETβ Generate with openssl rand -base64 32. You create this secret and configure it in both your code and the Sinch dashboard. Sinch uses it to sign webhook requests.DATABASE_URLβ Use SSL in production: add ?sslmode=require to your connection string.NEXTAUTH_URLβ Must match your deployment URL exactly, including protocol (http:// or https://).
1.5 Define Database Schema
Open prisma/schema.prisma and define your data models. Store phone numbers in E.164 format
(+15551234567) for consistent international number handling.
Prisma
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// NextAuth required models for session management
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
// User model with phone number for SMS association
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
password String? // Hashed with bcryptjs (10 salt rounds)
phoneNumber String? @unique // E.164 format (e.g., +15551234567)
image String?
accounts Account[]
sessions Session[]
messages Message[] // All SMS messages received by this user
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
// SMS message storage with user association
model Message {
id String @id @default(cuid())
sinchMessageId String @unique // Sinch's unique message identifier
fromNumber String // Sender's phone (E.164 format from Sinch)
toNumber String // Your Sinch number (E.164 format)
body String? @db.Text // Message text content
direction String // 'inbound' or 'outbound'
timestamp DateTime // When Sinch received the message
userId String? // Link to User (null if sender not registered)
user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
createdAt DateTime @default(now()) // When we stored it
// Explicit indexes for query optimization (B-tree indexes for range/equality operations)
// These indexes significantly improve query performance on large datasets (thousands+ records)
@@index([userId]) // Optimizes: WHERE userId = ? and JOIN operations
@@index([fromNumber]) // Optimizes: WHERE fromNumber = ? for sender lookups
@@index([timestamp]) // Optimizes: ORDER BY timestamp and date range queries
}
Key design decisions:
- E.164 phone format β International standard ensures consistent number matching
- Unique constraints β Prevent duplicate users and messages
- Explicit indexes β B-tree indexes speed up queries by 10-100Γ on large datasets
- Optional user link β Messages stored even if sender isn't registered
- Cascade deletes β Removing a user also removes their sessions (not their messages)
1.6 Apply Database Migrations
Sync your Prisma schema with your PostgreSQL database:
npx prisma db push
This command creates all tables, indexes, and constraints defined in your schema. Use db push
for rapid development iteration.
For production deployments, use Prisma's migration system for version control:
npx prisma migrate dev --name init
npx prisma migrate deploy
Migrations provide:
- Version-controlled schema changes
- Rollback capabilities
- Safe multi-developer workflows
- Deployment history tracking
1.7 Project Structure Overview
Your src directory structure:
src/
βββ app/
β βββ api/ # API routes (Webhook handler here)
β β βββ webhooks/
β β βββ sinch/
β β βββ route.ts # Sinch webhook handler
β βββ (auth)/ # Authentication related pages (optional grouping)
β β βββ login/
β β β βββ page.tsx
β β βββ signup/
β β βββ page.tsx
β βββ dashboard/ # Protected area
β β βββ page.tsx
β βββ layout.tsx # Root layout
β βββ page.tsx # Home page
β βββ globals.css
βββ components/ # Reusable UI components
β βββ auth/
β β βββ LoginForm.tsx
β β βββ SignUpForm.tsx
β βββ ui/ # UI primitives (Button, Input, Label - needs creation/install)
β βββ button.tsx
β βββ input.tsx
β βββ label.tsx
βββ lib/ # Helper functions, Prisma client, actions
β βββ prisma.ts # Prisma client instance
β βββ actions/ # Server Actions (auth, potentially messaging)
β β βββ auth.ts
β βββ utils.ts # Utility functions (e.g., webhook verification)
βββ auth.config.ts # NextAuth base configuration
βββ auth.ts # NextAuth main configuration & handlers
βββ middleware.ts # NextAuth Middleware for route protection
βββ ... (other config files)
2. Implement User Authentication
Set up NextAuth.js (Auth.js v5) for secure user registration and login with email, password, and phone number validation.
2.1 Create Prisma Client Instance
Create a singleton Prisma client to prevent connection exhaustion in development:
import { PrismaClient } from '@prisma/client';
declare global {
var prisma: PrismaClient | undefined;
}
const prisma =
global.prisma ||
new PrismaClient({
log: process.env.NODE_ENV === 'development'
? ['query', 'error', 'warn']
: ['error'],
});
if (process.env.NODE_ENV !== 'production') {
global.prisma = prisma;
}
export default prisma;
Why use a global variable? Next.js hot-reloading in development creates new module instances on each save. Without the global variable, you'd create dozens of database connections, eventually exhausting your connection pool.
2.2 NextAuth Base Configuration (auth.config.ts)
Define basic configurations like custom pages and the authorization callback used by the middleware.
import type { NextAuthConfig } from 'next-auth';
export const authConfig = {
pages: {
signIn: '/login',
},
callbacks: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
const isOnAuthPage = nextUrl.pathname === '/login' || nextUrl.pathname === '/signup';
if (isOnDashboard) {
if (isLoggedIn) return true;
return false;
} else if (isLoggedIn && isOnAuthPage) {
const dashboardUrlBase = process.env.NEXTAUTH_URL;
if (dashboardUrlBase) {
console.log(`Redirecting logged-in user from ${nextUrl.pathname} to dashboard.`);
return Response.redirect(new URL('/dashboard', dashboardUrlBase));
} else {
console.warn('NEXTAUTH_URL is not set. Attempting relative redirect to /dashboard.');
return Response.redirect(new URL('/dashboard', nextUrl.origin));
}
}
return true;
},
},
providers: [
],
} satisfies NextAuthConfig;
2.3 NextAuth Main Configuration (auth.ts)
This file includes the provider logic (Credentials), database interactions, and password handling.
import NextAuth from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { z } from 'zod';
import bcrypt from 'bcryptjs';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { authConfig } from './auth.config';
import prisma from '@/lib/prisma';
import type { User } from '@prisma/client';
async function getUserByEmail(email: string): Promise<User | null> {
try {
return await prisma.user.findUnique({ where: { email } });
} catch (error) {
console.error('Failed to fetch user by email:', error);
throw new Error('Database error while fetching user.');
}
}
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
...authConfig,
adapter: PrismaAdapter(prisma),
session: { strategy: 'jwt' },
providers: [
Credentials({
async authorize(credentials) {
const parsedCredentials = z
.object({
email: z.string().email({ message: 'Invalid email format.' }),
password: z.string().min(6, { message: 'Password must be at least 6 characters.' }),
})
.safeParse(credentials);
if (!parsedCredentials.success) {
console.log('Invalid credentials format:', parsedCredentials.error.flatten().fieldErrors);
return null;
}
const { email, password } = parsedCredentials.data;
const user = await getUserByEmail(email);
if (!user || !user.password) {
console.log(`No user found for email: ${email} or user has no password set.`);
return null;
}
const passwordsMatch = await bcrypt.compare(password, user.password);
if (passwordsMatch) {
console.log(`User ${email} authenticated successfully.`);
return { id: user.id, email: user.email, name: user.name, image: user.image };
}
console.log(`Password mismatch for user: ${email}`);
return null;
},
}),
],
callbacks: {
...authConfig.callbacks,
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
if (token && session.user) {
session.user.id = token.id as string;
}
return session;
},
}
});
2.4 Middleware for Route Protection
Create the middleware file to protect routes based on authentication status using the authorized
callback defined in auth.config.ts.
import NextAuth from 'next-auth';
import { authConfig } from './auth.config';
export default NextAuth(authConfig).auth;
export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$|favicon.ico).*)'],
};
2.5 Server Actions for Auth (actions/auth.ts)
Create Server Actions for handling sign-up and login form submissions.
'use server';
import { z } from 'zod';
import bcrypt from 'bcryptjs';
import { AuthError } from 'next-auth';
import { signIn, signOut } from '@/auth';
import prisma from '@/lib/prisma';
const LoginSchema = z.object({
email: z.string().email({ message: 'Please enter a valid email.' }),
password: z.string().min(1, { message: 'Password is required.' }),
});
export type LoginState = {
errors?: {
email?: string[];
password?: string[];
credentials?: string[];
};
message?: string | null;
};
export async function authenticate(
prevState: LoginState | undefined,
formData: FormData,
): Promise<LoginState> {
const validatedFields = LoginSchema.safeParse(
Object.fromEntries(formData.entries()),
);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
message: 'Invalid fields. Failed to Login.',
};
}
const { email, password } = validatedFields.data;
try {
console.log(`Attempting sign in for user: ${email}`);
await signIn('credentials', {
email,
password,
redirect: false,
});
console.log(`Sign in successful for: ${email}. Redirect should occur via middleware.`);
return { message: 'Login successful! Redirectingβ¦' };
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
console.error('CredentialsSignin error:', error.cause?.err?.message);
return { message: 'Invalid email or password.', errors: { credentials: ['Invalid email or password.'] } };
case 'CallbackRouteError':
console.error('CallbackRouteError:', error.cause?.err?.message);
return { message: `Authentication callback failed: ${error.cause?.err?.message || 'Unknown callback error'}`, errors: { credentials: ['Authentication callback failed.'] } };
default:
console.error('Unknown NextAuth error:', error);
return { message: 'Something went wrong during authentication.', errors: { credentials: ['An unexpected error occurred.'] } };
}
}
console.error('Non-AuthError during authenticate:', error);
return { message: 'An unexpected server error occurred during login.', errors: { credentials: ['Server error during login.'] } };
}
}
const SignUpSchema = z.object({
email: z.string().email({ message: 'Please enter a valid email.' }),
password: z.string().min(6, { message: 'Password must be at least 6 characters.' }),
name: z.string().min(1, { message: 'Name is required.' }),
phoneNumber: z.string()
.min(8, { message: 'Please enter a valid phone number (e.g., +15551234567).' })
.regex(/^\+[1-9]\d{1,14}$/, { message: "Invalid phone number format. Must be E.164 format: +[country code][number] (e.g., +15551234567). Total 2-15 digits." }),
});
export type SignUpState = {
errors?: {
email?: string[];
password?: string[];
name?: string[];
phoneNumber?: string[];
server?: string[];
};
message?: string | null;
success?: boolean;
};
export async function registerUser(
prevState: SignUpState | undefined,
formData: FormData,
): Promise<SignUpState> {
const validatedFields = SignUpSchema.safeParse(
Object.fromEntries(formData.entries()),
);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
message: 'Invalid fields. Failed to Sign Up.',
success: false,
};
}
const { email, password, name, phoneNumber } = validatedFields.data;
const formattedPhoneNumber = phoneNumber.startsWith('+') ? phoneNumber : `+${phoneNumber}`;
try {
const existingUserByEmail = await prisma.user.findUnique({ where: { email } });
if (existingUserByEmail) {
return { message: 'Email already in use.', errors: { email: ['Email already exists.'] }, success: false };
}
const existingUserByPhone = await prisma.user.findUnique({ where: { phoneNumber: formattedPhoneNumber } });
if (existingUserByPhone) {
return { message: 'Phone number already in use.', errors: { phoneNumber: ['Phone number already exists.'] }, success: false };
}
const hashedPassword = await bcrypt.hash(password, 10);
await prisma.user.create({
data: {
email,
password: hashedPassword,
name,
phoneNumber: formattedPhoneNumber,
},
});
console.log(`User registered successfully: ${email}`);
return { message: 'Registration successful! Please log in.', success: true };
} catch (error) {
console.error('Registration error:', error);
if (error instanceof Error && 'code' in error && (error as any).code === 'P2002') {
const target = (error as any).meta?.target;
if (target?.includes('email')) {
return { message: 'Email already in use.', errors: { email: ['Email already exists.'] }, success: false };
}
if (target?.includes('phoneNumber')) {
return { message: 'Phone number already in use.', errors: { phoneNumber: ['Phone number already exists.'] }, success: false };
}
}
return { message: 'Database error: Failed to register user.', errors: { server: ['Could not create user account.'] }, success: false };
}
}
export async function logout() {
try {
console.log("Attempting user sign out.");
await signOut({ redirectTo: '/login' });
} catch (error) {
console.error("Sign out error:", error);
}
}
2.6 UI Components (Login and Sign Up Forms)
Create basic form components. Note: These examples assume you have basic
Button, Input, and Label components. Install shadcn/ui for pre-built
components:
npx shadcn-ui@latest init
npx shadcn-ui@latest add button input label
'use client';
import { useActionState } from 'react';
import { authenticate, LoginState } from '@/lib/actions/auth';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import Link from 'next/link';
export default function LoginForm() {
const initialState: LoginState | undefined = undefined;
const [state, formAction, isPending] = useActionState(authenticate, initialState);
return (
<form action={formAction} className="space-y-4 w-full max-w-sm mx-auto p-6 border rounded-lg shadow-md bg-white">
<h2 className="text-2xl font-semibold text-center">Login</h2>
{/* Display general success or error messages */}
{state?.message && !state.errors?.credentials && !state.errors?.email && !state.errors?.password && (
<p className={`text-sm p-2 rounded ${state.errors ? 'text-red-600 bg-red-100' : 'text-green-600 bg-green-100'}`}>
{state.message}
</p>
)}
{/* Display specific credentials error */}
{state?.errors?.credentials && (
<p className="text-sm text-red-600 bg-red-100 p-2 rounded">{state.errors.credentials.join(', ')}</p>
)}
<div className="space-y-1">
<Label htmlFor="email">Email</Label>
<Input
id="email"
name="email"
type="email"
placeholder="you@example.com"
required
aria-describedby="email-error"
/>
{state?.errors?.email && (
<p id="email-error" className="text-sm text-red-500">
{state.errors.email.join(', ')}
</p>
)}
</div>
<div className="space-y-1">
<Label htmlFor="password">Password</Label>
<Input
id="password"
name="password"
type="password"
required
minLength={6}
aria-describedby="password-error"
/>
{state?.errors?.password && (
<p id="password-error" className="text-sm text-red-500">
{state.errors.password.join(', ')}
</p>
)}
</div>
<Button type="submit" className="w-full" aria-disabled={isPending}>
{isPending ? 'Logging inβ¦' : 'Log In'}
</Button>
<p className="text-center text-sm text-gray-600">
Don't have an account?{' '}
<Link href="/signup" className="text-blue-600 hover:underline">
Sign Up
</Link>
</p>
</form>
);
}
'use client';
import { useActionState, useEffect } from 'react';
import { registerUser, SignUpState } from '@/lib/actions/auth';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
export default function SignUpForm() {
const initialState: SignUpState | undefined = undefined;
const [state, formAction, isPending] = useActionState(registerUser, initialState);
const router = useRouter();
useEffect(() => {
if (state?.success) {
console.log("Registration successful:", state.message);
router.push('/login');
}
}, [state?.success, state?.message, router]);
return (
<form action={formAction} className="space-y-4 w-full max-w-sm mx-auto p-6 border rounded-lg shadow-md bg-white">
<h2 className="text-2xl font-semibold text-center">Sign Up</h2>
{/* Display general error messages (not success message, as we redirect) */}
{state?.message && !state.success && (
<p className="text-sm text-red-600 bg-red-100 p-2 rounded">{state.message}</p>
)}
{state?.errors?.server && (
<p className="text-sm text-red-600 bg-red-100 p-2 rounded">{state.errors.server.join(', ')}</p>
)}
<div className="space-y-1">
<Label htmlFor="name">Name</Label>
{/* ... (rest of SignUpForm component - input fields for name, email, phone, password) ... */}
{/* Make sure inputs have correct id, name, type, required attributes, and error handling similar to LoginForm */}
{/* Example for Name input: */}
<Input
id="name"
name="name"
type="text"
placeholder="Your Name"
required
aria-describedby="name-error"
/>
{state?.errors?.name && (
<p id="name-error" className="text-sm text-red-500">
{state.errors.name.join(', ')}
</p>
)}
</div>
{/* Add similar divs for Email, Phone Number, and Password */}
<div className="space-y-1">
<Label htmlFor="email">Email</Label>
<Input
id="email"
name="email"
type="email"
placeholder="you@example.com"
required
aria-describedby="signup-email-error"
/>
{state?.errors?.email && (
<p id="signup-email-error" className="text-sm text-red-500">
{state.errors.email.join(', ')}
</p>
)}
</div>
<div className="space-y-1">
<Label htmlFor="phoneNumber">Phone Number (e.g., +15551234567)</Label>
<Input
id="phoneNumber"
name="phoneNumber"
type="tel" // Use type="tel" for phone numbers
placeholder="+15551234567"
required
aria-describedby="phoneNumber-error"
/>
{state?.errors?.phoneNumber && (
<p id="phoneNumber-error" className="text-sm text-red-500">
{state.errors.phoneNumber.join(', ')}
</p>
)}
</div>
<div className="space-y-1">
<Label htmlFor="signup-password">Password</Label>
<Input
id="signup-password"
name="password"
type="password"
required
minLength={6}
aria-describedby="signup-password-error"
/>
{state?.errors?.password && (
<p id="signup-password-error" className="text-sm text-red-500">
{state.errors.password.join(', ')}
</p>
)}
</div>
<Button type="submit" className="w-full" aria-disabled={isPending}>
{isPending ? 'Signing Upβ¦' : 'Sign Up'}
</Button>
<p className="text-center text-sm text-gray-600">
Already have an account?{' '}
<Link href="/login" className="text-blue-600 hover:underline">
Log In
</Link>
</p>
</form>
);
}
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β