I am developing a Next.js authentication application and encountering an issue where the user search functionality returns an empty array. I have verified in the Amity console that the user does exist. However, despite this, I’m consistently receiving an empty array.
Below is the code from the file where I’m attempting to implement the user search functionality:
import { getDataFromToken } from “@/helpers/getDataFromToken”;
import { NextRequest, NextResponse } from “next/server”;
import User from “@/models/userModel”;
import connect from “@/dbconfig/dbConfig”;
import { Client, API_REGIONS } from ‘@amityco/ts-sdk’;
import { UserRepository } from ‘@amityco/ts-sdk’;
const client = Client.createClient(${process.env.AMITY_API_KEY}
, API_REGIONS.US);
const sessionHandler: Amity.SessionHandler = {
sessionWillRenewAccessToken(renewal) {
renewal.renew();
},
};
const handleConnect = async (userId: string, displayName: string) => {
console.log(userId, displayName)
await Client.login({ userId, displayName }, sessionHandler);
};
connect();
export async function GET(request: NextRequest) {
try {
const userId = await getDataFromToken(request);
console.log(userId)
const user = await User.findOne({ _id: userId }).select("-password");
console.log(user)
handleConnect(`${user.email}`, `${user.username}`);
const unsub = UserRepository.searchUserByDisplayName({ displayName: `${user.username}` }, ({ data: user }) =>
console.log(user)
);
unsub();
return NextResponse.json({
mesaaage: "User found",
data: user
})
} catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 400 });
}
ts-sdk version
: 6.10.0
The amity console user management tab screenshot is also attached