Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getMessagingToken = async () => {
try {
const messaging = firebase.messaging();
await messaging.requestPermission();
const token = await messaging.getToken();
// TODO: debugging
console.log({ token });
return token;
} catch (error) {
console.error(error);
return null;
}
};
const parser = new UAParser();
const browserName = parser.getBrowser().name;
const osName = parser.getOS().name;
const deviceType = parser.getDevice().type;
let database;
let auth;
let messaging;
let storage;
if (process.env.NODE_ENV !== 'test') {
firebase.initializeApp(firebaseConf);
database = firebase.database();
auth = firebase.auth();
if (constants.SUPPORTEDBROWSERS.indexOf(browserName) >= 0 && osName !== 'iOS') { // safari,iosではエラーになるため
messaging = firebase.messaging();
}
storage = firebase.storage();
}
export default {
/**
* firebaseのdatabaseを返します。
*/
getDatabase() {
return database;
},
/**
* firebaseのauthを返します。
*/
getAuth() {
return auth;
export const initializeFirebase = callback => {
try {
const senderId = process.env.REACT_APP_FIREBASE_SENDER_ID;
if(!senderId){
throw new Error("FIREBASE_SENDER_ID is missing. Please add it to environment variables.");
}
firebase.initializeApp({ messagingSenderId: senderId});
const messagingKey = process.env.REACT_APP_FIREBASE_MESSAGING_PUSH_KEY;
if(!messagingKey){
throw new Error("FIREBASE_MESSAGING_PUSH_KEY is missing. Please add it to environment variables.");
}
const messaging = firebase.messaging();
messaging.usePublicVapidKey(messagingKey);
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
callback(payload);
});
} catch (err) {
console.error("Unable to initialize firebase. " + err);
}
};