This project demonstrates how to implement federated authentication with Amazon Cognito and Google OAuth using Next.js and SST (Serverless Stack) v3.
To run this application, follow these steps:
-
Ensure you have Node.js installed on your system.
-
Clone this repository to your local machine.
-
Install the dependencies by running
pnpm i
in the project root directory. -
Create a
.env
file in the root directory with the following variables:COGNITO_USER_POOL_DOMAIN=your-domain-prefix GOOGLE_OAUTH_CLIENT_ID=your-google-client-id GOOGLE_OAUTH_CLIENT_SECRET=your-google-client-secret
-
Start the development server by running:
npx sst dev
This command will deploy the application to your AWS account and start the local development environment.
This application is built using SST v3, which provides a powerful framework for building serverless applications on AWS.
It showcases a simple authentication flow that uses Amazon Cognito as the identity provider with Google OAuth integration. The app allows users to:
- Sign in with their Google account
- View their authentication details
- Sign out
sst.config.ts
: SST configuration fileinfra/index.ts
: Infrastructure setup with AWS Cognito resourcesapp/page.tsx
: Main Next.js page for the frontendapp/auth/page.tsx
: Authentication callback pageapp/components/GoogleSignInButton.tsx
: Component for Google sign-in functionalityapp/components/AuthProviderWrapper.tsx
: OIDC authentication provider wrapperapp/lib/types.ts
: TypeScript type definitionsapp/lib/utils.ts
: Utility functions
This project sets up the following AWS resources:
- Amazon Cognito User Pool: Manages user authentication and identity
- Cognito User Pool Domain: Provides a hosted UI for authentication
- Cognito Identity Provider: Configures Google as a federated identity provider
- Cognito User Pool Client: Configures the application client for authentication
To learn more about the technologies used in this project: