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

fix: simplify session user check in protected procedures #2044

Merged
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/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
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
Loading