diff --git a/middleware.js b/middleware.js deleted file mode 100644 index c336c853..00000000 --- a/middleware.js +++ /dev/null @@ -1,16 +0,0 @@ -import { NextResponse } from 'next/server' - -export function middleware(req) { - const locale = req.cookies.get('NEXT_LOCALE') || 'en'; - - if (req.nextUrl.pathname.startsWith('/reset-password') && locale !== 'en' && req.nextUrl.locale !== locale) { - return NextResponse.redirect( - new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url) - ) - } - return NextResponse.next(); -} - -export const config = { - matcher: '/reset-password', -} diff --git a/pages/reset-password.js b/pages/reset-password.js index 7712d427..d70e7fcb 100644 --- a/pages/reset-password.js +++ b/pages/reset-password.js @@ -24,7 +24,20 @@ function ResetPassword({ url, intl }) { ); } -ResetPassword.getInitialProps = ({ url }) => ({ url }); +ResetPassword.getInitialProps = async ({ url, req, asPath, locale }) => { + if (req) { + const cookies = req.cookies; + const nextLocale = cookies['NEXT_LOCALE'] || 'en'; + if (nextLocale !== 'en' && locale !== nextLocale) { + return { + redirectTo: `/${nextLocale}${asPath}`, + redirectPermanent: false + } + } + } + + return { url }; +} ResetPassword.propTypes = { url: PropTypes.shape({}).isRequired,