Skip to content
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

fix: update Middleware #4757

Merged
merged 10 commits into from Jun 24, 2022
Merged

fix: update Middleware #4757

merged 10 commits into from Jun 24, 2022

Conversation

balazsorban44
Copy link
Member

Following: https://nextjs.org/docs/messages/middleware-upgrade-guide#how-to-upgrade

☕️ Reasoning

What changes are being made? What feature/bug is being fixed here?

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

Please scout and link issues that might be solved by this PR.

Fixes: INSERT_ISSUE_LINK_HERE

📌 Resources

@vercel
Copy link

vercel bot commented Jun 23, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
next-auth ⬜️ Ignored (Inspect) Jun 23, 2022 at 1:09PM (UTC)

@github-actions github-actions bot added the core Refers to `@auth/core` label Jun 23, 2022
@balazsorban44 balazsorban44 temporarily deployed to Preview June 23, 2022 13:11 Inactive
@balazsorban44 balazsorban44 marked this pull request as ready for review June 23, 2022 13:26
@github-actions
Copy link

🎉 Experimental release published on npm!

npm i next-auth@0.0.0-pr.4757.5ac99859
yarn add next-auth@0.0.0-pr.4757.5ac99859

Copy link
Member

@ndom91 ndom91 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@balazsorban44 balazsorban44 merged commit 99f5b96 into main Jun 24, 2022
@balazsorban44 balazsorban44 deleted the docs/update-middleware branch June 24, 2022 12:11
@Juneezee
Copy link
Contributor

@balazsorban44 I'm getting null for token in the example. Before this PR (using the old _middleware.ts), the token is returned correctly
image

@balazsorban44
Copy link
Member Author

Note the content of this PR has not been released yet, besides #4757 (comment)

I tested this locally just now, and it works. Hard to tell more without a reproduction, but I would suggest opening an issue instead.

@Juneezee
Copy link
Contributor

Juneezee commented Jun 26, 2022

@balazsorban44 The only files I have changed are [...nextauth].ts and middleware.ts. Installed node_modules using yarn --ignore-engines. I'm using a CredentialsProvider that always return a dummy user object.

diff --git a/middleware.ts b/middleware.ts
index 2339882..5d8057c 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -3,9 +3,16 @@ import { withAuth } from "next-auth/middleware"
 // More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
 export default withAuth({
   callbacks: {
-    authorized: ({ req, token }) =>
+    authorized: ({ req, token }) => {
+      console.log("============ MIDDLEWARE =============")
+      console.log("req: ", req)
+      console.log("cookies: ", req.cookies)
+      console.log("headers: ", req.headers)
+      console.log("token: ", token)
+      console.log("============ MIDDLEWARE =============")
       // /admin requires admin role, but /me only requires the user to be logged in.
-      req.nextUrl.pathname !== "/admin" || token?.userRole === "admin",
+      return req.nextUrl.pathname !== "/admin" || token?.userRole === "admin"
+    },
   },
 })

--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -4,6 +4,7 @@ import FacebookProvider from "next-auth/providers/facebook"
 import GithubProvider from "next-auth/providers/github"
 import TwitterProvider from "next-auth/providers/twitter"
 import Auth0Provider from "next-auth/providers/auth0"
+import CredentialsProvider from "next-auth/providers/credentials"
 // import AppleProvider from "next-auth/providers/apple"
 // import EmailProvider from "next-auth/providers/email"
 
@@ -50,6 +51,22 @@ export const authOptions: NextAuthOptions = {
       clientSecret: process.env.AUTH0_SECRET,
       issuer: process.env.AUTH0_ISSUER,
     }),
+    CredentialsProvider({
+      // The name to display on the sign in form (e.g. 'Sign in with...')
+      name: "Credentials",
+      credentials: {
+        username: { label: "Username", type: "text", placeholder: "jsmith" },
+        password: { label: "Password", type: "password" },
+      },
+      async authorize(credentials, req) {
+        return {
+          name: "test",
+          email: "test@email.com",
+          image: "test.jpg",
+          id: "testabc123",
+        }
+      },
+    }),
   ],
   theme: {
     colorScheme: "light",

It seems req.cookies and req.headers have changed in Next.js 12.1.7-canary.45. Using getToken({ req }) returns null as well
EDIT: Cookies API are revamped https://nextjs.org/docs/messages/middleware-upgrade-guide#cookies-api-revamped

image

@balazsorban44
Copy link
Member Author

balazsorban44 commented Jun 26, 2022

Correct, this PR was opened to address these changes as linked in the description above. Once the Middleware changes go live in the next Next.js release, the recommendation will be to use the stable API anyway. Remember, Middleware has been in beta for a while, so this isn't breaking semver either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants