File size: 3,030 Bytes
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8153
 
 
 
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Injectable, OnModuleInit, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
  createClient,
  SupabaseClient,
  AuthResponse,
  User,
} from '@supabase/supabase-js';

@Injectable()
export class SupabaseService implements OnModuleInit {
  private readonly logger = new Logger(SupabaseService.name);
  private client: SupabaseClient;

  constructor(private configService: ConfigService) {}

  onModuleInit() {
    const supabaseUrl = this.configService.get<string>('database.supabaseUrl');
    const supabaseKey = this.configService.get<string>(
      'database.supabaseServiceRoleKey',
    );

    if (!supabaseUrl || !supabaseKey) {
      throw new Error('Supabase URL and Service Role Key must be provided');
    }

    // Temporary log to verify key role
    try {
      const payload = JSON.parse(
        Buffer.from(supabaseKey.split('.')[1], 'base64').toString(),
      );
      this.logger.log('Supabase key role: ' + payload.role);
    } catch {
      this.logger.warn('Could not decode Supabase key');
    }

    this.client = createClient(supabaseUrl, supabaseKey, {
      auth: {
        autoRefreshToken: false,
        persistSession: false,
      },
    });

    this.logger.log('Supabase client initialized');
  }

  getClient(): SupabaseClient {
    return this.client;
  }

  from(table: string) {
    return this.client.from(table);
  }

  rpc<T = any>(fn: string, params?: Record<string, any>) {
    return this.client.rpc(fn, params) as unknown as Promise<{ data: T | null; error: Error | null }>;
  }

  storage() {
    return this.client.storage;
  }

  // Auth methods
  async signUp(email: string, password: string): Promise<AuthResponse> {
    this.logger.log('Using admin.createUser for: ' + email);
    const { data, error } = await this.client.auth.admin.createUser({
      email,
      password,
      email_confirm: true,
    });
    return {
      data: { user: data?.user ?? null, session: null },
      error,
    } as AuthResponse;
  }

  async signIn(email: string, password: string): Promise<AuthResponse> {
    return this.client.auth.signInWithPassword({ email, password });
  }

  async signOut(token: string): Promise<{ error: Error | null }> {
    return this.client.auth.admin.signOut(token);
  }

  async getUser(
    token: string,
  ): Promise<{ data: { user: User | null }; error: Error | null }> {
    return this.client.auth.getUser(token);
  }

  async refreshToken(refreshToken: string): Promise<AuthResponse> {
    return this.client.auth.refreshSession({ refresh_token: refreshToken });
  }

  async adminGetUserById(
    userId: string,
  ): Promise<{ data: { user: User | null }; error: Error | null }> {
    return this.client.auth.admin.getUserById(userId);
  }

  // Health check
  async healthCheck(): Promise<boolean> {
    try {
      const { error } = await this.client
        .from('profiles')
        .select('count', { count: 'exact', head: true });
      return !error;
    } catch {
      return false;
    }
  }
}