Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions api/domain/entities/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export class User {
private email: string;
private password: string;
private name: string;
private createdAt: Date;
private updatedAt: Date;

constructor(email: string, password: string, name: string) {
this.email = email;
this.password = password;
this.name = name;
this.createdAt = new Date();
this.updatedAt = new Date();
}

public getEmail(): string {
return this.email;
}

public getPassword(): string {
return this.password;
}

public getName(): string {
return this.name;
}

public getCreatedAt(): Date {
return this.createdAt;
}

public getUpdatedAt(): Date {
return this.updatedAt;
}

public static create(email: string, password: string, name: string): User {
return new User(email, password, name);
}

public toPlainObject(): { [key: string]: any } {
return {
email: this.email,
name: this.name,
createdAt: this.createdAt.toISOString(),
updatedAt: this.updatedAt.toISOString(),
};
}
}
26 changes: 26 additions & 0 deletions api/domain/repositories/AuthRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Auth } from 'firebase-admin/auth';
import { auth } from '../../../lib/main.admin';

export class AuthRepository {
private auth!: Auth;

constructor() {
this.initializeAuth();
}

private async initializeAuth() {
this.auth = await auth();
}

public async createUser(email: string, password: string): Promise<any> {
try {
const userRecord = await this.auth.createUser({
email,
password,
});
return userRecord;
} catch (error) {
throw new Error('Error creating user: ' + (error as any).message);
}
}
}
23 changes: 23 additions & 0 deletions api/domain/repositories/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Firestore } from 'firebase-admin/firestore';
import { User } from '../entities/User';
import { firestore } from '../../../lib/main.admin';

export class UserRepository {
private db!: Firestore;

constructor() {
this.initializeFirestore();
}

private async initializeFirestore() {
this.db = await firestore();
}

public async save(user: User): Promise<void> {
try {
await this.db.collection('users').doc().set(user.toPlainObject());
} catch (error) {
throw new Error('Error saving user: ' + (error as Error).message);
}
}
}
24 changes: 24 additions & 0 deletions api/usecases/RegisterUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { User } from '../domain/entities/User';
import { AuthRepository } from '../domain/repositories/AuthRepository';
import { UserRepository } from '../domain/repositories/UserRepository';

export class RegisterUserUseCase {
private userRepository: UserRepository;
private authRepository: AuthRepository;

constructor(userRepository: UserRepository, authRepository: AuthRepository) {
this.userRepository = userRepository;
this.authRepository = authRepository;
}

public async execute(
userEmail: string,
userPassword: string,
userName: string
): Promise<User> {
const user = User.create(userEmail, userPassword, userName);
await this.authRepository.createUser(userEmail, userPassword);
await this.userRepository.save(user);
return user;
}
}
28 changes: 0 additions & 28 deletions lib/configureFirebaseServer.ts

This file was deleted.

24 changes: 24 additions & 0 deletions lib/firebase.admin.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use server';
// firebase.config.ts
import { initializeApp, getApps, getApp, cert } from 'firebase-admin/app';
import { getFirestore as getFirestoreInstance } from 'firebase-admin/firestore';
import { getAuth as getAuthInstance } from 'firebase-admin/auth';

const projectId = process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID!;
const clientEmail = process.env.PRIVATE_FIREBASE_CLIENT_EMAIL!;
const privateKey = JSON.parse(process.env.PRIVATE_FIREBASE_PRIVATE_KEY!);
const databaseURL = process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL!;

const app = !getApps().length
? initializeApp({
credential: cert({
projectId,
clientEmail,
privateKey,
}),
databaseURL,
})
: getApp();

export const getFirestore = () => getFirestoreInstance(app);
export const getAuth = () => getAuthInstance(app);
6 changes: 6 additions & 0 deletions lib/main.admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getAuth, getFirestore } from './firebase.admin.config';

const firestore = async () => await getFirestore();
const auth = async () => await getAuth();

export { firestore, auth };
28 changes: 28 additions & 0 deletions src/app/api/register/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { User } from '../../../../api/domain/entities/User';
import { UserRepository } from '../../../../api/domain/repositories/UserRepository';
import { AuthRepository } from '../../../../api/domain/repositories/AuthRepository';
import { RegisterUserUseCase } from '../../../../api/usecases/RegisterUser';

const userRepository = new UserRepository();
const authRepository = new AuthRepository();
const registerUserUseCase = new RegisterUserUseCase(
userRepository,
authRepository
);

export async function POST(req: Request) {
const { userEmail, userPassword, userName } = await req.json();

try {
const user = await registerUserUseCase.execute(
userEmail,
userPassword,
userName
);

return Response.json(user);
} catch (error) {
console.error('Error creating user:', error);
return Response.json({ error: 'Failed to create user' }, { status: 500 });
}
}