Skip to content

Commit

Permalink
fix: simplify session user check in protected procedures (#2044)
Browse files Browse the repository at this point in the history
Co-authored-by: Julius Marminge <[email protected]>
  • Loading branch information
ImBIOS and juliusmarminge authored Jan 12, 2025
1 parent d22a0bf commit cd4cf33
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 18 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
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
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
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 cd4cf33

Please sign in to comment.