Skip to content

Proof-of-concept: Support OAuth and OIDC auth providers #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#######################################
# Authentication
#######################################

# A random string used for encrypting tokens and verification hashes.
# Generate its value by running `openssl rand -base64 32`
# or via https://generate-secret.vercel.app/32
NEXTAUTH_SECRET="Replace me"

# The template provides a Cognito authentication example by default. To make it work,
# you'll need to set these. Using Cognito is not required! See the setup-authentication.md
# documentation for more details.
COGNITO_CLIENT_ID=
COGNITO_CLIENT_SECRET=
COGNITO_DOMAIN=
COGNITO_ISSUER=https://cognito-idp.{PUT REGION HERE}.amazonaws.com/{PUT USER POOL ID HERE}
COGNITO_DOMAIN=https://{PUT SUBDOMAIN HERE}.auth.{PUT REGION HERE}.amazoncognito.com
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npm-debug.log*

# local env files
.env*.local
!.env.local.example

# vercel
.vercel
Expand Down
1 change: 1 addition & 0 deletions app/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />
Copy link
Contributor Author

@sawyerh sawyerh Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Not auth related. Added due to Next.js App router being utilized in this branch


// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
149 changes: 146 additions & 3 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@uswds/uswds": "3.1.0",
"i18next": "^23.0.0",
"next": "^14.0.0",
"next-auth": "^5.0.0-beta.3",
"next-i18next": "^14.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
7 changes: 7 additions & 0 deletions app/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @file Behind the scenes, this creates all the relevant OAuth API routes within /api/auth/*
* such as GET /api/auth/signin and POST /api/auth/signin/:provider
*
* @see https://authjs.dev/getting-started/providers/oauth-tutorial
*/
export { GET, POST } from "src/auth";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks a little different from the snippet in the tutorial, which looks like

export default NextAuth({
  providers: [ ... ],
})

just curious for my own understanding the choice to do it this way. my guess is because you wanted to use the new App Router and the NextAuth snippet is using the old method of routing?

second question, also for my edification: since my typescript is a bit rusty but i don't see how the type exported from src/auth matches, it looks like src/auth exports { handlers: { GET, POST } }, but here we have { GET, POST } (without handlers), so how does that work? nvm figured this part out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this branch is using the 5.0 beta version so some of the existing docs are out of date

17 changes: 17 additions & 0 deletions app/src/app/auth/signout/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";

import { federateSignOut } from "src/auth";

import { useEffect } from "react";

export default function Page() {
/**
* Not super sure why this needs to be called on the client-side, but at the
* moment the Auth.js signOut method has a dependency on `window`, at a minimum.
*/
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my guess is they need to clear some cookies, which only get sent to the matching domain by the browser for security reasons

useEffect(() => {
federateSignOut().catch(console.error);
}, []);

return <div>Signing out...</div>;
}
15 changes: 15 additions & 0 deletions app/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const metadata = {
title: "Next.js template",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
21 changes: 21 additions & 0 deletions app/src/app/user/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { auth } from "src/auth";

import Link from "next/link";

export default async function Page() {
const session = await auth();

if (session) {
return (
<>
<p>
<Link href="/auth/signout">Sign out</Link>
</p>
<strong>User ID: {session.sub}</strong>
<code>{JSON.stringify(session, null, 2)}</code>
</>
);
}

return <Link href="/api/auth/signin">Sign in</Link>;
}
Loading