Authentication Setup
SaaS Developer Templates comes pre-integrated with NextAuth, a powerful library for managing authentication in Next.js applications. Follow this guide to configure authentication for your SaaS project.
Step 1: Install Dependencies
First, ensure that you have NextAuth installed in your project. If not, install it using the following command:
npm install next-auth
Step 2: Configure Authentication Providers
- 
Create a new file at pages/api/auth/[...nextauth].js:mkdir -p pages/api/auth
 touch pages/api/auth/[...nextauth].js
- 
Add the following code to pages/api/auth/[...nextauth].js:import NextAuth from 'next-auth';
 import GoogleProvider from 'next-auth/providers/google';
 import GithubProvider from 'next-auth/providers/github';
 export default NextAuth({
 providers: [
 GoogleProvider({
 clientId: process.env.GOOGLE_CLIENT_ID,
 clientSecret: process.env.GOOGLE_CLIENT_SECRET,
 }),
 GithubProvider({
 clientId: process.env.GITHUB_CLIENT_ID,
 clientSecret: process.env.GITHUB_CLIENT_SECRET,
 }),
 ],
 secret: process.env.NEXTAUTH_SECRET,
 callbacks: {
 async session({ session, token }) {
 session.user.id = token.sub;
 return session;
 },
 },
 });
- 
Update your .envfile with the credentials for your authentication providers:GOOGLE_CLIENT_ID=your-google-client-id
 GOOGLE_CLIENT_SECRET=your-google-client-secret
 GITHUB_CLIENT_ID=your-github-client-id
 GITHUB_CLIENT_SECRET=your-github-client-secret
 NEXTAUTH_SECRET=your-random-secretReplace your-google-client-id,your-google-client-secret, etc., with your actual credentials.
Step 3: Add Authentication to Your App
- 
Use the SessionProvidercomponent from NextAuth to wrap your app:Open pages/_app.jsorpages/_app.tsxand add:import { SessionProvider } from 'next-auth/react';
 export default function MyApp({ Component, pageProps: { session, ...pageProps } }) {
 return (
 <SessionProvider session={session}>
 <Component {...pageProps} />
 </SessionProvider>
 );
 }
- 
Protect specific pages or components using the useSessionhook:import { useSession, signIn, signOut } from 'next-auth/react';
 export default function ProtectedPage() {
 const { data: session } = useSession();
 if (!session) {
 return (
 <div>
 <p>You need to sign in to access this page.</p>
 <button onClick={() => signIn()}>Sign In</button>
 </div>
 );
 }
 return (
 <div>
 <p>Welcome, {session.user.name}!</p>
 <button onClick={() => signOut()}>Sign Out</button>
 </div>
 );
 }
Step 4: Customizing Authentication
NextAuth provides extensive customization options:
- Callbacks: Customize session behavior.
- JWT Tokens: Add custom claims or manage token lifecycle.
- Database: Store user sessions in a database by configuring Prisma or another database of your choice.
Learn more in the NextAuth documentation.
Troubleshooting
- Provider Errors: Double-check your API keys and secret configurations.
- Environment Variables: Ensure .envvariables are loaded correctly (restart the server if necessary).
- NextAuth Secret: Generate a secure secret using openssl rand -base64 32if needed.
Your authentication setup is now complete! 🎉
Proceed to the next step: Database Setup.