Skip to content

How to add 3rd party auth for Nodejs?

Updated: at 09:16 AM

Table of contents

Open Table of contents

Preface

My tech stack is Next.js and Node.js. I use next rewrites to resolve the CORS problem, which allows my application to communicate with external APIs without facing cross-origin issues.

CORS

To resolve the CORS problem, put the following code in next.config.js:

async rewrites() {
    return [
      {
        source: "/api/:path*",
        destination:
          process.env.NEXT_PUBLIC_ENV === "PRD"
            ? process.env.NEXT_PUBLIC_PRD_API_URL + "/api/:path*"
            : process.env.NEXT_PUBLIC_STG_API_URL + "/api/:path*",
      },
    ];
  },

Auth

This section covers how to support third-party providers such as Google and GitHub, including configurations for both frontend and backend.

3rd party

Take Github for example:

Frontend

Backend

In the backend, I use Passport.js and JSON Web Tokens (JWT) to handle authentication.

After the authentication process, we can redirect the user to the Frontend Home URL, providing a seamless user experience.