| import { db } from '@/backend/services/firebase'; |
|
|
| export const AuthService = { |
| async saveCustomKey(sessionId, apiKey) { |
| const snapshot = await db.ref('users').orderByChild('Active_Session_ID').equalTo(sessionId).once('value'); |
| if (!snapshot.exists()) throw new Error("User not found."); |
| const userKey = Object.keys(snapshot.val())[0]; |
| await db.ref(`users/${userKey}`).update({ Private_API_Key: apiKey }); |
| return { success: true }; |
| }, |
|
|
| async updateActiveSession(userId, newSessionId) { |
| if (!userId) throw new Error("Membership ID is required."); |
|
|
| |
| let snapshot = await db.ref('users').orderByChild('Login_Key').equalTo(userId).once('value'); |
| |
| |
| if (!snapshot.exists()) { |
| snapshot = await db.ref('users').orderByChild('Active_Session_ID').equalTo(userId).once('value'); |
| } |
|
|
| if (!snapshot.exists()) throw new Error("Membership ID not found in records."); |
| |
| const userKey = Object.keys(snapshot.val())[0]; |
| const userData = snapshot.val()[userKey]; |
| |
| const updates = { Active_Session_ID: newSessionId }; |
| |
| |
| if (!userData.Login_Key) { |
| updates.Login_Key = userId; |
| } |
| |
| await db.ref(`users/${userKey}`).update(updates); |
| |
| return { success: true }; |
| } |
| }; |