@@ -316,71 +316,110 @@ The snippets below demonstrates the minimal code required for a single-page appl
316
316
``` html
317
317
318
318
<head >
319
- <meta charset =" UTF-8" >
320
- <meta http-equiv =" X-UA-Compatible" content =" IE=edge" >
321
- <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
322
-
323
- <script
324
- type =" text/javascript"
325
- src =" https://secure.aadcdn.microsoftonline-p.com/lib/1.0.18/js/adal.min.js" >
326
- </script >
319
+ <meta charset =" UTF-8" >
320
+ <meta http-equiv =" X-UA-Compatible" content =" IE=edge" >
321
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
322
+ <script type =" text/javascript" src =" https://alcdn.msauth.net/lib/1.0.18/js/adal.min.js" ></script >
327
323
</head >
328
324
329
- <div >
330
- <button id =" loginButton" >Login</button >
331
- <button id =" logoutButton" style =" visibility : hidden ;" >Logout</button >
332
- <button id =" tokenButton" style =" visibility : hidden ;" >Get Token</button >
333
- </div >
334
-
335
325
<body >
336
- <script >
337
-
338
- const loginButton = document .getElementById (" loginButton" );
339
- const logoutButton = document .getElementById (" logoutButton" );
340
- const tokenButton = document .getElementById (" tokenButton" );
341
-
342
- var authContext = new AuthenticationContext ({
343
- instance: ' https://login.microsoftonline.com/' ,
344
- clientId: " ENTER_CLIENT_ID" ,
345
- tenant: " ENTER_TENANT_ID" ,
346
- cacheLocation: " sessionStorage" ,
347
- redirectUri: " http://localhost:3000" ,
348
- popUp: true ,
349
- callback : function (errorDesc , token , error , tokenType ) {
350
- console .log (' Hello ' + authContext .getCachedUser ().profile .upn )
326
+ <div >
327
+ <p id =" welcomeMessage" style =" visibility : hidden ;" ></p >
328
+ <button id =" loginButton" >Login</button >
329
+ <button id =" logoutButton" style =" visibility : hidden ;" >Logout</button >
330
+ <button id =" tokenButton" style =" visibility : hidden ;" >Get Token</button >
331
+ </div >
332
+ <script >
333
+ // DOM elements to work with
334
+ var welcomeMessage = document .getElementById (" welcomeMessage" );
335
+ var loginButton = document .getElementById (" loginButton" );
336
+ var logoutButton = document .getElementById (" logoutButton" );
337
+ var tokenButton = document .getElementById (" tokenButton" );
338
+
339
+ // if user is logged in, update the UI
340
+ function updateUI (user ) {
341
+ if (! user) {
342
+ return ;
343
+ }
351
344
352
- loginButton .style .visibility = " hidden" ;
345
+ welcomeMessage .innerHTML = ' Hello ' + user .profile .upn + ' !' ;
346
+ welcomeMessage .style .visibility = " visible" ;
353
347
logoutButton .style .visibility = " visible" ;
354
348
tokenButton .style .visibility = " visible" ;
355
- }
356
- });
357
-
358
- authContext .log ({
359
- level: 3 ,
360
- log : function (message ) {
361
- console .log (message);
362
- },
363
- piiLoggingEnabled: false
364
- });
365
-
366
- loginButton .addEventListener (' click' , function () {
367
- authContext .login ();
368
- });
369
-
370
- logoutButton .addEventListener (' click' , function () {
371
- authContext .logOut ();
372
- });
373
-
374
- tokenButton .addEventListener (' click' , () => {
375
- authContext .acquireTokenPopup (
376
- " https://graph.microsoft.com" ,
377
- null , null ,
378
- function (error , token ) {
379
- console .log (error, token);
349
+ loginButton .style .visibility = " hidden" ;
350
+ };
351
+
352
+ // attach logger configuration to window
353
+ window .Logging = {
354
+ piiLoggingEnabled: false ,
355
+ level: 3 ,
356
+ log : function (message ) {
357
+ console .log (message);
380
358
}
381
- )
382
- });
383
- </script >
359
+ };
360
+
361
+ // ADAL configuration
362
+ var adalConfig = {
363
+ instance: ' https://login.microsoftonline.com/' ,
364
+ clientId: " ENTER_CLIENT_ID_HERE" ,
365
+ tenant: " ENTER_TENANT_ID_HERE" ,
366
+ redirectUri: " ENTER_REDIRECT_URI_HERE" ,
367
+ cacheLocation: " sessionStorage" ,
368
+ popUp: true ,
369
+ callback : function (errorDesc , token , error , tokenType ) {
370
+ if (error) {
371
+ console .log (error, errorDesc);
372
+ } else {
373
+ updateUI (authContext .getCachedUser ());
374
+ }
375
+ }
376
+ };
377
+
378
+ // instantiate ADAL client object
379
+ var authContext = new AuthenticationContext (adalConfig);
380
+
381
+ // handle redirect response or check for cached user
382
+ if (authContext .isCallback (window .location .hash )) {
383
+ authContext .handleWindowCallback ();
384
+ } else {
385
+ updateUI (authContext .getCachedUser ());
386
+ }
387
+
388
+ // attach event handlers to button clicks
389
+ loginButton .addEventListener (' click' , function () {
390
+ authContext .login ();
391
+ });
392
+
393
+ logoutButton .addEventListener (' click' , function () {
394
+ authContext .logOut ();
395
+ });
396
+
397
+ tokenButton .addEventListener (' click' , () => {
398
+ authContext .acquireToken (
399
+ " https://graph.microsoft.com" ,
400
+ function (errorDesc , token , error ) {
401
+ if (error) {
402
+ console .log (error, errorDesc);
403
+
404
+ authContext .acquireTokenPopup (
405
+ " https://graph.microsoft.com" ,
406
+ null , // extraQueryParameters
407
+ null , // claims
408
+ function (errorDesc , token , error ) {
409
+ if (error) {
410
+ console .log (error, errorDesc);
411
+ } else {
412
+ console .log (token);
413
+ }
414
+ }
415
+ );
416
+ } else {
417
+ console .log (token);
418
+ }
419
+ }
420
+ );
421
+ });
422
+ </script >
384
423
</body >
385
424
386
425
</html >
@@ -393,72 +432,111 @@ The snippets below demonstrates the minimal code required for a single-page appl
393
432
``` html
394
433
395
434
<head >
396
- <meta charset =" UTF-8" >
397
- <meta http-equiv =" X-UA-Compatible" content =" IE=edge" >
398
- <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
399
-
400
- <script
401
- type =" text/javascript"
402
- src =" https://alcdn.msauth.net/browser/2.14.2/js/msal-browser.min.js" >
403
- </script >
435
+ <meta charset =" UTF-8" >
436
+ <meta http-equiv =" X-UA-Compatible" content =" IE=edge" >
437
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
438
+ <script type =" text/javascript" src =" https://alcdn.msauth.net/browser/2.34.0/js/msal-browser.min.js" ></script >
404
439
</head >
405
440
406
- <div >
407
- <button id =" loginButton" >Login</button >
408
- <button id =" logoutButton" style =" visibility : hidden ;" >Logout</button >
409
- <button id =" tokenButton" style =" visibility : hidden ;" >Get Token</button >
410
- </div >
411
-
412
441
<body >
413
- <script >
414
- const loginButton = document .getElementById (" loginButton" );
415
- const logoutButton = document .getElementById (" logoutButton" );
416
- const tokenButton = document .getElementById (" tokenButton" );
417
-
418
- const pca = new msal.PublicClientApplication ({
419
- auth: {
420
- clientId: " ENTER_CLIENT_ID" ,
421
- authority: " https://login.microsoftonline.com/ENTER_TENANT_ID" ,
422
- redirectUri: " http://localhost:3000" ,
423
- },
424
- cache: {
425
- cacheLocation: " sessionStorage"
426
- },
427
- system: {
428
- loggerOptions: {
429
- loggerCallback (loglevel , message , containsPii ) {
430
- console .log (message);
431
- },
432
- piiLoggingEnabled: false ,
433
- logLevel: msal .LogLevel .Verbose ,
442
+ <div >
443
+ <p id =" welcomeMessage" style =" visibility : hidden ;" ></p >
444
+ <button id =" loginButton" >Login</button >
445
+ <button id =" logoutButton" style =" visibility : hidden ;" >Logout</button >
446
+ <button id =" tokenButton" style =" visibility : hidden ;" >Get Token</button >
447
+ </div >
448
+ <script >
449
+ // DOM elements to work with
450
+ const welcomeMessage = document .getElementById (" welcomeMessage" );
451
+ const loginButton = document .getElementById (" loginButton" );
452
+ const logoutButton = document .getElementById (" logoutButton" );
453
+ const tokenButton = document .getElementById (" tokenButton" );
454
+
455
+ // if user is logged in, update the UI
456
+ const updateUI = (account ) => {
457
+ if (! account) {
458
+ return ;
434
459
}
435
- }
436
- });
437
-
438
- loginButton .addEventListener (' click' , () => {
439
- pca .loginPopup ().then ((response ) => {
440
- console .log (` Hello ${ response .account .username } !` );
441
460
442
- loginButton .style .visibility = " hidden" ;
461
+ welcomeMessage .innerHTML = ` Hello ${ account .username } !` ;
462
+ welcomeMessage .style .visibility = " visible" ;
443
463
logoutButton .style .visibility = " visible" ;
444
464
tokenButton .style .visibility = " visible" ;
445
- })
446
- });
447
-
448
- logoutButton .addEventListener (' click' , () => {
449
- pca .logoutPopup ().then ((response ) => {
450
- window .location .reload ();
451
- })
452
- });
453
-
454
- tokenButton .addEventListener (' click' , () => {
455
- pca .acquireTokenPopup ({
456
- scopes: [" User.Read" ]
457
- }).then ((response ) => {
458
- console .log (response);
459
- })
460
- });
461
- </script >
465
+ loginButton .style .visibility = " hidden" ;
466
+ };
467
+
468
+ // MSAL configuration
469
+ const msalConfig = {
470
+ auth: {
471
+ clientId: " ENTER_CLIENT_ID_HERE" ,
472
+ authority: " https://login.microsoftonline.com/ENTER_TENANT_ID_HERE" ,
473
+ redirectUri: " ENTER_REDIRECT_URI_HERE" ,
474
+ },
475
+ cache: {
476
+ cacheLocation: " sessionStorage"
477
+ },
478
+ system: {
479
+ loggerOptions: {
480
+ loggerCallback (loglevel , message , containsPii ) {
481
+ console .log (message);
482
+ },
483
+ piiLoggingEnabled: false ,
484
+ logLevel: msal .LogLevel .Verbose ,
485
+ }
486
+ }
487
+ };
488
+
489
+ // instantiate MSAL client object
490
+ const pca = new msal.PublicClientApplication (msalConfig);
491
+
492
+ // handle redirect response or check for cached user
493
+ pca .handleRedirectPromise ().then ((response ) => {
494
+ if (response) {
495
+ pca .setActiveAccount (response .account );
496
+ updateUI (response .account );
497
+ } else {
498
+ const account = pca .getAllAccounts ()[0 ];
499
+ updateUI (account);
500
+ }
501
+ }).catch ((error ) => {
502
+ console .log (error);
503
+ });
504
+
505
+ // attach event handlers to button clicks
506
+ loginButton .addEventListener (' click' , () => {
507
+ pca .loginPopup ().then ((response ) => {
508
+ pca .setActiveAccount (response .account );
509
+ updateUI (response .account );
510
+ })
511
+ });
512
+
513
+ logoutButton .addEventListener (' click' , () => {
514
+ pca .logoutPopup ().then ((response ) => {
515
+ window .location .reload ();
516
+ });
517
+ });
518
+
519
+ tokenButton .addEventListener (' click' , () => {
520
+ const account = pca .getActiveAccount ();
521
+
522
+ pca .acquireTokenSilent ({
523
+ account: account,
524
+ scopes: [" User.Read" ]
525
+ }).then ((response ) => {
526
+ console .log (response);
527
+ }).catch ((error ) => {
528
+ if (error instanceof msal .InteractionRequiredAuthError ) {
529
+ pca .acquireTokenPopup ({
530
+ scopes: [" User.Read" ]
531
+ }).then ((response ) => {
532
+ console .log (response);
533
+ });
534
+ }
535
+
536
+ console .log (error);
537
+ });
538
+ });
539
+ </script >
462
540
</body >
463
541
464
542
</html >
0 commit comments