``` import { Injectable } from '@angular/core'; im...
# javascript
k
Copy code
import { Injectable } from '@angular/core';
import {AuthChangeEvent, createClient, Session, SupabaseClient} from '@supabase/supabase-js';
import { environment } from 'src/environments/environment';
export interface IUser {
  username: string;
  email: string;
}
@Injectable({
  providedIn: 'root'
})
export class AuthService {
  private supabase: SupabaseClient;

  constructor() {
    this.supabase = createClient(environment.supabaseUrl, environment.supabaseKey);
  }
  get user() {
    return this.supabase.auth.user();
  }

  get session() {
    return this.supabase.auth.session();
  }
  get profile() {
    return this.supabase
      .from('profiles')
      .select(`username, email`)
      .eq('id', this.user?.id)
      .single();
  }
  authChanges(callback: (event: AuthChangeEvent, session: Session | null) => void) {
    return this.supabase.auth.onAuthStateChange(callback);
  }
  signIn(email: string, password: string) {
    return this.supabase.auth.signIn({ "email": email, "password": password });
  }
  signUp(email: string, password: string) {
    return this.supabase.auth.signUp({ email, password })
  }
  signOut() {
    return this.supabase.auth.signOut();
  }
  updateProfile(profile: IUser) {
    const update = {
      ...profile,
      id: this.user?.id,
      updated_at: new Date()
    }
    return this.supabase.from('profiles').upsert(update, {
      returning: 'minimal',
    });
  }
  downLoadImage(path: string) {
    return this.supabase.storage.from('avatars').download(path);
  }
  uploadAvatar(filePath: string, file: File) {
    return this.supabase.storage
      .from('avatars')
      .upload(filePath, file);
  }
}