Currently in the process of developing a web platform utilizing [email protected] and Auth.js([email protected]).
The provider has been configured with the given code, allowing successful signing in using the "Sign in" button.
auth.ts
import NextAuth from 'next-auth'
import Cognito from 'next-auth/providers/cognito'
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Cognito],
})
app/login/page.tsx
import { auth, signIn } from '@/auth'
export default async function Login() {
return (
<form
action={async () => {
'use server'
return await signIn('cognito')
}}
>
<button type="submit">Sign in</button>
</form>
)
}
Challenges Encountered
Incorporating middleware to secure all pages and automatically redirect users to the Amazon Cognito(IdP) sign-in page is causing issues. The current code is failing to work as intended (remaining on protected pages without an active session and failing to redirect).
Is there a method to directly redirect to the Cognito sign-in page?
Note: While I am able to redirect users to /login within the application, I aim to streamline this process for user convenience.
middleware
import { signIn, auth } from "@/auth"
export default auth((request) => {
if (!request.auth) {
signIn('cognito')
}
})
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|login).*)"],
}
Error Log
ReadonlyRequestCookiesError: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
at Proxy.callable (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/spec-extension/adapters/request-cookies.js:22:15)
at signIn (webpack-internal:///(middleware)/./node_modules/next-auth/lib/actions.js:53:65)
⨯ unhandledRejection: ReadonlyRequestCookiesError: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
at Proxy.callable (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/spec-extension/adapters/request-cookies.js:22:15)
at signIn (webpack-internal:...