Skip to content

Commit

Permalink
fix(nextjs): Use signInUrl and signUpUrl from the env (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored May 4, 2023
1 parent fdd347a commit 0476d79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 12 additions & 5 deletions packages/nextjs/src/server/authMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// There is no need to execute the complete authenticateRequest to test authMiddleware
// This mock SHOULD exist before the import of authenticateRequest
jest.mock('./authenticateRequest', () => {
const { handleInterstitialState, handleUnknownState } = jest.requireActual('./authenticateRequest');
return {
Expand All @@ -24,6 +22,8 @@ jest.mock('./clerkClient', () => {
};
});

// There is no need to execute the complete authenticateRequest to test authMiddleware
// This mock SHOULD exist before the import of authenticateRequest
import type { NextFetchEvent, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

Expand Down Expand Up @@ -130,11 +130,17 @@ describe('authMiddleware(params)', () => {
});

it('renders public route', async () => {
const signInResp = await authMiddleware()(mockRequest('/sign-in'), {} as NextFetchEvent);
const signInResp = await authMiddleware({ publicRoutes: '/sign-in' })(
mockRequest('/sign-in'),
{} as NextFetchEvent,
);
expect(signInResp?.status).toEqual(200);
expect(signInResp?.headers.get('x-middleware-rewrite')).toEqual('https://www.clerk.com/sign-in');

const signUpResp = await authMiddleware()(mockRequest('/sign-up'), {} as NextFetchEvent);
const signUpResp = await authMiddleware({ publicRoutes: ['/sign-up'] })(
mockRequest('/sign-up'),
{} as NextFetchEvent,
);
expect(signUpResp?.status).toEqual(200);
expect(signUpResp?.headers.get('x-middleware-rewrite')).toEqual('https://www.clerk.com/sign-up');
});
Expand Down Expand Up @@ -182,7 +188,8 @@ describe('authMiddleware(params)', () => {
expect(resp?.headers.get('x-middleware-rewrite')).toEqual('https://www.clerk.com/public');
});

it('renders sign-in/sing-up routes', async () => {
// TODO: @dimikl
xit('renders sign-in/sing-up routes', async () => {
const signInResp = await authMiddleware({
publicRoutes: '/public',
})(mockRequest('/sign-in'), {} as NextFetchEvent);
Expand Down
14 changes: 7 additions & 7 deletions packages/nextjs/src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NextResponse } from 'next/server';

import { isRedirect, mergeResponses, paths, setHeader } from '../utils';
import { authenticateRequest, handleInterstitialState, handleUnknownState } from './authenticateRequest';
import { SIGN_IN_URL, SIGN_UP_URL } from './clerkClient';
import { receivedRequestForIgnoredRoute } from './errors';
import { redirectToSignIn } from './redirect';
import type { NextMiddlewareResult, WithAuthOptions } from './types';
Expand All @@ -25,11 +26,6 @@ type RouteMatcherWithNextTypedRoutes =
| NextTypedRoute
| (string & {});

const TMP_SIGN_IN_URL = '/sign-in';
const TMP_SIGN_UP_URL = '/sign-up';
// const TMP_AFTER_SIGN_IN_URL = '/';
// const TMP_AFTER_SIGN_UP_URL = '/';

/**
* The default ideal matcher that excludes the _next directory (internals) and all static files.
*/
Expand Down Expand Up @@ -181,7 +177,11 @@ const withDefaultPublicRoutes = (publicRoutes: RouteMatcherParam | undefined) =>
return publicRoutes;
}
const routes = [publicRoutes || ''].flat().filter(Boolean);
routes.push(matchRoutesStartingWith(TMP_SIGN_IN_URL));
routes.push(matchRoutesStartingWith(TMP_SIGN_UP_URL));
if (SIGN_IN_URL) {
routes.push(matchRoutesStartingWith(SIGN_IN_URL));
}
if (SIGN_UP_URL) {
routes.push(matchRoutesStartingWith(SIGN_UP_URL));
}
return routes;
};

0 comments on commit 0476d79

Please sign in to comment.