Switch to day view when clicking on days in week and month view. Refresh tokens every 12 hrs

This commit is contained in:
Milan Paunovic
2024-10-19 23:40:08 +02:00
parent 3653400a92
commit 478341cce4
3 changed files with 74 additions and 4 deletions

View File

@ -11,7 +11,6 @@ const db = admin.firestore();
let expo = new Expo({accessToken: process.env.EXPO_ACCESS_TOKEN});
// Firestore trigger that listens for new events in the 'Events' collection
exports.sendNotificationOnEventCreation = functions.firestore
.document('Events/{eventId}')
.onCreate(async (snapshot, context) => {
@ -193,6 +192,73 @@ exports.generateCustomToken = onRequest(async (request, response) => {
}
});
exports.refreshTokens = functions.pubsub.schedule('every 12 hours').onRun(async (context) => {
console.log('Running token refresh job...');
const profilesSnapshot = await db.collection('Profiles').get();
profilesSnapshot.forEach(async (profileDoc) => {
const profileData = profileDoc.data();
if (profileData.googleToken) {
try {
const refreshedGoogleToken = await refreshGoogleToken(profileData.googleToken);
await profileDoc.ref.update({ googleToken: refreshedGoogleToken });
console.log(`Google token updated for user ${profileDoc.id}`);
} catch (error) {
console.error(`Error refreshing Google token for user ${profileDoc.id}:`, error.message);
}
}
if (profileData.microsoftToken) {
try {
const refreshedMicrosoftToken = await refreshMicrosoftToken(profileData.microsoftToken);
await profileDoc.ref.update({ microsoftToken: refreshedMicrosoftToken });
console.log(`Microsoft token updated for user ${profileDoc.id}`);
} catch (error) {
console.error(`Error refreshing Microsoft token for user ${profileDoc.id}:`, error.message);
}
}
if (profileData.appleToken) {
try {
const refreshedAppleToken = await refreshAppleToken(profileData.appleToken);
await profileDoc.ref.update({ appleToken: refreshedAppleToken });
console.log(`Apple token updated for user ${profileDoc.id}`);
} catch (error) {
console.error(`Error refreshing Apple token for user ${profileDoc.id}:`, error.message);
}
}
});
return null;
});
// Function to refresh Google token
async function refreshGoogleToken(token) {
// Assuming you use OAuth2 token refresh flow
const response = await axios.post('https://oauth2.googleapis.com/token', {
grant_type: 'refresh_token',
refresh_token: token, // Add refresh token stored previously
client_id: 'YOUR_GOOGLE_CLIENT_ID',
client_secret: 'YOUR_GOOGLE_CLIENT_SECRET',
});
return response.data.access_token; // Return new access token
}
async function refreshMicrosoftToken(token) {
const response = await axios.post('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
grant_type: 'refresh_token',
refresh_token: token, // Add refresh token stored previously
client_id: 'YOUR_MICROSOFT_CLIENT_ID',
client_secret: 'YOUR_MICROSOFT_CLIENT_SECRET',
scope: 'https://graph.microsoft.com/Calendars.ReadWrite offline_access',
});
return response.data.access_token; // Return new access token
}
async function getPushTokensForEvent() {
const usersRef = db.collection('Profiles');
const snapshot = await usersRef.get();
@ -223,4 +289,4 @@ async function getPushTokensForFamilyExcludingCreator(familyId, creatorId) {
});
return pushTokens;
}
}