Skip to content

Commit 52c77e3

Browse files
Updated angular app sample (#638)
* Updated readme * Added reload logic to angular app
1 parent 1470239 commit 52c77e3

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

samples/angular-app/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ 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-
> 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.
8+
> NOTE: This sample is linked to the locally built mgt packages. If you haven't already, run the following commands in the root of the repo before getting started.
9+
```bash
10+
npm i -g yarn
11+
yarn
12+
yarn build
13+
```
914

1015
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:
1116
* `openid`
1217
* `profile`
1318
* `user.read`
1419
* `calendars.read`
1520
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.
21+
1. Modify `src/environment/environment.msal.ts` by replacing `[YOUR-CLIENT-ID]` with the Azure App Registration **Client Id** from earlier.
22+
1. Run `yarn 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.
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<app-nav-bar></app-nav-bar>
2-
<app-angular-agenda *ngIf="this.isLoggedIn(); else: notLoggedIn"></app-angular-agenda>
2+
<app-angular-agenda #agenda *ngIf="this.isLoggedIn; else notLoggedIn"></app-angular-agenda>
33

44
<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>
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+
/>
11+
</svg>
12+
<p>You aren't logged in!</p>
1313
</div>
14-
</ng-template>
14+
</div>
15+
</ng-template>
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
22
import { MsalService } from '@azure/msal-angular';
33
import { MsalProvider } from '@microsoft/mgt';
44
import { LoginType, Providers, ProviderState, TemplateHelper } from '@microsoft/mgt-element';
@@ -11,20 +11,24 @@ import { MSALAngularConfig } from '../environments/environment.msal';
1111
})
1212
export class AppComponent implements OnInit {
1313
title = 'demo-mgt-angular';
14+
isLoggedIn: boolean = false;
1415

15-
constructor(msalService: MsalService) {
16+
constructor(msalService: MsalService, private cd: ChangeDetectorRef) {
1617
Providers.globalProvider = new MsalProvider({
1718
userAgentApplication: msalService,
1819
scopes: MSALAngularConfig.consentScopes,
1920
loginType: MSALAngularConfig.popUp === true ? LoginType.Popup : LoginType.Redirect
2021
});
2122

22-
TemplateHelper.setBindingSyntax('[[', ']]');
23-
}
23+
Providers.globalProvider.onStateChanged(() => this.onProviderStateChanged());
2424

25-
public isLoggedIn() {
26-
return Providers.globalProvider.state === ProviderState.SignedIn;
25+
TemplateHelper.setBindingSyntax('[[', ']]');
2726
}
2827

2928
public ngOnInit() {}
29+
30+
private onProviderStateChanged() {
31+
this.isLoggedIn = Providers.globalProvider.state === ProviderState.SignedIn;
32+
this.cd.detectChanges();
33+
}
3034
}

samples/angular-app/src/environments/environment.msal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigato
77

88
export const MsalConfig: Configuration = {
99
auth: {
10-
clientId: 'a974dfa0-9f57-49b9-95db-90f04ce2111a',
10+
clientId: '[YOUR-CLIENT-ID]',
1111
authority: 'https://login.microsoftonline.com/common/',
1212
validateAuthority: true,
1313
redirectUri: 'http://localhost:4200/',

0 commit comments

Comments
 (0)