@@ -12,7 +12,14 @@ import {
1212 GraphEndpoint ,
1313 MICROSOFT_GRAPH_DEFAULT_ENDPOINT
1414} from '@microsoft/mgt-element' ;
15- import { TeamsFx , TeamsUserCredential } from '@microsoft/teamsfx' ;
15+ import { TokenCredential } from '@azure/core-auth' ;
16+
17+ /**
18+ * Interface represents TeamsUserCredential in TeamsFx library
19+ */
20+ export interface TeamsFxUserCredential extends TokenCredential {
21+ login ( scopes : string | string [ ] , resources ?: string [ ] ) : Promise < void > ;
22+ }
1623
1724/**
1825 * TeamsFx Provider handler
@@ -32,16 +39,6 @@ export class TeamsFxProvider extends IProvider {
3239 return 'MgtTeamsFxProvider' ;
3340 }
3441
35- /**
36- * returns teamsfx instance, if you construct TeamsFxProvider with TeamsUserCredential, this value should be null
37- *
38- * @readonly
39- * @memberof TeamsFxProvider
40- */
41- public get teamsfx ( ) : TeamsFx {
42- return this . _teamsfx ;
43- }
44-
4542 /**
4643 * Privilege level for authentication
4744 *
@@ -53,20 +50,12 @@ export class TeamsFxProvider extends IProvider {
5350 private scopes : string | string [ ] = [ ] ;
5451
5552 /**
56- * TeamsUserCredential instance
57- *
58- * @type {TeamsFx }
59- * @memberof TeamsFxProvider
60- */
61- private readonly _credential : TeamsUserCredential ;
62-
63- /**
64- * TeamsFx instance
53+ * TeamsFxUserCredential instance
6554 *
6655 * @type {TeamsFx }
6756 * @memberof TeamsFxProvider
6857 */
69- private readonly _teamsfx : TeamsFx ;
58+ private readonly _credential : TeamsFxUserCredential ;
7059
7160 /**
7261 * Access token provided by TeamsFx
@@ -76,19 +65,40 @@ export class TeamsFxProvider extends IProvider {
7665 */
7766 private _accessToken : string = '' ;
7867
79- constructor ( teamsfx : TeamsFx , scopes : string | string [ ] , baseURL ?: GraphEndpoint ) ;
80- constructor ( teamsUserCredential : TeamsUserCredential , scopes : string | string [ ] , baseURL ?: GraphEndpoint ) ;
81- constructor ( authConfig : TeamsFx | TeamsUserCredential , scopes : string | string [ ] , baseURL ?: GraphEndpoint ) {
68+ /**
69+ * Constructor of TeamsFxProvider.
70+ *
71+ * @example
72+ * ```typescript
73+ * import {Providers} from '@microsoft/mgt-element';
74+ * import {TeamsFxProvider} from '@microsoft/mgt-teamsfx-provider';
75+ * import {TeamsUserCredential, TeamsUserCredentialAuthConfig} from "@microsoft/teamsfx";
76+ *
77+ * const authConfig: TeamsUserCredentialAuthConfig = {
78+ * clientId: process.env.REACT_APP_CLIENT_ID,
79+ * initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
80+ * };
81+ * const scope = ["User.Read"];
82+ *
83+ * const credential = new TeamsUserCredential(authConfig);
84+ * const provider = new TeamsFxProvider(credential, scope);
85+ * Providers.globalProvider = provider;
86+ * ```
87+ *
88+ * @param {TeamsFxUserCredential } credential - TeamsUserCredential instance in TeamsFx library.
89+ * @param {string | string[] } scopes - The list of scopes for which the token will have access.
90+ * @param {GraphEndpoint } baseURL - Graph endpoint.
91+ *
92+ */
93+ constructor (
94+ credential : TeamsFxUserCredential ,
95+ scopes : string | string [ ] ,
96+ baseURL : GraphEndpoint = MICROSOFT_GRAPH_DEFAULT_ENDPOINT
97+ ) {
8298 super ( ) ;
8399
84- if ( ! this . _teamsfx && ! this . _credential ) {
85- if ( ( authConfig as TeamsFx ) . getCredential ) {
86- this . _teamsfx = authConfig as TeamsFx ;
87- this . _credential = null ;
88- } else {
89- this . _credential = authConfig as TeamsUserCredential ;
90- this . _teamsfx = null ;
91- }
100+ if ( ! this . _credential ) {
101+ this . _credential = credential ;
92102 }
93103
94104 this . validateScopesType ( scopes ) ;
@@ -101,12 +111,7 @@ export class TeamsFxProvider extends IProvider {
101111 this . scopes = scopesArr ;
102112 }
103113
104- if ( baseURL ) {
105- this . baseURL = baseURL ;
106- } else {
107- this . baseURL = MICROSOFT_GRAPH_DEFAULT_ENDPOINT ;
108- }
109-
114+ this . baseURL = baseURL ;
110115 this . graph = createFromProvider ( this ) ;
111116 }
112117
@@ -118,12 +123,7 @@ export class TeamsFxProvider extends IProvider {
118123 */
119124 public async getAccessToken ( ) : Promise < string > {
120125 try {
121- let accessToken ;
122- if ( this . _teamsfx ) {
123- accessToken = await this . _teamsfx . getCredential ( ) . getToken ( this . scopes ) ;
124- } else {
125- accessToken = await this . _credential . getToken ( this . scopes ) ;
126- }
126+ const accessToken = await this . _credential . getToken ( this . scopes ) ;
127127 this . _accessToken = accessToken ? accessToken . token : '' ;
128128 if ( ! this . _accessToken ) {
129129 throw new Error ( 'Access token is null' ) ;
@@ -146,11 +146,7 @@ export class TeamsFxProvider extends IProvider {
146146 const token : string = await this . getAccessToken ( ) ;
147147
148148 if ( ! token ) {
149- if ( this . _teamsfx ) {
150- await this . _teamsfx . login ( this . scopes ) ;
151- } else {
152- await this . _credential . login ( this . scopes ) ;
153- }
149+ await this . _credential . login ( this . scopes ) ;
154150 }
155151
156152 this . _accessToken = token ?? ( await this . getAccessToken ( ) ) ;
0 commit comments