-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataconnect not recgonizing Signed in user in Next.js API route #8797
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
Hi @ignitehub-dev, have you tried using |
@dlarocque @maneesht Ahh havent seen the serverApp before. I tried updating const authIdToken = request.headers.get("authorization")?.split(" ")[1];
const serverApp = initializeServerApp(app, { authIdToken });
const auth = getAuth(serverApp);
await auth.authStateReady();
const dataConnectServer = getDataConnect(serverApp, connectorConfig);
If I disable the emulators, I still get a 401 unauthenticated. |
Actually think I wasnt connecting to the emulator with the server app. Added connectDataConnectEmulator(dataConnectServer, "localhost", 9399); But still getting 401 unauthenticated |
@ignitehub-dev have you tried to pass in the
|
@maneesht Yessir, full file below. It looks like my user isnt getting authenticated with the token? The Documentation you sent states that. // FirebaseServerApp and Auth will now attempt
// to sign in the current user based on provided
// authIdToken. But assuming im getting null either because im setting up a new auth instance or the user hasn't had time to get created in the emulator? But Wrapping my call in a setTimeout didnt help.
import { NextResponse } from "next/server";
import { getUserById, createUser } from "@IgniteHub/dataconnect";
// import { dataConnectServer } from "../firebaseServer";
import { app, dataConnect, firebaseConfig } from "@/utils/firebase";
import { initializeServerApp } from "firebase/app";
import { z } from "zod";
import { connectorConfig } from "@IgniteHub/dataconnect";
import {
getDataConnect,
connectDataConnectEmulator
} from "firebase/data-connect";
import { getAuth } from "firebase/auth";
const querySchema = z.object({
userId: z.string(),
displayName: z.string().optional(),
email: z.string()
});
export async function GET(request: Request) {
const authIdToken = request.headers.get("authorization")?.split(" ")[1];
const serverApp = initializeServerApp(app, { authIdToken });
const auth = await getAuth(serverApp);
console.log("auth.currentUser: ", auth.currentUser);
await auth.authStateReady();
const dataConnectServer = getDataConnect(serverApp, connectorConfig);
connectDataConnectEmulator(dataConnectServer, "localhost", 9399);
const { searchParams } = new URL(request.url);
const query = Object.fromEntries(searchParams.entries());
const parsed = querySchema.safeParse(query);
if (!parsed.success) {
return NextResponse.json(
{ error: parsed.error.flatten() },
{ status: 400 }
);
}
const { userId, displayName, email } = parsed.data;
try {
const userProfile = await getUserById(dataConnectServer, { id: userId });
if (!userProfile.data.user) {
const [firstName, lastName = ""] = (displayName ?? "").split(" ");
const userData = await createUser(dataConnectServer, {
id: userId,
firstName,
lastName,
email
});
console.log("mutation data: ", userData);
}
return NextResponse.json({ message: "Success" });
} catch (error) {
return NextResponse.json(
{ error: "Failed to fetch user profile:" + error },
{ status: 500 }
);
}
} |
Operating System
macOS Monterey 12.7.4
Environment (if applicable)
Chrome 132
Firebase SDK Version
11.2.0
Firebase SDK Product(s)
DataConnect, Auth
Project Tooling
My project is typescript next.js version 15.1.6 using app router. It uses Firebase Auth, Functions and DataConnect. Project repo is public on my profile named "IgniteHub". Latest is pushed.
Detailed Problem Description
I have an auth context that I want to connect to an api route the will create a users profile in DataConnect when a new user signs in/up. It will check dataconnect for the users UID in my users table if the user doesnt exist it creates the profile with userCredentials from the return of
signInWithPopup
. When doing this DataConnect isnt reconizing the signed in user and giving me the error message.authContext function
API Route
firebase.ts
package.json
Error Message
Steps and code to reproduce issue
npm run dev
handleGoogleSignIn
The text was updated successfully, but these errors were encountered: