Skip to content
Back

Realtime connection not working with user permissions

  • 0
  • Auth
  • Web
  • Realtime
  • Cloud
! NHero
18 Dec, 2024, 18:24

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.
! NHero
18 Dec, 2024, 18:24

attached image is my realtime code

! NHero
18 Dec, 2024, 18:40

and in the network tab in realtime the user is showing null

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more