Firebase Admin credentials error. Cannot connect to database.
Answered
European sprat posted this in #help-forum
European spratOP
Im using firebase with firebase admin and the regular firebase credentials.
To use firebase admin I have to aquire a list of credentials from a service account.
When I use the private key in admin.credential.cert({}) it shoots this error:
after reading a lot on stack overflow and netlify (i have the deployment there)
I made it work like this once:
But the company asked to shoot down that deployment, and now it does not work, it shoots the same error again and again, and now Im tired and need help to fix this bug.
On local dev I used ' " private key" ' and then parsed that string, that did the job on local, but on netlify production does not work that way.
Let me know if you can help me.
To use firebase admin I have to aquire a list of credentials from a service account.
When I use the private key in admin.credential.cert({}) it shoots this error:
⨯ Error: secretOrPrivateKey must be an asymmetric key when using RS256
at module.exports [as sign] (/var/task/node_modules/jsonwebtoken/sign.js:130:22)after reading a lot on stack overflow and netlify (i have the deployment there)
I made it work like this once:
credential: admin.credential.cert({
privateKey: JSON.parse(
`${process.env.CHESSFORTUNE_ADMIN_SDK_PRIVATE_KEY}`
),But the company asked to shoot down that deployment, and now it does not work, it shoots the same error again and again, and now Im tired and need help to fix this bug.
On local dev I used ' " private key" ' and then parsed that string, that did the job on local, but on netlify production does not work that way.
Let me know if you can help me.
47 Replies
@European sprat Im using firebase with firebase admin and the regular firebase credentials.
To use firebase admin I have to aquire a list of credentials from a service account.
When I use the private key in admin.credential.cert({}) it shoots this error:
bash
⨯ Error: secretOrPrivateKey must be an asymmetric key when using RS256
at module.exports [as sign] (/var/task/node_modules/jsonwebtoken/sign.js:130:22)
after reading a lot on stack overflow and netlify (i have the deployment there)
I made it work like this once:
js
credential: admin.credential.cert({
privateKey: JSON.parse(
`${process.env.CHESSFORTUNE_ADMIN_SDK_PRIVATE_KEY}`
),
But the company asked to shoot down that deployment, and now it does not work, it shoots the same error again and again, and now Im tired and need help to fix this bug.
On local dev I used ' " private key" ' and then parsed that string, that did the job on local, but on netlify production does not work that way.
Let me know if you can help me.
hi, try this
credential: admin.credential.cert({
privateKey: `${process.env.CHESSFORTUNE_ADMIN_SDK_PRIVATE_KEY}`.replace(/\\n/g, '\n')
),@Ray hi, try this
ts
credential: admin.credential.cert({
privateKey: `${process.env.CHESSFORTUNE_ADMIN_SDK_PRIVATE_KEY}`.replace(/\\n/g, '\n')
),
European spratOP
Hi ray, it gave me the same error.
I read somewhere that using the latest lts of node it would fix the issue, and localy it did.
But on deploy I have the same issue.
I read somewhere that using the latest lts of node it would fix the issue, and localy it did.
But on deploy I have the same issue.
@European sprat Hi ray, it gave me the same error.
I read somewhere that using the latest lts of node it would fix the issue, and localy it did.
But on deploy I have the same issue.
have you try setting the nodejs version on netlify?
I think you could set it to latest lts
@Ray have you try setting the nodejs version on netlify?
European spratOP
Hi again ray, did not work, but it worked for local.
European spratOP
Yeah:
Error: secretOrPrivateKey must be an asymmetric key when using RS256I'll try again with the old solutions to see if with the latest lts one of those may work.
@European sprat Yeah:
bash
Error: secretOrPrivateKey must be an asymmetric key when using RS256
does the
CHESSFORTUNE_ADMIN_SDK_PRIVATE_KEY look like this?-----BEGIN PRIVATE KEY-----\n<YOUR_PRIVATE_KEY>\n-----END PRIVATE KEY-----\nEuropean spratOP
Yeah, just like that.
European spratOP
No, I did not think of that. Lets see.
European spratOP
Well that did it, but it broke my middleware Jajajaja
One step closer thats good
Can I pass my middleware function here?
This is my middleware function, it uses a cookie to check if user is logged in and has valid credentials.
export async function middleware(request, response) {
const session = request.cookies.get("session");
//Return to /login if don't have a session
if (!session) {
return NextResponse.redirect(new URL("/login", request.url));
}
//Call the authentication endpoint
const responseAPI= await fetch(`${request.nextUrl.origin}/api/login`, {
headers: {
Cookie: `session=${session?.value}`,
},
});
//Return to /login if token is not authorized
if (responseAPI.status !== 200) {
console.log("Unauthorized");
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}@European sprat Well that did it, but it broke my middleware Jajajaja
then your variable didn't set correctly
Answer
European spratOP
but now it does not let me
@European sprat but now it does not let me
let you what?
@Ray then your variable didn't set correctly
European spratOP
it does not let me see other than the unprotected routes, but the fetch to firebase using the key works just fine.
it redirect you to /login?
European spratOP
yeah.
@European sprat yeah.
check the log on
/api/loginor can you show the code on
/api/login?European spratOP
Going
export async function GET(request) {
const session = cookies().get("session").value || "";
if (!session) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
const decodedClaims = await auth().verifySessionCookie(session, true);
//WHEN ALL ADMINS ARE CREATED UNCOMMENT THE SNIPPETS BELOW
/*const admin= await getAuth()
.getUser(decodedClaims.uid)
.then((userRecord) => {
return userRecord.toJSON()})
.catch((error) => {
console.log('Error fetching user data:', error);
});*/
if (!decodedClaims /*|| 'admin' in admin.customClaims == false*/) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
return NextResponse.json({ isLogged: true }, { status: 200 });
}@European sprat js
export async function GET(request) {
const session = cookies().get("session").value || "";
if (!session) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
const decodedClaims = await auth().verifySessionCookie(session, true);
//WHEN ALL ADMINS ARE CREATED UNCOMMENT THE SNIPPETS BELOW
/*const admin= await getAuth()
.getUser(decodedClaims.uid)
.then((userRecord) => {
return userRecord.toJSON()})
.catch((error) => {
console.log('Error fetching user data:', error);
});*/
if (!decodedClaims /*|| 'admin' in admin.customClaims == false*/) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
return NextResponse.json({ isLogged: true }, { status: 200 });
}
try this
// middleware
export async function middleware(request, response) {
const session = request.cookies.get("session");
//Return to /login if don't have a session
if (!session?.value) {
return NextResponse.redirect(new URL("/login", request.url));
}
//Call the authentication endpoint
const responseAPI= await fetch(`${request.nextUrl.origin}/api/login`, {
headers: {
session: session.value,
},
});
//Return to /login if token is not authorized
if (responseAPI.status !== 200) {
console.log("Unauthorized");
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
// api/login/route.js
export async function GET(request) {
const session = request.headers.get('session')
if (!session) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
const decodedClaims = await auth().verifySessionCookie(session, true);
//WHEN ALL ADMINS ARE CREATED UNCOMMENT THE SNIPPETS BELOW
/*const admin= await getAuth()
.getUser(decodedClaims.uid)
.then((userRecord) => {
return userRecord.toJSON()})
.catch((error) => {
console.log('Error fetching user data:', error);
});*/
if (!decodedClaims /*|| 'admin' in admin.customClaims == false*/) {
return NextResponse.json({ isLogged: false }, { status: 401 });
}
return NextResponse.json({ isLogged: true }, { status: 200 });
}European spratOP
Nope. I think I will just let the middleware out this time. It works local, but it does not work on netlify.
European spratOP
Nope, it just does not authorize the user to go to protected paths but it does not show any error
and in development is working fine.
European spratOP
Wait
it is working now
Lol.
European spratOP
Sometimes happens that it works and you arent sure how?
Hey Ray
thanks a lot bro
Now I can go to sleep finally
You have my gratituted and my best wishes.
If I may a final question to close this topic:
What docs (or videos if you want) read about middleware and how it works? (outside documentation)
And any other advice on how to get better with next, I'll be gratefull.
What docs (or videos if you want) read about middleware and how it works? (outside documentation)
And any other advice on how to get better with next, I'll be gratefull.
Thanks a lot my friend.
@European sprat Thanks a lot my friend.
no prob, for the doc about middleware, I just read the official doc 😆
European spratOP
Thanks bro.
