Skip to content

Commit afca5ee

Browse files
committed
Completed the add MS Graph step
1 parent d67532e commit afca5ee

6 files changed

Lines changed: 186 additions & 155 deletions

File tree

demo/graph-tutorial/app.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function signInComplete(iss, sub, profile, accessToken, refreshToken, para
7070
let oauthToken = oauth2.accessToken.create(params);
7171

7272
// Save the profile and tokens in user storage
73-
users[profile.oid] = { profile, accessToken };
73+
users[profile.oid] = { profile, oauthToken };
7474
return done(null, users[profile.oid]);
7575
}
7676
// </SignInCompleteSnippet>
@@ -95,6 +95,7 @@ passport.use(new OIDCStrategy(
9595
var indexRouter = require('./routes/index');
9696
var usersRouter = require('./routes/users');
9797
var authRouter = require('./routes/auth');
98+
var calendarRouter = require('./routes/calendar');
9899
var graph = require('./graph');
99100

100101
var app = express();
@@ -134,6 +135,15 @@ app.use(function(req, res, next) {
134135
app.set('views', path.join(__dirname, 'views'));
135136
app.set('view engine', 'hbs');
136137

138+
// <FormatDateSnippet>
139+
var hbs = require('hbs');
140+
var moment = require('moment');
141+
// Helper to format date/time sent by Graph
142+
hbs.registerHelper('eventDateTime', function(dateTime){
143+
return moment(dateTime).format('M/D/YY h:mm A');
144+
});
145+
// </FormatDateSnippet>
146+
137147
app.use(logger('dev'));
138148
app.use(express.json());
139149
app.use(express.urlencoded({ extended: false }));
@@ -157,6 +167,7 @@ app.use(function(req, res, next) {
157167

158168
app.use('/', indexRouter);
159169
app.use('/auth', authRouter);
170+
app.use('/calendar', calendarRouter);
160171
app.use('/users', usersRouter);
161172

162173
// catch 404 and forward to error handler

demo/graph-tutorial/graph.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ module.exports = {
1010

1111
const user = await client.api('/me').get();
1212
return user;
13+
},
14+
15+
// <GetEventsSnippet>
16+
getEvents: async function(accessToken) {
17+
const client = getAuthenticatedClient(accessToken);
18+
19+
const events = await client
20+
.api('/me/events')
21+
.select('subject,organizer,start,end')
22+
.orderby('createdDateTime DESC')
23+
.get();
24+
25+
return events;
1326
}
27+
// </GetEventsSnippet>
1428
};
1529

1630
function getAuthenticatedClient(accessToken) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
var express = require('express');
5+
var router = express.Router();
6+
var tokens = require('../tokens.js');
7+
var graph = require('../graph.js');
8+
9+
/* GET /calendar */
10+
// <GetRouteSnippet>
11+
router.get('/',
12+
async function(req, res) {
13+
if (!req.isAuthenticated()) {
14+
// Redirect unauthenticated requests to home page
15+
res.redirect('/')
16+
} else {
17+
let params = {
18+
active: { calendar: true }
19+
};
20+
21+
// Get the access token
22+
var accessToken;
23+
try {
24+
accessToken = await tokens.getAccessToken(req);
25+
} catch (err) {
26+
req.flash('error_msg', {
27+
message: 'Could not get access token. Try signing out and signing in again.',
28+
debug: JSON.stringify(err)
29+
});
30+
}
31+
32+
if (accessToken && accessToken.length > 0) {
33+
try {
34+
// Get the events
35+
var events = await graph.getEvents(accessToken);
36+
params.events = events.value;
37+
} catch (err) {
38+
req.flash('error_msg', {
39+
message: 'Could not fetch events',
40+
debug: JSON.stringify(err)
41+
});
42+
}
43+
} else {
44+
req.flash('error_msg', 'Could not get an access token');
45+
}
46+
47+
res.render('calendar', params);
48+
}
49+
}
50+
);
51+
// </GetRouteSnippet>
52+
53+
module.exports = router;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!-- Copyright (c) Microsoft Corporation.
2+
Licensed under the MIT License. -->
3+
4+
<!-- <LayoutSnippet> -->
5+
<h1>Calendar</h1>
6+
<table class="table">
7+
<thead>
8+
<tr>
9+
<th scope="col">Organizer</th>
10+
<th scope="col">Subject</th>
11+
<th scope="col">Start</th>
12+
<th scope="col">End</th>
13+
</tr>
14+
</thead>
15+
<tbody>
16+
{{#each events}}
17+
<tr>
18+
<td>{{this.organizer.emailAddress.name}}</td>
19+
<td>{{this.subject}}</td>
20+
<td>{{eventDateTime this.start.dateTime}}</td>
21+
<td>{{eventDateTime this.end.dateTime}}</td>
22+
</tr>
23+
{{/each}}
24+
</tbody>
25+
</table>
26+
<!-- </LayoutSnippet> -->

tutorial/04-add-aad-auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ However, storing just the access token doesn't allow you to check expiration or
242242
243243
1. Replace the existing `signInComplete` function with the following.
244244
245-
:::code language="javascript" source="../demo/graph-tutorial/app.js" id="SignInCompleteSnippet" highlight="17-18":::
245+
:::code language="javascript" source="../demo/graph-tutorial/app.js" id="SignInCompleteSnippet" highlight="17-18, 21":::
246246
247247
1. Replace the existing callback route in `./routes/auth.js` with the following.
248248

0 commit comments

Comments
 (0)