Skip to content

Commit 52a0877

Browse files
author
David Inga
committed
added docs
1 parent ba33cc1 commit 52a0877

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

Diff for: README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ npm install --save passport-control-tower
1313

1414
```js
1515
passport.use(new ControlTowerStrategy({
16-
apiUrl: '[CONTROL_TOWER_API_URL]',
16+
controlTowerUrl: '[CONTROL_TOWER_API_URL]',
1717
callbackUrl: '[YOUR_CALLBACK_URL]'
1818
}));
1919
```
2020

21-
2221
### Examples of use:
2322

2423
* [Express JS](./examples/express-example.js)

Diff for: examples/express-example.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ const app = express();
88
function isAuthenticated(req, res, next) {
99
if (req.isAuthenticated()) return next();
1010
// if they aren't redirect them to the home page
11-
res.redirect('/login');
11+
res.redirect('/');
1212
}
1313

1414
// Use the Control Tower Strategy within Passport.
15-
passport.use(new ControlTowerStrategy({
16-
apiUrl: '[CONTROL_TOWER_API_URL]',
17-
callbackUrl: '[YOUR_CALLBACK_URL]'
18-
}));
15+
const controlTowerStrategy = new ControlTowerStrategy({
16+
controlTowerUrl: '[CONTROL_TOWER_API_URL]',
17+
callbackUrl: '[YOUR_CALLBACK_URL]' // auth path
18+
});
19+
passport.use(controlTowerStrategy);
1920

2021
// Passport session setup.
2122
// To support persistent login sessions, Passport needs to be able to
@@ -46,11 +47,16 @@ app.get('/private', isAuthenticated, function (req, res) {
4647
});
4748

4849
// This should be callback URL
49-
app.get('/login', passport.authenticate('control-tower'), function (req, res) {
50+
app.get('/auth', passport.authenticate('control-tower'), function (req, res) {
5051
// Success
5152
res.redirect('/private');
5253
});
5354

55+
app.get('/login', function(req, res) {
56+
controlTowerStrategy.login(req, res);
57+
});
58+
59+
5460
app.get('/logout', function (req, res) {
5561
req.session.destroy();
5662
req.logout();

Diff for: examples/next-example.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express');
22
const passport = require('passport');
33
const next = require('next');
44
const session = require('express-session');
5+
const cookieSession = require('cookie-session');
56
const ControlTowerStrategy = require('passport-control-tower');
67
const cookieParser = require('cookie-parser');
78
const bodyParser = require('body-parser');
@@ -21,14 +22,14 @@ const server = express();
2122
function isAuthenticated(req, res, nextAction) {
2223
if (req.isAuthenticated()) return nextAction();
2324
// if they aren't redirect them to the home page
24-
res.redirect('/login');
25+
res.redirect('/');
2526
}
26-
2727
// Use the Control Tower Strategy within Passport.
28-
passport.use(new ControlTowerStrategy({
29-
apiUrl: process.env.CONTROL_TOWER_API_URL,
30-
callbackUrl: process.env.CALLBACK_URL
31-
}));
28+
const controlTowerStrategy = new ControlTowerStrategy({
29+
controlTowerUrl: '[CONTROL_TOWER_API_URL]',
30+
callbackUrl: '[YOUR_CALLBACK_URL]' // auth path
31+
});
32+
passport.use(controlTowerStrategy);
3233

3334
// Passport session setup.
3435
// To support persistent login sessions, Passport needs to be able to
@@ -44,10 +45,14 @@ passport.deserializeUser(function (obj, done) {
4445
server.use(cookieParser());
4546
server.use(bodyParser.urlencoded({ extended: false }));
4647
server.use(bodyParser.json());
48+
server.use(cookieSession({
49+
name: 'session',
50+
keys: [process.env.SECRET || 'keyboard cat']
51+
}));
4752
server.use(session({
4853
secret: process.env.SECRET || 'keyboard cat',
4954
resave: false,
50-
saveUninitialized: false
55+
saveUninitialized: true
5156
}));
5257
// Initialize Passport! Also use passport.session() middleware, to support
5358
// persistent login sessions (recommended).
@@ -62,18 +67,19 @@ app.prepare()
6267
return app.render(req, res, '/landing');
6368
});
6469

65-
server.get('/login', passport.authenticate('control-tower'), function (req, res) {
66-
// Success
67-
res.redirect('/admin');
70+
server.get('/login', function(req, res) {
71+
controlTowerStrategy.login(req, res);
6872
});
6973

7074
server.get('/logout', function (req, res) {
71-
req.session.destroy();
7275
req.logout();
73-
// Success
7476
res.redirect('/');
7577
});
7678

79+
server.get('/auth', passport.authenticate('control-tower', { failureRedirect: '/' }), function (req, res) {
80+
res.redirect('/admin');
81+
});
82+
7783
server.get('/admin', isAuthenticated, function (req, res) {
7884
const parsedUrl = parse(req.url, true);
7985
return handle(req, res, parsedUrl);

0 commit comments

Comments
 (0)