Skip to content

Commit 42f56c8

Browse files
feat(auth): add supabase auth provider and restructure auth services
Rename firebase auth service files to follow consistent naming, update imports in auth factory and tests. Simplify .codegraph gitignore to ignore all local transient files except the ignore file itself. Implement full Supabase authentication provider with all standard auth operations including signup, signin, signout, profile management and auth state listening.
1 parent 4583701 commit 42f56c8

5 files changed

Lines changed: 235 additions & 27 deletions

File tree

‎.codegraph/.gitignore‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
# CodeGraph data files
2-
# These are local to each machine and should not be committed
3-
4-
# Database
5-
*.db
6-
*.db-wal
7-
*.db-shm
8-
9-
# Cache
10-
cache/
11-
12-
# Logs
13-
*.log
14-
15-
# Hook markers
16-
.dirty
1+
# CodeGraph data files — local to each machine, not for committing.
2+
# Ignore everything in .codegraph/ except this file itself, so transient
3+
# files (the database, daemon.pid, sockets, logs) never show up in git.
4+
*
5+
!.gitignore

‎__tests__/modules/authentication/infrastructure/firebase-auth.service.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jest.mock('@modules/firebase/domain/firebase.error', () => ({
1717

1818
import firebaseAuthService from '@modules/firebase/infrastructure/authentication.service';
1919
import { manageFirebaseError } from '@modules/firebase/domain/firebase.error';
20-
import firebaseAuthServiceAuth from '../../../../src/modules/authentication/infrastructure/firebase-auth.service';
20+
import firebaseAuthServiceAuth from '../../../../src/modules/authentication/infrastructure/auth.firebase.service';
2121

2222
describe('FirebaseAuthService', () => {
2323
const mockFirebaseUser = {

src/modules/authentication/infrastructure/firebase-auth.service.ts renamed to src/modules/authentication/infrastructure/auth.firebase.service.ts

File renamed without changes.

‎src/modules/authentication/infrastructure/auth.service.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import { AuthRepository } from '../domain/auth.repository';
22
import authHttpService from './auth.http.service';
3-
import authFirebaseService from './firebase-auth.service';
3+
import authFirebaseService from './auth.firebase.service';
4+
import authSupabaseService from './auth.supabase.service';
45
import authMockService from './auth.mock.service';
56
import { CONFIG } from '@config/config';
67

7-
/**
8-
* Auth Service Factory
9-
*
10-
* This factory returns the appropriate AuthRepository implementation
11-
* based on the SERVICE_PROVIDER configuration.
12-
*
13-
* To switch between implementations, update the SERVICE_PROVIDER
14-
* constant in config/config.ts
15-
*/
168
function createAuthService(): AuthRepository {
179
switch (CONFIG.SERVICE_PROVIDER) {
1810
case 'http':
1911
return authHttpService;
2012
case 'firebase':
2113
return authFirebaseService;
14+
case 'supabase':
15+
return authSupabaseService;
2216
case 'mock':
2317
return authMockService;
2418
default:
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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

Comments
 (0)