Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.

Commit ac41ceb

Browse files
committed
Implemented sign-in URL
1 parent 6662066 commit ac41ceb

6 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎app.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ var logger = require('morgan');
55
var cookieParser = require('cookie-parser');
66
var bodyParser = require('body-parser');
77

8+
require('dotenv').config();
9+
810
var index = require('./routes/index');
911
var users = require('./routes/users');
1012

11-
require('dotenv').config();
1213
var app = express();
1314

1415
// view engine setup

‎helpers/auth.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const credentials = {
2+
client: {
3+
id: process.env.APP_ID,
4+
secret: process.env.APP_PASSWORD,
5+
},
6+
auth: {
7+
tokenHost: 'https://login.microsoftonline.com',
8+
authorizePath: 'common/oauth2/v2.0/authorize',
9+
tokenPath: 'common/oauth2/v2.0/token'
10+
}
11+
};
12+
const oauth2 = require('simple-oauth2').create(credentials);
13+
14+
function getAuthUrl() {
15+
const returnVal = oauth2.authorizationCode.authorizeURL({
16+
redirect_uri: process.env.REDIRECT_URI,
17+
scope: process.env.APP_SCOPES
18+
});
19+
console.log(`Generated auth url: ${returnVal}`);
20+
return returnVal;
21+
}
22+
23+
exports.getAuthUrl = getAuthUrl;

‎routes/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
var express = require('express');
22
var router = express.Router();
3+
var authHelper = require('../helpers/auth');
34

45
/* GET home page. */
56
router.get('/', function(req, res, next) {
6-
res.render('index', { title: 'Express' });
7+
const signInUrl = authHelper.getAuthUrl();
8+
res.render('index', { title: 'Home', signInUrl: signInUrl });
79
});
810

911
module.exports = router;

‎sample.env‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
APP_ID="YOUR APP ID HERE"
22
APP_PASSWORD="YOUR APP PASSWORD HERE"
33
APP_SCOPES="openid User.Read Mail.Read"
4-
REDIRECT_URI="http://localhost:3000/"
4+
REDIRECT_URI="http://localhost:3000/authorize"

‎views/index.hbs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
{{#if user}}
55
<p>Welcome {{user.displayName}}</p>
66
{{else}}
7-
<a class="btn btn-primary btn-large" href="#">Click here to login</a>
7+
<a class="btn btn-primary btn-large" href="{{signInUrl}}">Click here to login</a>
88
{{/if}}
99
</div>

‎views/layout.hbs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</li>
3737
{{else}}
3838
<li class="nav-item">
39-
<a class="nav-link" href="#">Sign In</a>
39+
<a class="nav-link" href="{{signInUrl}}">Sign In</a>
4040
</li>
4141
{{/if}}
4242
</ul>

0 commit comments

Comments
 (0)