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
- How to get current user account session ...
i am using sveltekit and i'm a bit unclear on how to get the current user's session from the server side. thisi s what i have so far // src/lib/server/appwrite...
- Current User Is Not Authorized To Perfor...
I keep getting an error saying the current user is not authorized to perform the requested action, when I'm trying to create a row a table. Permissions have bee...
- Relationships and Realtime
Since now Relationship attributes are only returned when explicitly queried, how does this change reflect in Realtime? If a listener is listening to a row from ...