From b5a8712af8e408fa92af7b70bfa96c1632d03a61 Mon Sep 17 00:00:00 2001 From: Dejan Date: Sat, 5 Oct 2024 16:17:55 +0200 Subject: [PATCH] - Prepared methods for microsoft authorization and added a button to handle microsoft auth - Saved the microsoft token to the profile of the current user --- calendar-integration/google-calendar-utils.js | 4 +- .../pages/settings/CalendarSettingsPage.tsx | 113 +++++++++++++----- hooks/firebase/types/profileTypes.ts | 1 + package.json | 1 + 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/calendar-integration/google-calendar-utils.js b/calendar-integration/google-calendar-utils.js index c5dddbd..428cb42 100644 --- a/calendar-integration/google-calendar-utils.js +++ b/calendar-integration/google-calendar-utils.js @@ -1,6 +1,6 @@ -export async function fetchGoogleCalendarEvents(token, startDate) { +export async function fetchGoogleCalendarEvents(token, startDate, endDate) { const response = await fetch( - `https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}`, + `https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}&time_max=${endDate}`, { headers: { Authorization: `Bearer ${token}`, diff --git a/components/pages/settings/CalendarSettingsPage.tsx b/components/pages/settings/CalendarSettingsPage.tsx index 347f30f..bc31d07 100644 --- a/components/pages/settings/CalendarSettingsPage.tsx +++ b/components/pages/settings/CalendarSettingsPage.tsx @@ -10,6 +10,7 @@ import { useCreateEvent } from "@/hooks/firebase/useCreateEvent"; import { useAuthContext } from "@/contexts/AuthContext"; import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData"; import { GoogleSignin } from "@react-native-google-signin/google-signin"; +import { authorize } from "react-native-app-auth"; GoogleSignin.configure({ webClientId: @@ -21,6 +22,25 @@ const GoogleLogin = async () => { return await GoogleSignin.signIn(); }; +const microsoftConfig = { + issuer: "https://login.microsoftonline.com/common", + clientId: "", // Replace with your microsoft client id + redirectUrl: "", // replace with your redirect uri added in microsoft portal + scopes: ["openid", "profile", "email"], + serviceConfiguration: { + authorizationEndpoint: + "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", + tokenEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/token", + revocationEndpoint: + "https://login.microsoftonline.com/common/oauth2/v2.0/logout", + }, + useNonce: true, + usePKCE: true, //For iOS, we have added the useNonce and usePKCE parameters, which are recommended for security reasons. + additionalParameters: { + prompt: "consent", + }, +}; + const CalendarSettingsPage = (props: { setSelectedPage: (page: number) => void; }) => { @@ -32,16 +52,21 @@ const CalendarSettingsPage = (props: { const { mutateAsync: updateUserData } = useUpdateUserData(); const fetchAndSaveGoogleEvents = () => { - const timeMin = new Date(new Date().setHours(0, 0, 0, 0)).toISOString(); - - fetchGoogleCalendarEvents(profileData?.googleToken, timeMin).then( - (response) => { - response?.forEach((item) => createEvent(item)); - }, + const timeMin = new Date(new Date().setHours(0, 0, 0, 0)); + const timeMax = new Date( + new Date(new Date().setHours(0, 0, 0, 0)).setDate(timeMin.getDate() + 30), ); + + fetchGoogleCalendarEvents( + profileData?.googleToken, + timeMin.toISOString(), + timeMax.toISOString(), + ).then((response) => { + response?.forEach((item) => createEvent(item)); + }); }; - const fetchAndSaveMicrosoftEvents = (token: string) => { + const fetchAndSaveMicrosoftEvents = () => { const startDateTime = new Date(new Date().setHours(0, 0, 0, 0)); const endDateTime = new Date( new Date(new Date().setHours(0, 0, 0, 0)).setDate( @@ -50,7 +75,7 @@ const CalendarSettingsPage = (props: { ); fetchMicrosoftCalendarEvents( - token, + profileData?.microsoftToken, startDateTime.toISOString().slice(0, -5) + "Z", endDateTime.toISOString().slice(0, -5) + "Z", ).then((response) => { @@ -74,6 +99,17 @@ const CalendarSettingsPage = (props: { } }; + const handleMicrosoftSignIn = async () => { + try { + const { idToken } = await authorize(microsoftConfig); + if (idToken) { + await updateUserData({ newUserData: { microsoftToken: idToken } }); + } + } catch (error) { + console.log(error); + } + }; + return ( props.setSelectedPage(0)}> @@ -178,6 +214,25 @@ const CalendarSettingsPage = (props: { borderRadius={15} onPress={handleGoogleLogin} /> +