88/**
99 * This sample is referenced from - https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/manual/authorizationCodeSample.ts
1010 */
11+ import "isomorphic-fetch" ;
12+
1113import { AuthorizationCodeCredential } from "@azure/identity" ;
14+ import { Client } from "@microsoft/microsoft-graph-client" ;
15+ import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials" ;
1216import express from "express" ;
1317import { Server } from "http" ;
14- import "isomorphic-fetch" ;
1518import open from "open" ;
1619import qs from "qs" ;
1720
18- import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials" ;
19- import { Client } from "@microsoft/microsoft-graph-client" ;
20- import { port , tenantId , clientSecret , clientId , scopes , authorityHost , redirectUri } from "./secrets" ;
21+ import { authorityHost , clientId , clientSecret , port , redirectUri , scopes , tenantId } from "./secrets" ;
2122
2223if ( tenantId === undefined || clientId === undefined ) {
2324 console . error ( "AZURE_TENANT_ID and AZURE_CLIENT_ID environment variables must be set" ) ;
@@ -40,8 +41,7 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
4041 // authentication redirect to be sent to the local redirect URI.
4142 const authCodePromise = new Promise < string > ( ( resolve , reject ) => {
4243 const app = express ( ) ;
43- let server : Server | undefined = undefined ;
44-
44+ const server : Server = app . listen ( port , ( ) => console . log ( `Authorization code redirect server listening on port ${ port } ` ) ) ;
4545 app . get ( "/authresponse" , ( req , res ) => {
4646 // Close the temporary server once we've received the redirect.
4747 res . sendStatus ( 200 ) ;
@@ -57,14 +57,12 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
5757 reject ( new Error ( `Authentication Error "${ req . query [ "error" ] } ":\n\n${ req . query [ "error_description" ] } ` ) ) ;
5858 }
5959 } ) ;
60-
61- server = app . listen ( port , ( ) => console . log ( `Authorization code redirect server listening on port ${ port } ` ) ) ;
6260 } ) ;
6361
6462 // Direct the user to the authentication URI either by opening a
6563 // browser (desktop and mobile apps) or redirecting their browser
6664 // using a Location header (web apps and APIs).
67- const authorizeUrl = getAuthorizeUrl ( tenantId ! , clientId ! , scopes ) ;
65+ const authorizeUrl = getAuthorizeUrl ( tenantId , clientId , scopes ) ;
6866 console . log ( "Opening user's browser to URL:" , authorizeUrl ) ;
6967 await open ( authorizeUrl ) ;
7068
@@ -77,8 +75,8 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
7775 // refreshing the access token from this point forward.
7876 if ( clientSecret ) {
7977 return new AuthorizationCodeCredential (
80- tenantId ! ,
81- clientId ! ,
78+ tenantId ,
79+ clientId ,
8280 clientSecret ,
8381 authorizationCode ,
8482 redirectUri ,
@@ -92,8 +90,8 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
9290 // NOTE: If there is no client secret, we can construct an auth code credential
9391 // using this method.
9492 return new AuthorizationCodeCredential (
95- tenantId ! ,
96- clientId ! ,
93+ tenantId ,
94+ clientId ,
9795 authorizationCode ,
9896 redirectUri ,
9997 // NOTE: It is not necessary to explicitly pass the authorityHost when using
0 commit comments