import { auth } from "../../../firebase";
import { GoogleAuthProvider, signInWithPopup } from "firebase/auth";
const signIn = () =>{
console.log("logging");
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider)
.then((result) => {
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
// IdP data available using getAdditionalUserInfo(result)
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.customData.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
}
// This is my firebase.js file where configuration is done
const app = getApps().length > 0 ? getApp() : initializeApp(firebaseConfig);
const db = getFirestore();
const storage = getStorage();
const auth = getAuth(app);
export {app,db,storage,auth}`your text`
I have enabled the google sign-in method in firebase.
And since it already had localhost as an authorized domain.
It was working perfectly with next auth but is having a problem when I tried with firebase auth.
Related
How to setup firebase for storage in nodejs? i did storage set in react but for some issuess i want to do that in nodejs so how can i do that in nodejs? it's giving me errors can anyone can tell me how to import properly to use that as a nodejs route
i have created a file called firebasestore for this how to import properly:
import firebase from 'firebase/compat/app';
import 'firebase/compat/storage';
const firebaseConfig = {
// here is everything for config
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
export { firebase };
Route where i want to use how to import properly:
router.post('/changeusername', (req, res) => {
const { Image } = req.body
if (!Image.cancelled) {
setIsPosting(true)
const response = await fetch(Image);
const blob = await response.blob();
const filename = Image.substring(Image.lastIndexOf('/') + 1);
const ref = firebase.storage().ref().child(filename);
const snapshot = await ref.put(blob);
const url = await snapshot.ref.getDownloadURL();
}
})
I am using Firebase's GoogleAuthProvider in my Vue 2 app to login user. I added the calendar to the provider's scope the get access to the user's calendar. I then use the access_token given in the result to retrieve the events. The problem is the accessToken expires after one hour. How can I renew it?
Here is my code to handle the login:
const auth = getAuth();
const provider = new GoogleAuthProvider();
provider.addScope("https://www.googleapis.com/auth/calendar");
const result = await signInWithPopup(auth, provider);
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const user = result.user;
await this.checkUser(user, token);
I wanted make a login using facebook and google accounts, this is my code:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signOut, signInWithEmailAndPassword,
onAuthStateChanged, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-auth.js";
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
// login with google
const provider = new GoogleAuthProvider();
const signInWithGoogle = document.querySelector('#google-icon');
signInWithGoogle.addEventListener('click', (e) => {
e.preventDefault();
signInWithPopup(auth, provider).then((result) => {
const user = result.user;
const credential = GoogleAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const email = error.email;
const credential = GoogleAuthProvider.credentialFromError(error);
});
});
but i keep getting this error message:
Info: The current domain is not authorized for OAuth operations. This will prevent signInWithPopup, signInWithRedirect, linkWithPopup and linkWithRedirect from working. Add your domain (127.0.0.1) to the OAuth redirect domains list in the Firebase console -> Auth section -> Sign in method tab.
can you help me, please?
I'm getting the following error while trying to add a sign up with apple in my nodejs backend:
status: auth/missing-or-invalid-nonce
message: Duplicate credential received. Please try again with a new credential
Here is the code:
const clientId = 'com.mybusiness';
const { nonce: nonce } = await appleSignin.verifyIdToken(
appleIdentityToken,
{
audience: clientId,
ignoreExpiration: true,
}
);
const hashedNonce: string = crypto
.createHash('sha256')
.update(nonce)
.digest()
.toString();
const provider = new firebase.auth.OAuthProvider('apple.com');
const credential: OAuthCredential = provider.credential({
idToken: appleIdentityToken,
rawNonce: hashedNonce,
});
const userCredential: UserCredential = await this.clientAuth.signInWithCredential(
credential
);
Im trying to make a login page to my firebase project, but always when i send the params to the functions its returns "Firebase is not defined . Here's my code:
<button id="login" onclick="signIn()"><Login</button>
The function signIn() only change the href to http://localhost:5000/signin/:email/:password
then i have this
const functions = require('firebase-functions');
const adm = require('firebase-admin');
const express = require('express');
const signin = require('./modules/signin');
const firebase = require("firebase");
// // Create and Deploy Your First Cloud Functions
adm.initializeApp(
functions.config().adm
);
const app = express();
app.get('/signin/:email/:password', (request, response) => {
exports.signin = signin(request.params.email, request.params.password);
});
exports.app = functions.https.onRequest(app);
and my function is only
function signin (email, password) {
firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
// user signed in
console.log ("Usuário logado com sucesso!");
return True;
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
// alert('Wrong password.');
return false;
} else {
alert(errorCode+": "+errorMessage);
return false;
}
});
}
module.exports = signin;
im trying to use the index.js on functions folder as a type of "controller" to call functions on backend, but im having trouble to solve this simple problem.
You can't use the Firebase client SDK in Cloud Functions. Cloud Functions is considered backend code, and doesn't run on the browser. If you want to sign in the user, you have to use the SDK in your browser code, not backend code.