Skip to content

Instantly share code, notes, and snippets.

@adityajoshi12
Created September 4, 2021 14:41
Show Gist options
  • Save adityajoshi12/b3e2d794cefb9c8770fd3f18c25dc4b9 to your computer and use it in GitHub Desktop.
Save adityajoshi12/b3e2d794cefb9c8770fd3f18c25dc4b9 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { AuthChangeEvent, createClient, Session, SupabaseClient } from '@supabase/supabase-js';
import { environment } from "../environments/environment";
@Injectable({
providedIn: 'root'
})
export class SupabaseService {
private supabase: SupabaseClient;
constructor() {
this.supabase = createClient(environment.supabaseUrl, environment.supbaseKey);
}
get user() {
return this.supabase.auth.user();
}
get session() {
return this.supabase.auth.session();
}
get profile() {
return this.supabase
.from('profiles')
.select(`name`)
.eq('id', this.user?.id)
.single();
}
authChanges(callback: (event: AuthChangeEvent, session: Session | null) => void) {
return this.supabase.auth.onAuthStateChange(callback);
}
signUp(email: string, password: string) {
return this.supabase.auth.signUp({ email, password });
}
signOut() {
return this.supabase.auth.signOut();
}
createProfile(id: string, name: string) {
const update = {
name,
id
}
return this.supabase.from('profiles').upsert(update, {
returning: 'minimal',
});
}
login(email: string, password: string) {
return this.supabase.auth.signIn({ email, password });
}
getSession(): Session | null {
return this.supabase.auth.session()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment