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

chore: log error when user cookie is missing #1107

Merged
merged 2 commits into from
Nov 26, 2024
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
10 changes: 9 additions & 1 deletion apps/nestjs-backend/src/features/auth/guard/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { ExecutionContext } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard as PassportAuthGuard } from '@nestjs/passport';
import { AUTH_SESSION_COOKIE_NAME } from '../../../const';
import { ENSURE_LOGIN } from '../decorators/ensure-login.decorator';
import { IS_PUBLIC_KEY } from '../decorators/public.decorator';
import { ACCESS_TOKEN_STRATEGY_NAME } from '../strategies/constant';
@Injectable()
export class AuthGuard extends PassportAuthGuard(['session', ACCESS_TOKEN_STRATEGY_NAME]) {
private readonly logger = new Logger(AuthGuard.name);

constructor(private readonly reflector: Reflector) {
super();
}
Expand All @@ -21,6 +24,11 @@ export class AuthGuard extends PassportAuthGuard(['session', ACCESS_TOKEN_STRATE
context.getClass(),
]);

const cookie = context.switchToHttp().getRequest().headers.cookie;
if (!cookie?.includes(AUTH_SESSION_COOKIE_NAME)) {
this.logger.error('Auth session cookie is not found in request cookies');
}

if (isPublic) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Logger } from '@nestjs/common';
import { createAxios } from '@teable/openapi';
import type { ClsService } from 'nestjs-cls';
import type { IClsStore } from '../../types/cls';

export class ReadonlyService {
private readonly logger = new Logger(ReadonlyService.name);

protected axios;
constructor(clsService: ClsService<IClsStore>) {
this.axios = createAxios();
this.axios.interceptors.request.use((config) => {
config.headers.cookie = clsService.get('cookie');
const cookie = clsService.get('cookie');
config.headers.cookie = cookie;
config.baseURL = `http://localhost:${process.env.PORT}/api`;
if (!cookie) {
this.logger.error('Auth session cookie is not found in request headers');
}
return config;
});
}
Expand Down
8 changes: 7 additions & 1 deletion apps/nestjs-backend/src/share-db/share-db.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export class ShareDbAdapter extends ShareDb.DB {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (error: any | null, ids: string[], extra?: any) => void
) {
if (!options.cookie) {
this.logger.error(`No cookie found in options: ${JSON.stringify(options)}`);
}
try {
await this.cls.runWith(
{
Expand Down Expand Up @@ -205,14 +208,17 @@ export class ShareDbAdapter extends ShareDb.DB {
options: any,
callback: (err: unknown, data?: Snapshot) => void
) {
if (!options.agentCustom.cookie) {
this.logger.error(`No cookie found in options agentCustom: ${JSON.stringify(options)}`);
}
await this.cls.runWith(
{
...this.cls.get(),
cookie: options.agentCustom.cookie,
shareViewId: options.agentCustom.shareId,
},
async () => {
this.getSnapshotBulk(collection, [id], projection, options, (err, data) => {
return this.getSnapshotBulk(collection, [id], projection, options, (err, data) => {
if (err) {
callback(err);
} else {
Expand Down
Loading