mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 08:24:55 +00:00
- Prepared methods for microsoft authorization and added a button to handle microsoft auth
- Saved the microsoft token to the profile of the current user
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
export async function fetchGoogleCalendarEvents(token, startDate) {
|
export async function fetchGoogleCalendarEvents(token, startDate, endDate) {
|
||||||
const response = await fetch(
|
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: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import { useCreateEvent } from "@/hooks/firebase/useCreateEvent";
|
|||||||
import { useAuthContext } from "@/contexts/AuthContext";
|
import { useAuthContext } from "@/contexts/AuthContext";
|
||||||
import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData";
|
import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData";
|
||||||
import { GoogleSignin } from "@react-native-google-signin/google-signin";
|
import { GoogleSignin } from "@react-native-google-signin/google-signin";
|
||||||
|
import { authorize } from "react-native-app-auth";
|
||||||
|
|
||||||
GoogleSignin.configure({
|
GoogleSignin.configure({
|
||||||
webClientId:
|
webClientId:
|
||||||
@ -21,6 +22,25 @@ const GoogleLogin = async () => {
|
|||||||
return await GoogleSignin.signIn();
|
return await GoogleSignin.signIn();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const microsoftConfig = {
|
||||||
|
issuer: "https://login.microsoftonline.com/common",
|
||||||
|
clientId: "<your-client-id>", // Replace with your microsoft client id
|
||||||
|
redirectUrl: "<your-redirect-uri>", // 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: {
|
const CalendarSettingsPage = (props: {
|
||||||
setSelectedPage: (page: number) => void;
|
setSelectedPage: (page: number) => void;
|
||||||
}) => {
|
}) => {
|
||||||
@ -32,16 +52,21 @@ const CalendarSettingsPage = (props: {
|
|||||||
const { mutateAsync: updateUserData } = useUpdateUserData();
|
const { mutateAsync: updateUserData } = useUpdateUserData();
|
||||||
|
|
||||||
const fetchAndSaveGoogleEvents = () => {
|
const fetchAndSaveGoogleEvents = () => {
|
||||||
const timeMin = new Date(new Date().setHours(0, 0, 0, 0)).toISOString();
|
const timeMin = new Date(new Date().setHours(0, 0, 0, 0));
|
||||||
|
const timeMax = new Date(
|
||||||
fetchGoogleCalendarEvents(profileData?.googleToken, timeMin).then(
|
new Date(new Date().setHours(0, 0, 0, 0)).setDate(timeMin.getDate() + 30),
|
||||||
(response) => {
|
|
||||||
response?.forEach((item) => createEvent(item));
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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 startDateTime = new Date(new Date().setHours(0, 0, 0, 0));
|
||||||
const endDateTime = new Date(
|
const endDateTime = new Date(
|
||||||
new Date(new Date().setHours(0, 0, 0, 0)).setDate(
|
new Date(new Date().setHours(0, 0, 0, 0)).setDate(
|
||||||
@ -50,7 +75,7 @@ const CalendarSettingsPage = (props: {
|
|||||||
);
|
);
|
||||||
|
|
||||||
fetchMicrosoftCalendarEvents(
|
fetchMicrosoftCalendarEvents(
|
||||||
token,
|
profileData?.microsoftToken,
|
||||||
startDateTime.toISOString().slice(0, -5) + "Z",
|
startDateTime.toISOString().slice(0, -5) + "Z",
|
||||||
endDateTime.toISOString().slice(0, -5) + "Z",
|
endDateTime.toISOString().slice(0, -5) + "Z",
|
||||||
).then((response) => {
|
).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 (
|
return (
|
||||||
<View marginH-30>
|
<View marginH-30>
|
||||||
<TouchableOpacity onPress={() => props.setSelectedPage(0)}>
|
<TouchableOpacity onPress={() => props.setSelectedPage(0)}>
|
||||||
@ -178,6 +214,25 @@ const CalendarSettingsPage = (props: {
|
|||||||
borderRadius={15}
|
borderRadius={15}
|
||||||
onPress={handleGoogleLogin}
|
onPress={handleGoogleLogin}
|
||||||
/>
|
/>
|
||||||
|
<Button
|
||||||
|
label={"Connect Google"}
|
||||||
|
iconSource={() => (
|
||||||
|
<View
|
||||||
|
backgroundColor="#ededed"
|
||||||
|
width={40}
|
||||||
|
height={40}
|
||||||
|
style={{ borderRadius: 50 }}
|
||||||
|
centerV
|
||||||
|
centerH
|
||||||
|
>
|
||||||
|
<Ionicons name="logo-microsoft" size={22} color="#979797" />
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
backgroundColor="white"
|
||||||
|
color="#464039"
|
||||||
|
borderRadius={15}
|
||||||
|
onPress={handleMicrosoftSignIn}
|
||||||
|
/>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
<View style={styles.card}>
|
<View style={styles.card}>
|
||||||
@ -205,6 +260,7 @@ const CalendarSettingsPage = (props: {
|
|||||||
onPress={fetchAndSaveGoogleEvents}
|
onPress={fetchAndSaveGoogleEvents}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{profileData?.microsoftToken !== undefined && (
|
||||||
<Button
|
<Button
|
||||||
label={"Sync Outlook"}
|
label={"Sync Outlook"}
|
||||||
iconSource={() => (
|
iconSource={() => (
|
||||||
@ -225,6 +281,7 @@ const CalendarSettingsPage = (props: {
|
|||||||
borderRadius={15}
|
borderRadius={15}
|
||||||
onPress={fetchAndSaveMicrosoftEvents}
|
onPress={fetchAndSaveMicrosoftEvents}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
@ -18,6 +18,7 @@ export interface UserProfile {
|
|||||||
familyId?: string;
|
familyId?: string;
|
||||||
uid?: string;
|
uid?: string;
|
||||||
googleToken?: string;
|
googleToken?: string;
|
||||||
|
microsoftToken?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ParentProfile extends UserProfile {
|
export interface ParentProfile extends UserProfile {
|
||||||
|
|||||||
@ -60,6 +60,7 @@
|
|||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"react-dom": "18.2.0",
|
"react-dom": "18.2.0",
|
||||||
"react-native": "0.74.3",
|
"react-native": "0.74.3",
|
||||||
|
"react-native-app-auth": "^8.0.0",
|
||||||
"react-native-big-calendar": "^4.14.0",
|
"react-native-big-calendar": "^4.14.0",
|
||||||
"react-native-calendars": "^1.1306.0",
|
"react-native-calendars": "^1.1306.0",
|
||||||
"react-native-gesture-handler": "~2.16.1",
|
"react-native-gesture-handler": "~2.16.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user