mirror of
https://github.com/supabase/supabase.git
synced 2026-05-06 08:56:46 -04:00
d8bd6b047c
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated examples and guides to use Supabase publishable (client) keys instead of anon keys for client-side usage across frameworks and platforms. * Renamed environment variable examples and .env templates to reflect publishable key naming. * Adjusted sample requests and client-init examples to send/use the publishable key via the apikey header where applicable. * Updated references from service_role to secret for server-side credential guidance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: fadymak <fady@fadymak.com>
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import { Injectable } from '@angular/core'
|
|
import { LoadingController, ToastController } from '@ionic/angular'
|
|
import { AuthChangeEvent, createClient, Session, SupabaseClient } from '@supabase/supabase-js'
|
|
import { environment } from '../environments/environment'
|
|
|
|
export interface Profile {
|
|
username: string
|
|
website: string
|
|
avatar_url: string
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class SupabaseService {
|
|
private supabase: SupabaseClient
|
|
|
|
constructor(
|
|
private loadingCtrl: LoadingController,
|
|
private toastCtrl: ToastController
|
|
) {
|
|
this.supabase = createClient(environment.supabaseUrl, environment.supabasePublishableKey)
|
|
}
|
|
|
|
get user() {
|
|
return this.supabase.auth.getUser().then(({ data }) => data?.user)
|
|
}
|
|
|
|
get session() {
|
|
return this.supabase.auth.getClaims().then(async ({ data }) => {
|
|
if (!data?.claims) {
|
|
return null
|
|
}
|
|
|
|
const { data: userData } = await this.supabase.auth.getUser()
|
|
return userData?.user ? ({ user: userData.user } as Session) : null
|
|
})
|
|
}
|
|
|
|
get profile() {
|
|
return this.user
|
|
.then((user) => user?.id)
|
|
.then((id) =>
|
|
this.supabase.from('profiles').select(`username, website, avatar_url`).eq('id', id).single()
|
|
)
|
|
}
|
|
|
|
authChanges(callback: (event: AuthChangeEvent, session: Session | null) => void) {
|
|
return this.supabase.auth.onAuthStateChange(callback)
|
|
}
|
|
|
|
signIn(email: string) {
|
|
return this.supabase.auth.signInWithOtp({ email })
|
|
}
|
|
|
|
signOut() {
|
|
return this.supabase.auth.signOut()
|
|
}
|
|
|
|
async updateProfile(profile: Profile) {
|
|
const user = await this.user
|
|
const update = {
|
|
...profile,
|
|
id: user?.id,
|
|
updated_at: new Date(),
|
|
}
|
|
|
|
return this.supabase.from('profiles').upsert(update)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
async createNotice(message: string) {
|
|
const toast = await this.toastCtrl.create({ message, duration: 5000 })
|
|
await toast.present()
|
|
}
|
|
|
|
createLoader() {
|
|
return this.loadingCtrl.create()
|
|
}
|
|
}
|