Skip to content

Commit

Permalink
chore: move away from await in local-auth-guard
Browse files Browse the repository at this point in the history
  • Loading branch information
pratapalakshmi committed Dec 13, 2024
1 parent 56e8ac6 commit e4ec6f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
19 changes: 7 additions & 12 deletions services/workflows-service/src/auth/local/local-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { Injectable, ExecutionContext } from '@nestjs/common';
import { CanActivate, Injectable, ExecutionContext } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { SupabaseService } from '../../supabase/supabase.service';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
export class LocalAuthGuard extends AuthGuard('local') implements CanActivate {
constructor(private readonly supabaseService: SupabaseService) {
super();
}

async canActivate(context: ExecutionContext): Promise<boolean> {
const result = (await super.canActivate(context)) as boolean;
canActivate(context: ExecutionContext): Promise<boolean> {
const result = super.canActivate(context) as boolean;
const request = context.switchToHttp().getRequest();

// Use the injected service
if (result && request.user) {
const fullUrl = request.protocol + '://' + request.get('host') + request.originalUrl;
await this.supabaseService.logSignIn(fullUrl);
this.supabaseService.logSignIn(fullUrl);
}

// Ensure the session is logged in (if using sessions)
await super.logIn(request);

return result;
super.logIn(request);
return Promise.resolve(result);
}
}
44 changes: 24 additions & 20 deletions services/workflows-service/src/supabase/supabase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,34 @@ export class SupabaseService implements ISupabaseService {
private readonly configService: ConfigService,
private readonly SentryService: SentryService,
) {
const telemetryEnabled = this.configService.get('TELEMETRY_ENABLED');
const supabaseUrl = this.configService.get<string>('TELEMETRY_SUPABASE_URL');
const supabaseApiKey = this.configService.get<string>('TELEMETRY_SUPABASE_API_KEY');

if (!supabaseUrl || !supabaseApiKey) {
throw new Error('Supabase URL or API key is missing in configuration');
} else {
this.supabaseClient = createClient(supabaseUrl, supabaseApiKey, {
db: { schema: 'public' },
});
this.logger.log('Supabase client initialized.');
// This log is created as part of the entrypoint script
// which gather details of the infrastructure.
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const infradata = require('/tmp/infra.json');
void this.logInfraData(infradata);
} catch (error: Error | any) {
if (error.code === 'MODULE_NOT_FOUND') {
this.logger.error(`file not present: ${error.message}`);
this.SentryService.captureException(error.message);
} else {
this.logger.error(`Exception infra data not present: ${error.message}`);
if (telemetryEnabled) {
if (!supabaseUrl || !supabaseApiKey) {
throw new Error('Supabase URL or API key is missing in configuration');
} else {
this.supabaseClient = createClient(supabaseUrl, supabaseApiKey, {
db: { schema: 'public' },
});
this.logger.log('Supabase client initialized.');
// This log is created as part of the entrypoint script
// which gather details of the infrastructure.
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const infradata = require('/tmp/infra.json');
void this.logInfraData(infradata);
} catch (error: Error | any) {
if (error.code === 'MODULE_NOT_FOUND') {
this.logger.error(`file not present: ${error.message}`);
this.SentryService.captureException(error.message);
} else {
this.logger.error(`Exception infra data not present: ${error.message}`);
}
}
}
} else {
this.logger.log('Telemetry disabled.');
}
}

Expand Down

0 comments on commit e4ec6f3

Please sign in to comment.