Skip to content

Commit 6abf8ff

Browse files
Pr/582 continued (#618)
* Update Angular sample to use MsalAngular * remove direct dep on msal * pass scopes and loginType from msal-angular config into MsalProvider * Use MsalModule.forRoot instead of injection tokens * Remove unused values from msal-angular config * Clean up diff * Integrated angular app with locally built packages * Updated client id in angular sample * Updated angular app readme Co-authored-by: Matthew Haugen <[email protected]>
1 parent 218d888 commit 6abf8ff

File tree

10 files changed

+101
-42
lines changed

10 files changed

+101
-42
lines changed

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"packages": ["packages/*", "samples/react-app"],
2+
"packages": ["packages/*", "samples/react-app", "samples/angular-app"],
33
"npmClient": "yarn",
44
"useWorkspaces": true,
55
"version": "independent"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"workspaces": [
55
"packages/*",
6-
"samples/react-app"
6+
"samples/react-app",
7+
"samples/angular-app"
78
],
89
"scripts": {
910
"build": "npm run prettier:check && npm run clean && lerna run build --scope @microsoft/*",

samples/angular-app/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ This project was generated with [Angular CLI](https://github.com/angular/angular
55
## Getting Started
66
The is the sample code explained in [A Lap Around the Mircosoft Graph Toolkit Day 14 - How to use Microsoft Graph Toolkit with Angular](https://developer.microsoft.com/en-us/graph/blogs/a-lap-around-microsoft-graph-toolkit-day-14-using-microsoft-graph-toolkit-with-angular/)
77

8-
1. Run `npm install` (to download all dependant npm packages)
9-
2. Create an Azure App Registration with the following Graph API permissions
10-
* openid
11-
* profile
12-
* user.read
13-
* calendars.read
14-
2. Modify src/app/app.component.ts and replace [YOUR-CLIENT-ID] with an Azure App Registration client id
15-
3. Run `ng serve` for a local dev server and navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
8+
> NOTE: This sample is linked to the locally built mgt packages. If you haven't already, run `npm i -g yarn` in the root of the repo before getting started.
9+
10+
1. In a web browser, navigate to the Azure Portal and create a new [App Registration](http://aka.ms/AppRegistrations) with the following Graph API permissions:
11+
* `openid`
12+
* `profile`
13+
* `user.read`
14+
* `calendars.read`
15+
1. Record the **Client Id** value for later.
16+
1. Open up a terminal instance at the repo root.
17+
1. `yarn` to download all dependant npm packages.
18+
1. `yarn build` to build the local mgt packages.
19+
1. `cd ./samples/angular-app` to navigate to the angular sample folder.
20+
1. Modify `src/app/app.component.ts` by replacing `[YOUR-CLIENT-ID]` with the Azure App Registration **Client Id** from earlier.
21+
1. Run `ng start` for a local dev server and navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.

samples/angular-app/package-lock.json

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/angular-app/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"@angular/platform-browser": "~9.1.6",
2020
"@angular/platform-browser-dynamic": "~9.1.6",
2121
"@angular/router": "~9.1.6",
22-
"@microsoft/mgt": "^1.3.0",
22+
"@azure/msal-angular": "^1.0.0",
23+
"@microsoft/mgt": "*",
24+
"@microsoft/mgt-element": "*",
2325
"rxjs": "~6.5.4",
2426
"tslib": "^1.10.0",
2527
"zone.js": "~0.10.2"

samples/angular-app/src/app/angular-agenda/angular-agenda.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { Component, OnInit, ViewChild } from '@angular/core';
1+
import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';
2+
import { MgtAgenda } from '@microsoft/mgt';
23

34
@Component({
45
selector: 'app-angular-agenda',
56
templateUrl: './angular-agenda.component.html',
67
styleUrls: ['./angular-agenda.component.scss']
78
})
8-
export class AngularAgendaComponent implements OnInit {
9-
@ViewChild('myagenda', {static: true}) agendaElement: any;
9+
export class AngularAgendaComponent implements AfterViewInit {
10+
@ViewChild('myagenda', {static: true})
11+
agendaElement: ElementRef<MgtAgenda>;
1012

1113
constructor() { }
1214

13-
ngOnInit() {
15+
ngAfterViewInit() {
1416
this.agendaElement.nativeElement.templateContext = {
1517
openWebLink: (e: any, context: { event: { webLink: string | undefined; }; }, root: any) => {
1618
window.open(context.event.webLink, '_blank');
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
<app-nav-bar></app-nav-bar>
2-
<app-angular-agenda *ngIf="this.isLoggedIn()"></app-angular-agenda>
2+
<app-angular-agenda *ngIf="this.isLoggedIn(); else: notLoggedIn"></app-angular-agenda>
3+
4+
<ng-template #notLoggedIn>
5+
<div class="container mx-auto" style="margin-top: 20px">
6+
<div class="flex items-center bg-blue-500 text-white text-sm font-bold px-4 py-3" role="alert">
7+
<svg class="fill-current w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
8+
<path
9+
d="M12.432 0c1.34 0 2.01.912 2.01 1.957 0 1.305-1.164 2.512-2.679 2.512-1.269 0-2.009-.75-1.974-1.99C9.789 1.436 10.67 0 12.432 0zM8.309 20c-1.058 0-1.833-.652-1.093-3.524l1.214-5.092c.211-.814.246-1.141 0-1.141-.317 0-1.689.562-2.502 1.117l-.528-.88c2.572-2.186 5.531-3.467 6.801-3.467 1.057 0 1.233 1.273.705 3.23l-1.391 5.352c-.246.945-.141 1.271.106 1.271.317 0 1.357-.392 2.379-1.207l.6.814C12.098 19.02 9.365 20 8.309 20z" />
10+
</svg>
11+
<p>You aren't logged in!.</p>
12+
</div>
13+
</div>
14+
</ng-template>
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { Providers, MsalProvider, TemplateHelper, ProviderState} from '@microsoft/mgt';
2+
import { MsalService } from '@azure/msal-angular';
3+
import { MsalProvider } from '@microsoft/mgt';
4+
import { LoginType, Providers, ProviderState, TemplateHelper } from '@microsoft/mgt-element';
5+
import { MSALAngularConfig } from '../environments/environment.msal';
36

47
@Component({
58
selector: 'app-root',
@@ -9,13 +12,19 @@ import { Providers, MsalProvider, TemplateHelper, ProviderState} from '@microsof
912
export class AppComponent implements OnInit {
1013
title = 'demo-mgt-angular';
1114

12-
public isLoggedIn(){
13-
return Providers.globalProvider.state === ProviderState.SignedIn;
14-
}
15+
constructor(msalService: MsalService) {
16+
Providers.globalProvider = new MsalProvider({
17+
userAgentApplication: msalService,
18+
scopes: MSALAngularConfig.consentScopes,
19+
loginType: MSALAngularConfig.popUp === true ? LoginType.Popup : LoginType.Redirect
20+
});
1521

16-
ngOnInit()
17-
{
18-
Providers.globalProvider = new MsalProvider({ clientId: '[YOUR-CLIENT-ID]' });
1922
TemplateHelper.setBindingSyntax('[[', ']]');
2023
}
24+
25+
public isLoggedIn() {
26+
return Providers.globalProvider.state === ProviderState.SignedIn;
27+
}
28+
29+
public ngOnInit() {}
2130
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1+
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
12
import { BrowserModule } from '@angular/platform-browser';
2-
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
3-
3+
import { RouterModule } from '@angular/router';
4+
import { MsalModule } from '@azure/msal-angular';
5+
import { MSALAngularConfig as MsalAngularConfig, MsalConfig } from '../environments/environment.msal';
6+
import { AngularAgendaComponent } from './angular-agenda/angular-agenda.component';
47
import { AppComponent } from './app.component';
58
import { NavBarComponent } from './nav-bar/nav-bar.component';
6-
import { AngularAgendaComponent } from './angular-agenda/angular-agenda.component';
79

810
@NgModule({
9-
declarations: [
10-
AppComponent,
11-
NavBarComponent,
12-
AngularAgendaComponent
13-
],
14-
imports: [
15-
BrowserModule
16-
],
11+
declarations: [AppComponent, NavBarComponent, AngularAgendaComponent],
12+
imports: [BrowserModule, MsalModule.forRoot(MsalConfig, MsalAngularConfig), RouterModule.forRoot([])],
1713
schemas: [CUSTOM_ELEMENTS_SCHEMA],
1814
providers: [],
1915
bootstrap: [AppComponent]
2016
})
21-
export class AppModule { }
17+
export class AppModule {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// update these values with ones your AAD Client Id
2+
3+
import { MsalAngularConfiguration } from '@azure/msal-angular';
4+
import { Configuration } from 'msal';
5+
6+
const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1;
7+
8+
export const MsalConfig: Configuration = {
9+
auth: {
10+
clientId: 'a974dfa0-9f57-49b9-95db-90f04ce2111a',
11+
authority: 'https://login.microsoftonline.com/common/',
12+
validateAuthority: true,
13+
redirectUri: 'http://localhost:4200/',
14+
postLogoutRedirectUri: 'http://localhost:4200/',
15+
navigateToLoginRequestUrl: true
16+
},
17+
cache: {
18+
cacheLocation: 'localStorage',
19+
storeAuthStateInCookie: isIE // set to true for IE 11
20+
}
21+
};
22+
23+
export const MSALAngularConfig: MsalAngularConfiguration = {
24+
popUp: !isIE,
25+
consentScopes: ['user.read', 'openid', 'profile']
26+
};

0 commit comments

Comments
 (0)