Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge authored Jan 12, 2025
2 parents cbad9bc + 571cba7 commit 897ae87
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-apricots-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

fix: simplify session user check in protected procedures
5 changes: 5 additions & 0 deletions .changeset/thin-hats-reflect.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion cli/template/extras/src/server/api/trpc-app/with-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion cli/template/extras/src/server/api/trpc-pages/with-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
17 changes: 8 additions & 9 deletions cli/template/extras/start-database/mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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"
18 changes: 9 additions & 9 deletions cli/template/extras/start-database/postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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"
7 changes: 7 additions & 0 deletions www/src/components/docs/openSourceAppList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/ar/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions www/src/pages/en/folder-structure-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

</div>
<div data-components="drizzle">

#### `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.

</div>
<div data-components="trpc">
Expand Down
4 changes: 2 additions & 2 deletions www/src/pages/en/folder-structure-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

</div>
<div data-components="drizzle">

#### `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.

</div>
<div data-components="trpc">
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/en/usage/next-auth-app-router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions www/src/pages/en/usage/next-auth-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -175,8 +175,8 @@ Usage of NextAuth.js with Next.js middleware [requires the use of the JWT sessio
issues.
</Callout>

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
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/es/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/fr/usage/next-auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/ja/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/no/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/pl/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/pt/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/ru/usage/next-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/uk/usage/next-auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/zh-hans/usage/next-auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 897ae87

Please sign in to comment.