GithubHelp home page GithubHelp logo

teamsapp's Introduction

Handling authentication in custom Teams tabs using Microsoft Graph Toolkit

If you are looking for ways to build easy authentication in Microsoft Teams custom tab development, Microsoft Graph Toolkit Login component provides a button and flyout control to facilitate Microsoft identity platform authentication with couple of lines of code.

How to build a tab with straightforward authentication flow?

  1. Enable Microsoft Teams Toolkit extension for Visual Studio Code
  2. Build Microsoft Teams tab
  3. Implement Microsoft Graph Toolkit:
  4. Setup ngrok for tunneling
  5. Register your app in Azure Active Directory
  6. Import your app manifest to Microsoft Teams App Studio for testing

Enable Microsoft Teams Toolkit extension for Visual Studio Code

Install Microsoft Teams Toolkit from the Extensions tab on the left side bar in Visual Studio Code. For more information, Microsoft Teams Toolkit: Setup and Overview.

Microsoft Teams Toolkit Extension for Visual Studio Code

Build Microsoft Teams tab

  1. Select Microsoft Teams icon on the left side bar in Visual Studio Code and Sign in.

    Microsoft Teams Toolkit Extension for Visual Studio Code

  2. Select Create a new Teams app,

    • Give a name for the app
    • Choose Tab for the capability
    • Select Next

    Microsoft Teams Toolkit Extension for Visual Studio Code

    • Select Personal tab
    • Select Finish

    Microsoft Teams Toolkit Extension for Visual Studio Code

    • Open Terminal and run:

      npm install

      npm install @microsoft/mgt @microsoft/teams-js

      npm start

Add a new file under src folder and name it as Auth.js.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App';
import { Provider, themes } from '@fluentui/react-northstar' //https://fluentsite.z22.web.core.windows.net/quick-start

ReactDOM.render(
    <Provider theme={themes.teams}>
        <App />
    </Provider>, document.getElementById('auth')
);

Add a new file under public folder and name as auth.html. CTRL+SPACE for adding HTML Sample. Add below code in <body></body>

<div id="auth"></div>
<script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@microsoft/mgt/dist/bundle/mgt-loader.js"></script>
<script>
  mgt.TeamsProvider.handleAuth();
</script>

Add below code in index.html <body></body>

 <script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
 <script src="https://unpkg.com/@microsoft/mgt/dist/bundle/mgt-loader.js"></script>
 <mgt-teams-provider client-id="YOUR-CLIENT-ID" auth-popup-url="YOUR-NGROK-URL/auth.html"></mgt-teams-provider> 
 <mgt-login></mgt-login>
 <mgt-agenda></mgt-agenda>

Setup ngrok for tunneling

  1. Open Terminal and run the solution. Default Teams tab will be running https://localhost:3000:

    npm start

  2. Go to ngrok website and login.

  3. Complete the setup and installation guide.

  4. Save Authtoken in the default configuration file C:\Users\[user name]\.ngrok:

    ngrok authtoken <YOUR_AUTHTOKEN>

  5. Run below script to create ngrok tunnel for https://localhost:3000:

    ngrok http https://localhost:3000

    Ngrok Setup

    Ngrok Setup

  6. Go to your project .publish > Development.env, replace baseUrl0 with ngrok url https://xxxxxxxxxxxx.ngrok.io.

    Ngrok Setup

  7. Go to your project public > index.html, replace YOUR-NGROK-URL with ngrok url https://xxxxxxxxxxxx.ngrok.io in mgt-teams-provider > auth-popup-url.

    Ngrok Setup

Register your app in Azure Active Directory

  1. Go to Azure Portal, then Azure Active Directory > App Registration and select New Registration.

    Ngrok Setup

  2. Fill the details to register an app:

    • give a name to your application
    • select Accounts in any organizational directory as an access level
    • place auth-popup-url as the redirect url https://xxxxxxxxxxxx.ngrok.io/auth.html
    • select Register

    Ngrok Setup

  3. Go to Authentication tab and enable Implicit grant by selecting Access tokens and ID tokens

    Ngrok Setup

  4. Go to API permissions tab, select Add a permission > Microsoft Graph > Delegated permissions and add Calendar.Read, Calendar.ReadWrite.

  5. Then, select Grant admin consent.

    Ngrok Setup

  6. Go to Overview tab and copy Application (client) ID

  7. Then go to your project public > index.html, replace YOUR-CLIENT-ID with Application (client) ID in mgt-teams-provider > auth-popup-url.

    Ngrok Setup

    Ngrok Setup

Import your app manifest to Microsoft Teams App Studio for testing

  1. Go to Microsoft Teams, open App Studio > Manifest Editor and select Import an existing app.

    Ngrok Setup

  2. Select Development.zip under your project folder > .publish.

    Ngrok Setup

  3. Scroll down and select Test and distribute, then click Install and Add your app.

    Ngrok Setup

    Ngrok Setup

  4. Click on Sign in for the authentication and give consent to AAD registered app you created.

    Ngrok Setup

  5. Your profile information e-mail, name and calendar should appear in your tab after the successful authentication.

    Ngrok Setup

teamsapp's People

Contributors

aycabas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.