11
22"use strict" ;
33
4- // instantiate MSAL
4+ // Browser check variables
5+ // If you support IE, our recommendation is that you sign-in using Redirect APIs
6+ // If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
7+ const ua = window . navigator . userAgent ;
8+ const msie = ua . indexOf ( "MSIE " ) ;
9+ const msie11 = ua . indexOf ( "Trident/" ) ;
10+ const msedge = ua . indexOf ( "Edge/" ) ;
11+ const isIE = msie > 0 || msie11 > 0 ;
12+ const isEdge = msedge > 0 ;
13+
14+ let signInType ;
15+
16+ // Create the main myMSALObj instance
17+ // configuration parameters are located at authConfig.js
518const myMSALObj = new Msal . UserAgentApplication ( msalConfig ) ;
619
7- // sign-in and acquire a token silently with popup flow.
8- // Fall back in case of failure with silent acquisition to popup.
9- function signIn ( ) {
20+ // Register Callbacks for Redirect flow
21+ myMSALObj . handleRedirectCallback ( authRedirectCallBack ) ;
22+
23+ function authRedirectCallBack ( error , response ) {
24+ if ( error ) {
25+ console . log ( error ) ;
26+ } else {
27+ if ( response . tokenType === "id_token" && myMSALObj . getAccount ( ) && ! myMSALObj . isCallback ( window . location . hash ) ) {
28+ console . log ( 'id_token acquired at: ' + new Date ( ) . toString ( ) ) ;
29+ updateUI ( ) ;
30+ getTokenRedirect ( loginRequest ) ;
31+ } else if ( response . tokenType === "access_token" ) {
32+ console . log ( 'access_token acquired at: ' + new Date ( ) . toString ( ) ) ;
33+ } else {
34+ console . log ( "token type is:" + response . tokenType ) ;
35+ }
36+ }
37+ }
38+
39+ // Redirect: once login is successful and redirects with tokens, call Graph API
40+ if ( myMSALObj . getAccount ( ) && ! myMSALObj . isCallback ( window . location . hash ) ) {
41+ // avoid duplicate code execution on page load in case of iframe and Popup window.
42+ updateUI ( ) ;
43+ }
44+
45+
46+ function signIn ( method ) {
47+
48+ signInType = isIE ? "Redirect" : method ;
49+
50+ if ( signInType === "Popup" ) {
1051 myMSALObj . loginPopup ( loginRequest )
11- . then ( loginResponse => {
12- getToken ( tokenRequest )
13- . then ( updateUI ) ;
14- } ) . catch ( error => {
15- logMessage ( error ) ;
52+ . then ( loginResponse => {
53+ console . log ( 'id_token acquired at: ' + new Date ( ) . toString ( ) ) ;
54+ if ( myMSALObj . getAccount ( ) ) {
55+ updateUI ( ) ;
56+ }
57+ } ) . catch ( function ( error ) {
58+ console . log ( error ) ;
1659 } ) ;
60+
61+ } else if ( signInType === "Redirect" ) {
62+ myMSALObj . loginRedirect ( loginRequest )
63+ }
64+ }
65+
66+ // sign-out the user
67+ function logout ( ) {
68+ // Removes all sessions, need to call AAD endpoint to do full logout
69+ myMSALObj . logout ( ) ;
1770}
1871
19- //acquire a token silently
20- function getToken ( tokenRequest ) {
21- return myMSALObj . acquireTokenSilent ( tokenRequest )
72+ function getTokenPopup ( request ) {
73+ return myMSALObj . acquireTokenSilent ( request )
2274 . catch ( error => {
23- console . log ( "Silent token acquisition fails. acquiring token using popup" ) ;
24-
75+ console . log ( "silent token acquisition fails. acquiring token using popup" ) ;
2576 // fallback to interaction when silent call fails
26- return myMSALObj . acquireTokenPopup ( tokenRequest )
77+ return myMSALObj . acquireTokenPopup ( request )
2778 . then ( tokenResponse => {
2879 } ) . catch ( error => {
29- logMessage ( "Failed token acquisition" , error ) ;
80+ console . log ( error ) ;
3081 } ) ;
3182 } ) ;
32- }
33-
34- // sign-out the user
35- function logout ( ) {
36- // Removes all sessions, need to call AAD endpoint to do full logout
37- myMSALObj . logout ( ) ;
38- }
83+ }
84+
85+ // This function can be removed if you do not need to support IE
86+ function getTokenRedirect ( request ) {
87+ return myMSALObj . acquireTokenSilent ( request )
88+ . then ( ( response ) => {
89+ } ) . catch ( error => {
90+ console . log ( "silent token acquisition fails. acquiring token using redirect" ) ;
91+ // fallback to interaction when silent call fails
92+ return myMSALObj . acquireTokenRedirect ( request )
93+ } ) ;
94+ }
0 commit comments