diff --git a/.changeset/itchy-apricots-compare.md b/.changeset/itchy-apricots-compare.md new file mode 100644 index 0000000000..69fc34e676 --- /dev/null +++ b/.changeset/itchy-apricots-compare.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +fix: simplify session user check in protected procedures diff --git a/.changeset/thin-hats-reflect.md b/.changeset/thin-hats-reflect.md new file mode 100644 index 0000000000..386ff6715a --- /dev/null +++ b/.changeset/thin-hats-reflect.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +start-database.sh script gets DB_NAME and DB_CONTAINER_NAME based off of DATABASE_URL in .env diff --git a/cli/template/extras/src/server/api/trpc-app/with-auth-db.ts b/cli/template/extras/src/server/api/trpc-app/with-auth-db.ts index 00d924faa8..923751a8e3 100644 --- a/cli/template/extras/src/server/api/trpc-app/with-auth-db.ts +++ b/cli/template/extras/src/server/api/trpc-app/with-auth-db.ts @@ -121,7 +121,7 @@ export const publicProcedure = t.procedure.use(timingMiddleware); export const protectedProcedure = t.procedure .use(timingMiddleware) .use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/cli/template/extras/src/server/api/trpc-app/with-auth.ts b/cli/template/extras/src/server/api/trpc-app/with-auth.ts index 755b973c29..5f798e9dd6 100644 --- a/cli/template/extras/src/server/api/trpc-app/with-auth.ts +++ b/cli/template/extras/src/server/api/trpc-app/with-auth.ts @@ -118,7 +118,7 @@ export const publicProcedure = t.procedure.use(timingMiddleware); export const protectedProcedure = t.procedure .use(timingMiddleware) .use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/cli/template/extras/src/server/api/trpc-pages/with-auth-db.ts b/cli/template/extras/src/server/api/trpc-pages/with-auth-db.ts index 8b4f3632a7..588ddf2c49 100644 --- a/cli/template/extras/src/server/api/trpc-pages/with-auth-db.ts +++ b/cli/template/extras/src/server/api/trpc-pages/with-auth-db.ts @@ -148,7 +148,7 @@ export const publicProcedure = t.procedure.use(timingMiddleware); export const protectedProcedure = t.procedure .use(timingMiddleware) .use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/cli/template/extras/src/server/api/trpc-pages/with-auth.ts b/cli/template/extras/src/server/api/trpc-pages/with-auth.ts index 7fe732be6b..d64d31b25b 100644 --- a/cli/template/extras/src/server/api/trpc-pages/with-auth.ts +++ b/cli/template/extras/src/server/api/trpc-pages/with-auth.ts @@ -146,7 +146,7 @@ export const publicProcedure = t.procedure.use(timingMiddleware); export const protectedProcedure = t.procedure .use(timingMiddleware) .use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/cli/template/extras/start-database/mysql.sh b/cli/template/extras/start-database/mysql.sh index 731725c31c..65789a3c7b 100755 --- a/cli/template/extras/start-database/mysql.sh +++ b/cli/template/extras/start-database/mysql.sh @@ -9,7 +9,13 @@ # On Linux and macOS you can run this script directly - `./start-database.sh` -DB_CONTAINER_NAME="project1-mysql" +set -a +source .env + +DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') +DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') +DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}') +DB_CONTAINER_NAME="$DB_NAME-postgres" if ! [ -x "$(command -v docker)" ]; then echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" @@ -32,13 +38,6 @@ if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then exit 0 fi -# import env variables from .env -set -a -source .env - -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') - if [ "$DB_PASSWORD" == "password" ]; then echo "You are using the default database password" read -p "Should we generate a random password for you? [y/N]: " -r REPLY @@ -54,6 +53,6 @@ fi docker run -d \ --name $DB_CONTAINER_NAME \ -e MYSQL_ROOT_PASSWORD="$DB_PASSWORD" \ - -e MYSQL_DATABASE=project1 \ + -e MYSQL_DATABASE="$DB_NAME" \ -p "$DB_PORT":3306 \ docker.io/mysql && echo "Database container '$DB_CONTAINER_NAME' was successfully created" diff --git a/cli/template/extras/start-database/postgres.sh b/cli/template/extras/start-database/postgres.sh index 378f19e23c..b162abdfe3 100755 --- a/cli/template/extras/start-database/postgres.sh +++ b/cli/template/extras/start-database/postgres.sh @@ -9,7 +9,14 @@ # On Linux and macOS you can run this script directly - `./start-database.sh` -DB_CONTAINER_NAME="project1-postgres" +# import env variables from .env +set -a +source .env + +DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') +DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') +DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}') +DB_CONTAINER_NAME="$DB_NAME-postgres" if ! [ -x "$(command -v docker)" ]; then echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" @@ -32,13 +39,6 @@ if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then exit 0 fi -# import env variables from .env -set -a -source .env - -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') - if [ "$DB_PASSWORD" = "password" ]; then echo "You are using the default database password" read -p "Should we generate a random password for you? [y/N]: " -r REPLY @@ -55,6 +55,6 @@ docker run -d \ --name $DB_CONTAINER_NAME \ -e POSTGRES_USER="postgres" \ -e POSTGRES_PASSWORD="$DB_PASSWORD" \ - -e POSTGRES_DB=project1 \ + -e POSTGRES_DB="$DB_NAME" \ -p "$DB_PORT":5432 \ docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created" diff --git a/www/src/components/docs/openSourceAppList.tsx b/www/src/components/docs/openSourceAppList.tsx index 4cb6488243..80d2adc701 100644 --- a/www/src/components/docs/openSourceAppList.tsx +++ b/www/src/components/docs/openSourceAppList.tsx @@ -330,6 +330,13 @@ const projects: App[] = [ linkName: "Squeak", link: "https://playsqueak.com/", }, + { + description: "Ray - A full stack admin starter", + repoName: "koujialong/ray-admin", + repo: "https://github.com/koujialong/ray-admin", + linkName: "Ray", + link: "https://koujialong-ray.vercel.app/", + }, ]; export default function OpenSourceAppList({ diff --git a/www/src/pages/ar/usage/next-auth.md b/www/src/pages/ar/usage/next-auth.md index 8d65fb8a71..4f245fff12 100644 --- a/www/src/pages/ar/usage/next-auth.md +++ b/www/src/pages/ar/usage/next-auth.md @@ -100,7 +100,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/trpc/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/en/folder-structure-app.mdx b/www/src/pages/en/folder-structure-app.mdx index 3005ddb419..e0f3d77b85 100644 --- a/www/src/pages/en/folder-structure-app.mdx +++ b/www/src/pages/en/folder-structure-app.mdx @@ -96,14 +96,14 @@ The `db` folder contains the Drizzle client and schema. Note that drizzle also r #### `src/server/db/index.ts` -The `index.ts` file is used to instantiate the Drizzle client at global scope. See [Drizzle usage](usage/drizzle#drizzle-client) and [best practices for using Drizzle with Next.js](https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices) for more information. +The `index.ts` file is used to instantiate the Drizzle client at global scope. See [Drizzle usage](usage/drizzle#drizzle-client) for more information.
#### `src/server/db/schema.ts` -The `schema.ts` file is used to define the database schema. See [Drizzle usage](usage/drizzle#drizzle-client) and [best practices for using Drizzle with Next.js](https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices) for more information. +The `schema.ts` file is used to define the database schema. See [Drizzle usage](usage/drizzle#drizzle-client) and [Drizzle schema docs](https://orm.drizzle.team/docs/sql-schema-declaration) for more information.
diff --git a/www/src/pages/en/folder-structure-pages.mdx b/www/src/pages/en/folder-structure-pages.mdx index 47d7c35a6f..a9c49aed4d 100644 --- a/www/src/pages/en/folder-structure-pages.mdx +++ b/www/src/pages/en/folder-structure-pages.mdx @@ -96,14 +96,14 @@ The `db` folder contains the Drizzle client and schema. Note that drizzle also r #### `src/server/db/index.ts` -The `index.ts` file is used to instantiate the Drizzle client at global scope. See [Drizzle usage](usage/drizzle#drizzle-client) and [best practices for using Drizzle with Next.js](https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices) for more information. +The `index.ts` file is used to instantiate the Drizzle client at global scope. See [Drizzle usage](usage/drizzle#drizzle-client) for more information.
#### `src/server/db/schema.ts` -The `schema.ts` file is used to define the database schema. See [Drizzle usage](usage/drizzle#drizzle-client) and [best practices for using Drizzle with Next.js](https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices) for more information. +The `schema.ts` file is used to define the database schema. See [Drizzle usage](usage/drizzle#drizzle-client) and [Drizzle schema docs](https://orm.drizzle.team/docs/sql-schema-declaration) for more information.
diff --git a/www/src/pages/en/usage/next-auth-app-router.mdx b/www/src/pages/en/usage/next-auth-app-router.mdx index fcf1031d6f..b610105d13 100644 --- a/www/src/pages/en/usage/next-auth-app-router.mdx +++ b/www/src/pages/en/usage/next-auth-app-router.mdx @@ -77,7 +77,7 @@ export const createTRPCContext = async (opts: { headers: Headers }) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure .use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/en/usage/next-auth-pages.mdx b/www/src/pages/en/usage/next-auth-pages.mdx index 2c26579933..7d21099805 100644 --- a/www/src/pages/en/usage/next-auth-pages.mdx +++ b/www/src/pages/en/usage/next-auth-pages.mdx @@ -116,7 +116,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ @@ -175,8 +175,8 @@ Usage of NextAuth.js with Next.js middleware [requires the use of the JWT sessio issues. -After switching to the JWT session strategy. Make sure to update the `session` callback in `src/server/auth.ts`. -The `user` object will be `undefined`. Instead, retrieve the user's ID from the `token` object. +After switching to the JWT session strategy. Make sure to update the `session` callback in `src/server/auth.ts`. +The `user` object will be `undefined`. Instead, retrieve the user's ID from the `token` object. I.e.: ```diff:server/auth.ts diff --git a/www/src/pages/es/usage/next-auth.md b/www/src/pages/es/usage/next-auth.md index ffc7be4b9b..3064d4924d 100644 --- a/www/src/pages/es/usage/next-auth.md +++ b/www/src/pages/es/usage/next-auth.md @@ -100,7 +100,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/trpc/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/fr/usage/next-auth.mdx b/www/src/pages/fr/usage/next-auth.mdx index cb263a78f6..6978124152 100644 --- a/www/src/pages/fr/usage/next-auth.mdx +++ b/www/src/pages/fr/usage/next-auth.mdx @@ -126,7 +126,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/ja/usage/next-auth.md b/www/src/pages/ja/usage/next-auth.md index 5cc13c61df..2b2464f54a 100644 --- a/www/src/pages/ja/usage/next-auth.md +++ b/www/src/pages/ja/usage/next-auth.md @@ -123,7 +123,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/no/usage/next-auth.md b/www/src/pages/no/usage/next-auth.md index a1deed32d7..b9d35c302b 100644 --- a/www/src/pages/no/usage/next-auth.md +++ b/www/src/pages/no/usage/next-auth.md @@ -120,7 +120,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/pl/usage/next-auth.md b/www/src/pages/pl/usage/next-auth.md index d4a65135f3..1c32827f15 100644 --- a/www/src/pages/pl/usage/next-auth.md +++ b/www/src/pages/pl/usage/next-auth.md @@ -120,7 +120,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/pt/usage/next-auth.md b/www/src/pages/pt/usage/next-auth.md index 8c68589e43..af24f239cf 100644 --- a/www/src/pages/pt/usage/next-auth.md +++ b/www/src/pages/pt/usage/next-auth.md @@ -123,7 +123,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/trpc/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/ru/usage/next-auth.md b/www/src/pages/ru/usage/next-auth.md index f6c19e5bd1..8c48cb504c 100644 --- a/www/src/pages/ru/usage/next-auth.md +++ b/www/src/pages/ru/usage/next-auth.md @@ -123,7 +123,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/uk/usage/next-auth.mdx b/www/src/pages/uk/usage/next-auth.mdx index c311d37b2a..abc01d97ef 100644 --- a/www/src/pages/uk/usage/next-auth.mdx +++ b/www/src/pages/uk/usage/next-auth.mdx @@ -126,7 +126,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({ diff --git a/www/src/pages/zh-hans/usage/next-auth.mdx b/www/src/pages/zh-hans/usage/next-auth.mdx index c17c4d21bc..c7be82a3cc 100644 --- a/www/src/pages/zh-hans/usage/next-auth.mdx +++ b/www/src/pages/zh-hans/usage/next-auth.mdx @@ -126,7 +126,7 @@ export const createContext = async (opts: CreateNextContextOptions) => { ```ts:server/api/trpc.ts export const protectedProcedure = t.procedure.use(({ ctx, next }) => { - if (!ctx.session || !ctx.session.user) { + if (!ctx.session?.user) { throw new TRPCError({ code: "UNAUTHORIZED" }); } return next({