ibrahim_khan47
CDCloudflare Developers
•Created by ibrahim_khan47 on 4/16/2024 in #pages-discussions
Nextjs 14 app API and am using firebase for chat in my app
here is code
// api endpoint to update user online status
import { db_api } from '@/app/api/(chat)/firebase/config';
import { isUserAuthenticated } from '@/app/api/(chat)/utils/utils';
export const runtime = 'edge';
export async function PUT(request) {
if (!isUserAuthenticated(request)) {
return new Response('Unauthorized', { status: 401 });
}
const formData = await request.formData();
const username = formData.get('username');
const userBadge = formData.get('userBadge'); // Ensure this value is correctly obtained
const online = formData.get('online');
// Basic validation
if (!username || online === null) {
return new Response('Bad Request', { status: 400 });
}
// console.log(`Updating user: ${username} with badge: ${userBadge} and online status: ${online}`);
// Get a reference to the user document in Firestore
const userRef = db_api.collection('users').doc(username);
try {
// Update the user's online status, last_changed timestamp, and userBadge
// Firestore will automatically create the document if it doesn't exist
await userRef.set(
{
username,
online: online === 'true' ? true : false,
last_changed: Date.now(),
...(userBadge ? { userBadge } : {}), // Conditionally add userBadge if it's provided
},
{ merge: true },
);
return new Response('User updated', { status: 200 });
} catch (error) {
console.error('Error updating user:', error);
// Provide more detailed error information in the response
return new Response(`Error updating user: ${error.message}`, {
status: 500,
statusText: 'Internal Server Error',
});
}
}
// api endpoint to update user online status
import { db_api } from '@/app/api/(chat)/firebase/config';
import { isUserAuthenticated } from '@/app/api/(chat)/utils/utils';
export const runtime = 'edge';
export async function PUT(request) {
if (!isUserAuthenticated(request)) {
return new Response('Unauthorized', { status: 401 });
}
const formData = await request.formData();
const username = formData.get('username');
const userBadge = formData.get('userBadge'); // Ensure this value is correctly obtained
const online = formData.get('online');
// Basic validation
if (!username || online === null) {
return new Response('Bad Request', { status: 400 });
}
// console.log(`Updating user: ${username} with badge: ${userBadge} and online status: ${online}`);
// Get a reference to the user document in Firestore
const userRef = db_api.collection('users').doc(username);
try {
// Update the user's online status, last_changed timestamp, and userBadge
// Firestore will automatically create the document if it doesn't exist
await userRef.set(
{
username,
online: online === 'true' ? true : false,
last_changed: Date.now(),
...(userBadge ? { userBadge } : {}), // Conditionally add userBadge if it's provided
},
{ merge: true },
);
return new Response('User updated', { status: 200 });
} catch (error) {
console.error('Error updating user:', error);
// Provide more detailed error information in the response
return new Response(`Error updating user: ${error.message}`, {
status: 500,
statusText: 'Internal Server Error',
});
}
}
1 replies