-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Description
Got error while using example from documentation
auth.middleware.ts
import {
MiddlewareOptions,
MiddlewareResponse,
TRPCMiddleware,
} from 'nestjs-trpc';
import { Inject, Injectable } from '@nestjs/common';
import { AuthService } from './auth.service';
import { TRPCError } from '@trpc/server';
import type { Context } from 'nestjs-trpc/types';
@Injectable()
export class AuthMiddleware implements TRPCMiddleware {
constructor(@Inject(AuthService) private readonly authService: AuthService) {}
async use(opts: MiddlewareOptions<Context>): Promise<MiddlewareResponse> {
const { req, next } = opts;
const session = await this.authService.getSession({ req });
if(session == null) {
throw new TRPCError("No session found.", "UNAUTHORIZED");
}
return next({
ctx: {
auth: {
userId: session.user.id
}
}
})
}
}
ctrhub
Metadata
Metadata
Assignees
Labels
No labels