|
| 1 | +import type { User as SupabaseUser } from '@supabase/supabase-js'; |
| 2 | +import { supabaseClient, manageSupabaseError } from '@modules/supabase'; |
| 3 | +import type { AuthRepository } from '../domain/auth.repository'; |
| 4 | +import type { |
| 5 | + SignUpPayload, |
| 6 | + SignUpResponse, |
| 7 | + SignInPayload, |
| 8 | + SignInResponse, |
| 9 | + AuthUser, |
| 10 | + AuthStateChangeCallback, |
| 11 | + AuthStateUnsubscribe, |
| 12 | +} from '../domain/auth.model'; |
| 13 | + |
| 14 | +function pickString(value: unknown): string | null { |
| 15 | + return typeof value === 'string' && value.length > 0 ? value : null; |
| 16 | +} |
| 17 | + |
| 18 | +function supabaseUserToEntity(user: SupabaseUser | null): AuthUser | null { |
| 19 | + if (!user) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + const metadata = (user.user_metadata ?? {}) as Record<string, unknown>; |
| 24 | + |
| 25 | + return { |
| 26 | + id: user.id, |
| 27 | + email: user.email ?? '', |
| 28 | + displayName: |
| 29 | + pickString(metadata.display_name) ?? |
| 30 | + pickString(metadata.full_name) ?? |
| 31 | + pickString(metadata.name), |
| 32 | + photoURL: |
| 33 | + pickString(metadata.avatar_url) ?? |
| 34 | + pickString(metadata.picture) ?? |
| 35 | + pickString(metadata.photo_url), |
| 36 | + emailVerified: Boolean(user.email_confirmed_at), |
| 37 | + createdAt: user.created_at ? new Date(user.created_at) : null, |
| 38 | + lastLoginAt: user.last_sign_in_at ? new Date(user.last_sign_in_at) : null, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +class SupabaseAuthService implements AuthRepository { |
| 43 | + async signup(data: SignUpPayload): Promise<SignUpResponse | Error> { |
| 44 | + try { |
| 45 | + const { data: authData, error } = await supabaseClient.auth.signUp({ |
| 46 | + email: data.email, |
| 47 | + password: data.password, |
| 48 | + options: data.displayName |
| 49 | + ? { |
| 50 | + data: { |
| 51 | + display_name: data.displayName, |
| 52 | + full_name: data.displayName, |
| 53 | + }, |
| 54 | + } |
| 55 | + : undefined, |
| 56 | + }); |
| 57 | + |
| 58 | + if (error) { |
| 59 | + return manageSupabaseError(error); |
| 60 | + } |
| 61 | + |
| 62 | + const user = supabaseUserToEntity(authData.user); |
| 63 | + if (!user) { |
| 64 | + return new Error('No se pudo obtener el usuario recién registrado.'); |
| 65 | + } |
| 66 | + |
| 67 | + return { user }; |
| 68 | + } catch (error) { |
| 69 | + return manageSupabaseError(error); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async signin(data: SignInPayload): Promise<SignInResponse | Error> { |
| 74 | + try { |
| 75 | + const { data: authData, error } = |
| 76 | + await supabaseClient.auth.signInWithPassword({ |
| 77 | + email: data.email, |
| 78 | + password: data.password, |
| 79 | + }); |
| 80 | + |
| 81 | + if (error) { |
| 82 | + return manageSupabaseError(error); |
| 83 | + } |
| 84 | + |
| 85 | + const user = supabaseUserToEntity(authData.user); |
| 86 | + if (!user) { |
| 87 | + return new Error('No se pudo obtener el usuario autenticado.'); |
| 88 | + } |
| 89 | + |
| 90 | + return { user }; |
| 91 | + } catch (error) { |
| 92 | + return manageSupabaseError(error); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + async signout(): Promise<void | Error> { |
| 97 | + try { |
| 98 | + const { error } = await supabaseClient.auth.signOut(); |
| 99 | + if (error) { |
| 100 | + return manageSupabaseError(error); |
| 101 | + } |
| 102 | + return; |
| 103 | + } catch (error) { |
| 104 | + return manageSupabaseError(error); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + async getCurrentUser(): Promise<AuthUser | null | Error> { |
| 109 | + try { |
| 110 | + const { data, error } = await supabaseClient.auth.getUser(); |
| 111 | + if (error) { |
| 112 | + return manageSupabaseError(error); |
| 113 | + } |
| 114 | + return supabaseUserToEntity(data.user); |
| 115 | + } catch (error) { |
| 116 | + return manageSupabaseError(error); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + onAuthStateChanged(callback: AuthStateChangeCallback): AuthStateUnsubscribe { |
| 121 | + const { data } = supabaseClient.auth.onAuthStateChange( |
| 122 | + (_event, session) => { |
| 123 | + callback(supabaseUserToEntity(session?.user ?? null)); |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
| 127 | + return () => { |
| 128 | + data.subscription.unsubscribe(); |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + async sendEmailVerification(): Promise<void | Error> { |
| 133 | + try { |
| 134 | + const { data: userData, error: userError } = |
| 135 | + await supabaseClient.auth.getUser(); |
| 136 | + if (userError) { |
| 137 | + return manageSupabaseError(userError); |
| 138 | + } |
| 139 | + |
| 140 | + const email = userData.user?.email; |
| 141 | + if (!email) { |
| 142 | + return new Error('No hay usuario autenticado.'); |
| 143 | + } |
| 144 | + |
| 145 | + const { error } = await supabaseClient.auth.resend({ |
| 146 | + type: 'signup', |
| 147 | + email, |
| 148 | + }); |
| 149 | + |
| 150 | + if (error) { |
| 151 | + return manageSupabaseError(error); |
| 152 | + } |
| 153 | + return; |
| 154 | + } catch (error) { |
| 155 | + return manageSupabaseError(error); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + async sendPasswordResetEmail(email: string): Promise<void | Error> { |
| 160 | + try { |
| 161 | + const { error } = await supabaseClient.auth.resetPasswordForEmail(email); |
| 162 | + if (error) { |
| 163 | + return manageSupabaseError(error); |
| 164 | + } |
| 165 | + return; |
| 166 | + } catch (error) { |
| 167 | + return manageSupabaseError(error); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + async updateProfile(data: { |
| 172 | + displayName?: string; |
| 173 | + photoURL?: string; |
| 174 | + }): Promise<AuthUser | Error> { |
| 175 | + try { |
| 176 | + const metadata: Record<string, string> = {}; |
| 177 | + if (data.displayName !== undefined) { |
| 178 | + metadata.display_name = data.displayName; |
| 179 | + metadata.full_name = data.displayName; |
| 180 | + } |
| 181 | + if (data.photoURL !== undefined) { |
| 182 | + metadata.avatar_url = data.photoURL; |
| 183 | + } |
| 184 | + |
| 185 | + const { data: updatedData, error } = await supabaseClient.auth.updateUser( |
| 186 | + { data: metadata }, |
| 187 | + ); |
| 188 | + |
| 189 | + if (error) { |
| 190 | + return manageSupabaseError(error); |
| 191 | + } |
| 192 | + |
| 193 | + const user = supabaseUserToEntity(updatedData.user); |
| 194 | + if (!user) { |
| 195 | + return new Error('No se pudo obtener el usuario actualizado.'); |
| 196 | + } |
| 197 | + return user; |
| 198 | + } catch (error) { |
| 199 | + return manageSupabaseError(error); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + async deleteAccount(): Promise<void | Error> { |
| 204 | + try { |
| 205 | + const { error } = await supabaseClient.rpc('delete_user'); |
| 206 | + if (error) { |
| 207 | + return manageSupabaseError(error); |
| 208 | + } |
| 209 | + |
| 210 | + const { error: signOutError } = await supabaseClient.auth.signOut(); |
| 211 | + if (signOutError) { |
| 212 | + return manageSupabaseError(signOutError); |
| 213 | + } |
| 214 | + return; |
| 215 | + } catch (error) { |
| 216 | + return manageSupabaseError(error); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +function createSupabaseAuthService(): AuthRepository { |
| 222 | + return new SupabaseAuthService(); |
| 223 | +} |
| 224 | + |
| 225 | +export default createSupabaseAuthService(); |
0 commit comments