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

feat: warn for incompat nextauth url and port #1596

Closed
Closed
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
5 changes: 5 additions & 0 deletions .changeset/cuddly-suns-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

feat: add a warning if nextuath url doens't match the port in dev
7 changes: 1 addition & 6 deletions cli/src/helpers/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const createProject = async ({
scopedAppName,
noInstall,
appRouter,
packages,
});

// Install the selected packages
Expand All @@ -55,12 +56,6 @@ export const createProject = async ({

// Select necessary _app,index / layout,page files
if (appRouter) {
// Replace next.config
fs.copyFileSync(
path.join(PKG_ROOT, "template/extras/config/next-config-appdir.mjs"),
path.join(projectDir, "next.config.mjs")
);

selectLayoutFile({ projectDir, packages });
selectPageFile({ projectDir, packages });
} else {
Expand Down
11 changes: 11 additions & 0 deletions cli/src/helpers/scaffoldProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const scaffoldProject = async ({
projectDir,
pkgManager,
noInstall,
appRouter,
packages,
}: InstallerOptions) => {
const srcDir = path.join(PKG_ROOT, "template/base");

Expand Down Expand Up @@ -90,6 +92,15 @@ export const scaffoldProject = async ({
path.join(projectDir, ".gitignore")
);

// Select next.config.mjs
const configDir = path.join(PKG_ROOT, "template/extras/config/next-config");
let filename = packages?.nextAuth.inUse ? "with-auth-" : "with-";
filename += appRouter ? "appdir.mjs" : "pagedir.mjs";
fs.copyFileSync(
path.join(configDir, filename),
path.join(projectDir, "next.config.mjs")
);

const scaffoldedName =
projectName === "." ? "App" : chalk.cyan.bold(projectName);

Expand Down
2 changes: 2 additions & 0 deletions cli/template/base/src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const env = createEnv({
*/
server: {
NODE_ENV: z.enum(["development", "test", "production"]),
PORT: z.coerce.number().default(3000),
},

/**
Expand All @@ -25,6 +26,7 @@ export const env = createEnv({
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
PORT: process.env.PORT,
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
},
/**
Expand Down
21 changes: 21 additions & 0 deletions cli/template/extras/config/next-config/with-auth-appdir.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
import { env } from "./src/env.mjs";

// Warn if local development URL doesn't match with the active port
const nextAuthPort = new URL(env.NEXTAUTH_URL).port;
if (env.NODE_ENV === "development" && nextAuthPort !== String(env.PORT)) {
throw new Error(
[
`❗ NEXTAUTH_URL (${env.NEXTAUTH_URL}) doesn't match with PORT (${env.PORT}).`,
`Either update your the URL in your .env file or make sure to run your dev server on port ${nextAuthPort}.`,
].join(" ")
);
}

/** @type {import("next").NextConfig} */
const config = {};

export default config;
33 changes: 33 additions & 0 deletions cli/template/extras/config/next-config/with-auth-pagedir.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
import { env } from "./src/env.mjs";

// Warn if local development URL doesn't match with the active port
const nextAuthPort = new URL(env.NEXTAUTH_URL).port;
if (env.NODE_ENV === "development" && nextAuthPort !== String(env.PORT)) {
throw new Error(
[
`❗ NEXTAUTH_URL (${env.NEXTAUTH_URL}) doesn't match with PORT (${env.PORT}).`,
`Either update your the URL in your .env file or make sure to run your dev server on port ${nextAuthPort}.`,
].join(" ")
);
}

/** @type {import("next").NextConfig} */
const config = {
reactStrictMode: true,

/**
* If you are using `appDir` then you must comment the below `i18n` config out.
*
* @see https://github.com/vercel/next.js/issues/41980
*/
i18n: {
locales: ["en"],
defaultLocale: "en",
},
};

export default config;
2 changes: 2 additions & 0 deletions cli/template/extras/src/env/with-auth-db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const env = createEnv({
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),
PORT: z.coerce.number().default(3000),
NEXTAUTH_SECRET:
process.env.NODE_ENV === "production"
? z.string()
Expand Down Expand Up @@ -49,6 +50,7 @@ export const env = createEnv({
runtimeEnv: {
DATABASE_URL: process.env.DATABASE_URL,
NODE_ENV: process.env.NODE_ENV,
PORT: process.env.PORT,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
Expand Down
2 changes: 2 additions & 0 deletions cli/template/extras/src/env/with-auth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const env = createEnv({
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),
PORT: z.coerce.number().default(3000),
NEXTAUTH_SECRET:
process.env.NODE_ENV === "production"
? z.string()
Expand Down Expand Up @@ -41,6 +42,7 @@ export const env = createEnv({
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
PORT: process.env.PORT,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
Expand Down
2 changes: 2 additions & 0 deletions cli/template/extras/src/env/with-db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const env = createEnv({
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),
PORT: z.coerce.number().default(3000),
},

/**
Expand All @@ -35,6 +36,7 @@ export const env = createEnv({
runtimeEnv: {
DATABASE_URL: process.env.DATABASE_URL,
NODE_ENV: process.env.NODE_ENV,
PORT: process.env.PORT,
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
},
/**
Expand Down
Loading