Im creating a chat app , with authentication in server side
Here is my client config
TypeScript
import env from "@/env";
import {
Client,
Account,
Storage,
Databases,
Avatars,
} from "appwrite"
import axios from "axios";
export async function createSessionClient() {
const client = new Client()
.setEndpoint(env.appwrite.endpoint)
.setProject(env.appwrite.projectId);
const session = (await axios.get("/api/auth/session")).data.session
if (!session || !session.value) {
throw new Error("No session found");
}
client.setSession(session.value);
return {
get client() {
return client;
},
get account() {
return new Account(client);
},
get storage() {
return new Storage(client);
},
get databases() {
return new Databases(client);
},
get avatars() {
return new Avatars(client);
},
};
}
export async function getLoggedInUser() {
try {
const { account } = await createSessionClient()
const user = await account.get()
return {
id: user.$id,
name: user.name,
email: user.email,
avatar: `https://ui-avatars.com/api/?name=${user.name.replace(/ /g, "+")}&background=random`,
};
}
catch (err) {
console.log(err)
return null;
}
}
here is my /api/auth/me route
TypeScript
import { getLoggedInUser } from "@/appwrite/server/config";
import { NextResponse } from "next/server";
export async function GET() {
const user = await getLoggedInUser();
if (!user) {
return NextResponse.json({error: "Not logged in"}, {status: 401})
}
return NextResponse.json({
id: user.$id,
name: user.name,
email: user.email,
avatar: `https://ui-avatars.com/api/?name=${user.name.replace(/ /g, "+")}&background=random`,
}, {status: 200})
}
TL;DR
Realtime connection issue in chat app due to user being null in network tab. Developers need to check authentication in the server-side code for logged-in user. Solution involves ensuring proper session handling and user retrieval in the server-side /api/auth/me route.attached image is my realtime code
and in the network tab in realtime the user is showing null
Recommended threads
- My cloud functions failing 3 days ago (P...
Hi, My cloud function using python has been failing for 3 days, I didn't push any new deployments... Its something to do with it not recognising the entrypoi...
- Looking for a Partner
- Function was working: Failed to load ent...
I had a Bun function deployed two months ago working until 2 days ago on a daily basis, now I'm getting: Failed to load entrypoint, file src/main.js does not ex...