Last active
July 3, 2024 09:35
-
-
Save kluplau/1f40441a424ccedd918a52624c2776a9 to your computer and use it in GitHub Desktop.
Updated supabase.ts based on article by Andrii Shupta (https://blog.andriishupta.dev/setup-supabase-with-nestjs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Inject, Injectable, Logger, Scope } from "@nestjs/common"; | |
import { Request } from "express"; | |
import { REQUEST } from "@nestjs/core"; | |
import { ConfigService } from "@nestjs/config"; | |
import { createClient, SupabaseClient } from "@supabase/supabase-js"; | |
import { ExtractJwt } from "passport-jwt"; | |
@Injectable({ scope: Scope.REQUEST }) | |
export class Supabase { | |
private readonly logger = new Logger(Supabase.name); | |
private clientInstance: SupabaseClient; | |
constructor( | |
@Inject(REQUEST) private readonly request: Request, | |
private readonly configService: ConfigService, | |
) {} | |
getClient() { | |
this.logger.log("getting supabase client..."); | |
if (this.clientInstance) { | |
this.logger.log("client exists - returning for current Scope.REQUEST"); | |
return this.clientInstance; | |
} | |
this.logger.log("initialising new supabase client for new Scope.REQUEST"); | |
this.clientInstance = createClient( | |
this.configService.get("SUPABASE_URL"), | |
this.configService.get("SUPABASE_KEY"), | |
{ | |
auth: { | |
autoRefreshToken: true, | |
detectSessionInUrl: false, | |
}, | |
global: { | |
headers: { | |
Authorization: `Bearer ${ExtractJwt.fromAuthHeaderAsBearerToken()(this.request)}`, | |
}, | |
}, | |
}, | |
); | |
this.logger.log("auth has been set!"); | |
return this.clientInstance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Upgraded to Supabase version 2 from 1, using the upgrade guide.