A Comprehensive Guide to Integrating Auth.js with Next.js and TypeScript

A Comprehensive Guide to Integrating Auth.js with Next.js and TypeScript

372

Next.js 14: A Comprehensive Guide to Integrating Auth.js with TypeScript

Next.js, a popular React framework, continues to evolve with its latest release, Next.js 14. Packed with new features, improved performance, and enhanced developer experience, it is the go-to choice for building modern web applications. One critical aspect of web applications is authentication, and integrating Auth.js with Next.js 14 using TypeScript enables secure and type-safe development. This guide will walk you through the setup and integration process step-by-step.

Table of Contents

  1. What’s New in Next.js 14?
  2. Why Use Auth.js for Authentication?
  3. Setting Up a Next.js 14 Project with TypeScript
  4. Installing and Configuring Auth.js
  5. Creating API Routes for Authentication
  6. Protecting Pages and Components
  7. Customizing the Auth.js Behavior
  8. Best Practices for Secure Authentication
  9. Common Issues and Troubleshooting
  10. Conclusion

What’s New in Next.js 14?

Before diving into authentication, it’s essential to highlight the key improvements in Next.js 14:

  • Server Actions: Simplify server-side logic by embedding server-side actions within your components.
  • Optimized Rendering: Enhanced streaming and React’s new features like the use hook for asynchronous code.
  • Improved Build Times: Faster builds and updates for large-scale applications.
  • TypeScript Enhancements: Automatic TypeScript project references for seamless type-checking across the app.

Why Use Auth.js for Authentication?

Auth.js (formerly NextAuth.js) is a robust, extensible, and easy-to-use authentication library for Next.js. Its benefits include:

  • Provider Support: Integrates with multiple providers like Google, GitHub, and Facebook.
  • Session Management: Built-in session handling with support for JWT and database sessions.
  • TypeScript Support: Full typings for secure and scalable development.

Setting Up a Next.js 14 Project with TypeScript

Follow these steps to create a Next.js 14 project with TypeScript:

  1. Initialize the Project:
bash
1npx create-next-app@latest nextjs-auth-demo --typescript
2cd nextjs-auth-demo


  1. Update Dependencies: Ensure you’re using the latest Next.js version.
bash
1npm install next@14 react react-dom


  1. Start the Development Server:
bash
1npm run dev


Installing and Configuring Auth.js

  1. Install Auth.js:
bash
1npm install next-auth


  1. Create a Configuration File: In the pages/api/auth directory, create a file named [...nextauth].ts:
typescript
1import NextAuth from "next-auth";
2import GoogleProvider from "next-auth/providers/google";
3
4export default NextAuth({
5  providers: [
6    GoogleProvider({
7      clientId: process.env.GOOGLE_CLIENT_ID!,
8      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
9    }),
10  ],
11  secret: process.env.NEXTAUTH_SECRET,
12});


  1. Environment Variables: Add the following to a .env.local file:
text
1GOOGLE_CLIENT_ID=your-google-client-id
2GOOGLE_CLIENT_SECRET=your-google-client-secret
3NEXTAUTH_SECRET=your-random-secret


Creating API Routes for Authentication

Next.js API routes simplify the integration of Auth.js. The [...nextauth].ts file serves as the authentication handler. Auth.js automatically creates endpoints like /api/auth/signin, /api/auth/signout, and /api/auth/session.

Example usage in a client component:

typescript
1'use client';
2
3import { signIn, signOut, useSession } from "next-auth/react";
4
5export default function LoginButton() {
6  const { data: session } = useSession();
7
8  if (session) {
9    return (
10      <div>
11        <p>Welcome, {session.user?.name}</p>
12        <button onClick={() => signOut()}>Sign Out</button>
13      </div>
14    );
15  }
16
17  return <button onClick={() => signIn("google")}>Sign In with Google</button>;
18}


Protecting Pages and Components

To restrict access to specific pages, use the getServerSideProps function:

typescript
1import { getSession } from "next-auth/react";
2import { GetServerSideProps } from "next";
3
4export const getServerSideProps: GetServerSideProps = async (context) => {
5  const session = await getSession(context);
6
7  if (!session) {
8    return {
9      redirect: {
10        destination: "/api/auth/signin",
11        permanent: false,
12      },
13    };
14  }
15
16  return {
17    props: { session },
18  };
19};
20
21export default function ProtectedPage() {
22  return <h1>Welcome to the protected page!</h1>;
23}

Customizing the Auth.js Behavior

  1. Callbacks: Modify JWT behavior or add user roles:
typescript
1callbacks: {
2  async jwt({ token, user }) {
3    if (user) {
4      token.role = user.role;
5    }
6    return token;
7  },
8},
9
10
11//Events: Log or trigger actions on authentication events:
12
13events: {
14  async signIn(message) {
15    console.log('User signed in:', message);
16  },
17},

Best Practices for Secure Authentication

  1. Use HTTPS: Ensure your app runs over HTTPS in production.
  2. Environment Variables: Store sensitive information securely.
  3. CSRF Protection: Auth.js includes built-in CSRF protection for API routes.

Common Issues and Troubleshooting

  • Misconfigured Environment Variables: Double-check .env.local for typos.
  • Outdated Dependencies: Ensure all packages are up to date.
  • Invalid Redirect URI: Verify that the OAuth provider’s redirect URI matches your application settings.

FAQs

  1. Can I use Auth.js with other frameworks? Yes, Auth.js can be adapted for non-Next.js frameworks but works best with Next.js.
  2. Does Auth.js support custom authentication providers? Absolutely. You can define custom providers for bespoke authentication systems.
  3. How do I persist sessions across tabs? Auth.js handles session persistence automatically with cookies.
  4. Can I use multiple providers simultaneously? Yes, you can configure multiple providers in the [...nextauth].ts file.
  5. How do I secure API routes? Use the getSession or getServerSession method to validate user sessions before processing requests.

Written by

Avatar of Mazen Alsenih

Mazen Alsenih