Skip to content

Commit

Permalink
fix (firebase) : client side initialisation of auth & database
Browse files Browse the repository at this point in the history
  • Loading branch information
fal3n-4ngel committed Feb 2, 2025
1 parent 8aa4bd7 commit 79b622f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export class AuthService {
static async logout(): Promise<void> {
try {
const auth = firebaseService.auth;
if (!auth) {
console.error('Database not found');
return;
}
await signOut(auth);
} catch (error) {
console.error('Error signing out:', error);
Expand Down
5 changes: 5 additions & 0 deletions app/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export class ChatService {
content: string,
): Promise<string> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return "";
}
const messagesRef = ref(database, 'messages');

const newMessageRef = push(messagesRef);

const message: ChatMessage = {
Expand Down
6 changes: 2 additions & 4 deletions app/services/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ class FirebaseService {
return FirebaseService.instance;
}

get database(): Database {
if (!this._database) throw new Error('Database not initialized');
get database(): Database|undefined {
return this._database;
}

get auth(): Auth {
if (!this._auth) throw new Error('Auth not initialized');
get auth(): Auth|undefined {
return this._auth;
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/services/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export class NodeService {
console.log('Client ID is missing');
return;
};


const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return;
}
const presenceRef = ref(database, `presence/${clientId}`);

try {
Expand All @@ -30,6 +35,10 @@ export class NodeService {
metadata: Partial<NetworkNode['metadata']>,
): Promise<void> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return;
}
const nodeRef = ref(database, `presence/${nodeId}`);

try {
Expand Down
35 changes: 34 additions & 1 deletion app/services/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export class TaskService {
): Promise<string> {
const database = firebaseService.database;
const auth = firebaseService.auth;
if (!database) {
console.error('Database not found');
return "";
}
if (!auth) {
console.error('Database not found');
return "";
}


const tasksRef = ref(database, 'tasks');
const newTaskRef = push(tasksRef);
const userId = auth.currentUser?.uid;
Expand Down Expand Up @@ -49,8 +59,11 @@ export class TaskService {
error?: string
): Promise<void> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return;
}
const taskRef = ref(database, `tasks/${taskId}`);

const updates: Partial<Task> = {
status,
updatedAt: new Date().toISOString(),
Expand All @@ -77,6 +90,10 @@ export class TaskService {
workerId: string
): Promise<void> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return;
}
const taskRef = ref(database, `tasks/${taskId}`);

try {
Expand All @@ -94,6 +111,10 @@ export class TaskService {

static async getTasksByUserId(userId: string): Promise<Task[]> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return [];
}
const tasksRef = ref(database, 'tasks');

try {
Expand Down Expand Up @@ -122,6 +143,10 @@ export class TaskService {

static async getTaskById(taskId: string): Promise<Task | null> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return null;
}
const taskRef = ref(database, `tasks/${taskId}`);

try {
Expand All @@ -141,6 +166,10 @@ export class TaskService {

static async getPendingTasks(): Promise<Task[]> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return [];
}
const tasksRef = ref(database, 'tasks');

try {
Expand Down Expand Up @@ -169,6 +198,10 @@ export class TaskService {

static async deleteTask(taskId: string): Promise<void> {
const database = firebaseService.database;
if (!database) {
console.error('Database not found');
return;
}
const taskRef = ref(database, `tasks/${taskId}`);

try {
Expand Down

0 comments on commit 79b622f

Please sign in to comment.