Last active
August 1, 2024 13:34
-
-
Save xar/fca77246add11cd9748881d154afdab6 to your computer and use it in GitHub Desktop.
ms-teams-init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SendErrorToSentry } from '~/plugins/sentry.client'; | |
import { app, authentication } from '@microsoft/teams-js'; | |
async function isInMobileTeamsApp() { | |
await app.initialize(); | |
const context = await app.getContext(); | |
const userAgent = navigator.userAgent || navigator.vendor || window.opera; | |
if (!context?.app?.appLaunchId) { | |
return false; | |
} | |
// Check for mobile-specific keywords in the user agent | |
const isMobile = /TeamsMobile|TeamsiOS|TeamsAndroid/i.test(userAgent); | |
console.log('Is in mobile Teams app:', isMobile); | |
return isMobile; | |
} | |
export function useMSTeams() { | |
const isInTeams = useState('isInTeams', () => false); | |
const teamsInitialized = useState('teamsInitialized', () => false); | |
async function initializeMsTeams() { | |
try { | |
if (teamsInitialized.value) return; | |
await app.initialize(); | |
const context = await app.getContext(); | |
isInTeams.value = true; | |
} catch (e) { | |
console.error(e); | |
isInTeams.value = false; | |
SendErrorToSentry(e); | |
} | |
} | |
async function startAuthFlow() { | |
try { | |
const authResult = await authentication.authenticate({ | |
url: '/auth?useRedirect=true&isInTeams=true', | |
isExternal: await isInMobileTeamsApp(), | |
}); | |
console.log(authResult); | |
} catch (e) { | |
console.error(e); | |
SendErrorToSentry(e); | |
} | |
} | |
return { | |
isInTeams, | |
initializeMsTeams, | |
startAuthFlow, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment