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
- Impossible to get USER after createEmail...
Am using provider to deal with functions linked to appwrite. Here is my login. Future<String?> login(String email, String password) async { try { aw...
- fastly error
Hey! I'm hitting a Fastly error on the www version of our site, but the root domain works fine. We have a wildcard set up, so I expected the subdomain to be cov...
- Facebook's scraper facebookexternalhit g...
share.bardbliss.com but works fine on the raw fra.appwrite.run URL. No execution logs appear when Facebook hits the custom domain. This was working before. How ...