Skip to content

Commit a2a07fc

Browse files
authored
Removed Teams dependency and simplified Teams sample (#90)
* published 0.1.0 * removed teams dependency and simplified teams sample
1 parent 25f514a commit a2a07fc

File tree

10 files changed

+104
-73
lines changed

10 files changed

+104
-73
lines changed

docs/providers/Teams.md

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,58 @@ author: nmetulev
77

88
# Microsoft Teams provider
99

10-
Use the Teams provider inside your Microsoft Teams App, inside a Tab, to power it with Microsoft Graph access.
10+
Use the Teams provider inside your Microsoft Teams Tab to facilitate authentication and Microsoft Graph access to all components.
1111

12-
Visit [the authentication docs](../providers.md) to learn more about the role of providers in the Microsoft Graph Toolkit
12+
Visit [the authentication docs](../providers.md) to learn more about the role of providers in the Microsoft Graph Toolkit.
1313

1414
## Getting started
1515

16-
Initialize the provider inside your html using the `mgt-teams-provider` component.
16+
Before using the Teams provider, you will need to make sure you have referenced the [Microsoft Teams SDK](https://docs.microsoft.com/en-us/javascript/api/overview/msteams-client?view=msteams-client-js-latest#using-the-sdk) in your page.
17+
18+
Here is an example using the provider in HTML (via CDN):
1719

1820
```html
19-
<mgt-teams-provider
20-
client-id="<YOUR_CLIENT_ID>"
21-
auth-popup-url="https://<YOUR-DOMAIN>.com/AUTH-PATH"
22-
></mgt-teams-provider>
21+
<!-- Microsoft Teams sdk must be referenced before the toolkit -->
22+
<script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
23+
<script src="https://unpkg.com/@microsoft/mgt/dist/bundle/mgt-loader.js"></script>
24+
25+
<mgt-teams-provider
26+
client-id="<YOUR_CLIENT_ID>"
27+
auth-popup-url="https://<YOUR-DOMAIN>.com/AUTH-PATH"
28+
></mgt-teams-provider>
29+
30+
```
31+
32+
Here is an example using the provider in JS modules (via NPM):
33+
34+
Make sure to install both the toolkit and the Microsoft Teams sdk
35+
36+
```bash
37+
npm install @microsoft/mgt @microsoft/teams-js
38+
```
39+
40+
Then import and use the provider
41+
42+
```ts
43+
import '@microsoft/teams-js';
44+
import {Providers, TeamsProvider} from '@microsoft/mgt';
45+
46+
Providers.globalProvider = new TeamsProvider(config);
47+
```
48+
49+
where `config` is
50+
51+
```ts
52+
export interface TeamsConfig {
53+
clientId: string;
54+
authPopupUrl: string;
55+
scopes?: string[];
56+
msalOptions?: Configuration;
57+
}
2358
```
2459

60+
See [sample](https://github.com/microsoftgraph/microsoft-graph-toolkit/tree/master/samples/teams-tab) for full example
61+
2562
## Configuring your Teams App
2663

2764
If you are just getting started with Teams Apps, you can follow the getting started guide [here](https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/tabs/tabs-overview). You could also use the [App Studio](https://docs.microsoft.com/en-us/microsoftteams/platform/get-started/get-started-app-studio) to quickly develop your app manifest for Microsoft Teams.
@@ -42,20 +79,14 @@ Make sure to enable Implicit Grant Flow as this is a requirement for web apps re
4279

4380
### Create the popup page
4481

45-
In order to login with your Teams credentials, you need to provide a URL that the Teams App with open in a popup, which will follow the authentication flow. This URL needs to be in your domain, and it needs to call the `TeamsProvider.handleAuth();` method. That's the only thing that this page needs to do. For example (if you are using parcel to bundle your app) - `auth.html`:
82+
In order to login with your Teams credentials, you need to provide a URL that the Teams App with open in a popup, which will follow the authentication flow. This URL needs to be in your domain, and it needs to call the `TeamsProvider.handleAuth();` method. That's the only thing that this page needs to do. For example:
4683

4784
```html
48-
<html>
49-
<head>
50-
<script src="./node_modules/@microsoft/mgt/dist/es6/index.js"></script>
51-
<script>
52-
var teamsProvider = parcelRequire('node_modules/@microsoft/mgt/dist/es6/index.js');
53-
teamsProvider.TeamsProvider.handleAuth();
54-
</script>
55-
</head>
56-
</html>
85+
<script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
86+
<script src="https://unpkg.com/@microsoft/mgt/dist/bundle/mgt-loader.js">
87+
</script>
88+
mgt.TeamsProvider.handleAuth();
89+
</script>
5790
```
5891

59-
### Configure Redirect URIs
60-
61-
After you publish this page in your website, you need to get it's URL and use it in the `auth-popup-url` property. This URL also needs to be configured as a valid redirect URI at your app configuration, at the AAD portal.
92+
After you publish this page in your website, you need to get it's URL and use it in the `auth-popup-url/authPopupUrl` property. This URL also needs to be configured as a valid redirect URI at your app configuration, at the AAD portal.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/mgt",
3-
"version": "0.1.0-preview.8",
3+
"version": "0.1.0",
44
"description": "The Microsoft Graph Toolkit",
55
"keywords": [
66
"microsoft graph",
@@ -49,7 +49,6 @@
4949
"@microsoft/microsoft-graph-client": "^1.7.0-Preview.1",
5050
"@microsoft/microsoft-graph-types": "^1.8.0",
5151
"@microsoft/microsoft-graph-types-beta": "microsoftgraph/msgraph-typescript-typings#beta",
52-
"@microsoft/teams-js": "^1.4.1",
5352
"lit-element": "^2.1.0",
5453
"msal": "^1.0.0",
5554
"office-ui-fabric-core": "10.0.0"

samples/teams-tab/MySampleApp.zip

-3.27 KB
Binary file not shown.

samples/teams-tab/auth.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<html>
22
<head>
3-
<script src="./node_modules/@microsoft/mgt/dist/es6/index.js"></script>
3+
<script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
4+
<script src="./bundle/mgt-loader.js"></script>
45
<script>
5-
var mgt = parcelRequire('node_modules/@microsoft/mgt/dist/es6/index.js');
6-
mgt.TeamsProvider.auth();
6+
mgt.TeamsProvider.handleAuth();
77
</script>
88
</head>
9+
<body></body>
910
</html>

samples/teams-tab/index.html

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<html>
2+
<head>
3+
<script src="https://unpkg.com/@microsoft/teams-js/dist/MicrosoftTeams.min.js" crossorigin="anonymous"></script>
4+
<script src="https://unpkg.com/@microsoft/mgt/dist/bundle/mgt-loader.js"></script>
5+
</head>
26
<body>
3-
<head>
4-
<script src="./node_modules/@microsoft/mgt/dist/es6/index.js"></script>
5-
</head>
6-
<mgt-teams-provider
7-
client-id="ded74c58-5126-42b0-be48-c3971d8c8e98"
8-
login-popup-url="https://mgtsampleapps.z5.web.core.windows.net/auth.html"
9-
></mgt-teams-provider>
10-
<mgt-login style="float: right;"></mgt-login>
11-
<mgt-person person-id="alex"></mgt-person>
12-
<mgt-agenda id="myDay"></mgt-agenda>
7+
<mgt-teams-provider client-id="<CLIENT-ID>" auth-popup-url="auth.html"></mgt-teams-provider>
8+
<mgt-login></mgt-login>
9+
<mgt-agenda group-by-day></mgt-agenda>
1310
</body>
1411
</html>

samples/teams-tab/package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

samples/teams-tab/readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Microsoft Teams Tab sample
2+
3+
For the quickest way to run this sample, you will need:
4+
5+
* Visual Studio Code
6+
* [Live Server extension in Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer)
7+
* [ngrok](https://ngrok.com/)
8+
9+
1. Visit the [Microsoft identity platform documentation](https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app) to learn more about creating a new application and retrieving a clientId. Add the client id in `index.html`
10+
11+
> Note: Msal only supports the Implicit Flow for OAuth. Make sure to enable Implicit Flow in your application in the Azure Portal (it is not enabled by default). Under `Authentication`, find the `Implicit grant` section and check both checkboxes for `Access tokens` and `ID tokens`
12+
13+
2. Open `index.html` with Live Server and start `ngrok` in terminal/cmd
14+
15+
```bash
16+
ngrok http [port]
17+
```
18+
19+
Live Server default port is `8080`
20+
21+
4. Go back to the application registration in the Azure portal and add the redirect url. The redirect url is your ngrok url with the auth page appended.
22+
23+
Ex: `https://b5d9404b.ngrok.io/auth.html`
24+
25+
5. Now you can use [App Studio](https://docs.microsoft.com/en-us/microsoftteams/platform/get-started/get-started-app-studio) to quickly develop your app manifest for Microsoft Teams and test the app. Use the ngrok url for the tab url.

src/providers/MsalProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class MsalProvider extends IProvider {
7777
if (this._userAgentApplication.isCallback(window.location.hash)) {
7878
return;
7979
}
80-
if (this._userAgentApplication.getAccount()) {
80+
if (this._userAgentApplication.getAccount() && (await this.getAccessToken(null))) {
8181
this.setState(ProviderState.SignedIn);
8282
} else {
8383
this.setState(ProviderState.SignedOut);

src/providers/TeamsProvider.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client/lib/es/IAuthenticationProviderOptions';
99
import { MsalProvider } from './MsalProvider';
10-
import * as microsoftTeams from '@microsoft/teams-js';
1110
import { LoginType, ProviderState } from './IProvider';
1211
import { Configuration, UserAgentApplication } from 'msal';
1312

13+
declare var microsoftTeams: any;
14+
1415
export interface TeamsConfig {
1516
clientId: string;
1617
authPopupUrl: string;
@@ -36,29 +37,16 @@ export class TeamsProvider extends MsalProvider {
3637
return this._accessToken;
3738
}
3839

39-
static async isAvailable(): Promise<boolean> {
40-
const ms = 1000;
41-
return Promise.race([
42-
new Promise<boolean>((resolve, reject) => {
43-
microsoftTeams.initialize();
44-
microsoftTeams.getContext(function(context) {
45-
if (context) {
46-
resolve(true);
47-
} else {
48-
resolve(false);
49-
}
50-
});
51-
}),
52-
new Promise<boolean>((resolve, reject) => {
53-
let id = setTimeout(() => {
54-
clearTimeout(id);
55-
resolve(false);
56-
}, ms);
57-
})
58-
]);
40+
static async isAvailable() {
41+
return !!microsoftTeams;
5942
}
6043

6144
static handleAuth() {
45+
if (!this.isAvailable) {
46+
console.error('Make sure you have referenced the Microsoft Teams sdk before using the TeamsProvider');
47+
return;
48+
}
49+
6250
// we are in popup world now - authenticate and handle it
6351

6452
var url = new URL(window.location.href);
@@ -117,6 +105,11 @@ export class TeamsProvider extends MsalProvider {
117105
options: config.msalOptions
118106
});
119107

108+
if (!TeamsProvider.isAvailable) {
109+
console.error('Make sure you have referenced the Microsoft Teams sdk before using the TeamsProvider');
110+
return;
111+
}
112+
120113
this._authPopupUrl = config.authPopupUrl;
121114
microsoftTeams.initialize();
122115
this.accessToken = sessionStorage.getItem(this._sessionStorageTokenKey);

src/utils/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// THIS FILE IS AUTO GENERATED
99
// ANY CHANGES WILL BE LOST DURING BUILD
1010

11-
export const PACKAGE_VERSION = '0.1.0-preview.8';
11+
export const PACKAGE_VERSION = '0.1.0';

0 commit comments

Comments
 (0)