Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
adding support for redirects that are the same route but have a custo…
Browse files Browse the repository at this point in the history
…m handler. Closes #121
  • Loading branch information
ekryski committed Mar 24, 2016
1 parent f7fa8cc commit b3bd9a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const PROVIDERS = {
// Options that apply to any provider
const defaults = {
idField: '_id',
shouldSetupSuccessRoute: true,
shouldSetupFailureRoute: true,
successRedirect: '/auth/success',
failureRedirect: '/auth/failure',
tokenEndpoint: '/auth/token',
Expand Down Expand Up @@ -52,27 +54,17 @@ export default function auth(config = {}) {

// Merge and flatten options
const authOptions = Object.assign({}, defaults, app.get('auth'), config);

// If we should redirect on success and the redirect route is the same as the
// default then we'll set up a route handler. Otherwise we'll leave it to the developer
// to set up their own custom route handler.
if (authOptions.successRedirect === defaults.successRedirect) {
debug(`Setting up successRedirect route: ${authOptions.successRedirect}`);

app.get(authOptions.successRedirect, function(req, res){
res.sendFile(path.resolve(__dirname, 'public', 'auth-success.html'));
});

// If a custom success redirect is passed in or it is disabled then we
// won't setup the default route handler.
if (authOptions.successRedirect !== defaults.successRedirect) {
authOptions.shouldSetupSuccessRoute = false;
}

// If we should redirect on failure and the redirect route is the same as the
// default then we'll set up a route handler. Otherwise we'll leave it to the developer
// to set up their own custom route handler.
if (authOptions.failureRedirect === defaults.failureRedirect) {
debug(`Setting up failureRedirect route: ${authOptions.failureRedirect}`);

app.get(authOptions.failureRedirect, function(req, res){
res.sendFile(path.resolve(__dirname, 'public', 'auth-fail.html'));
});
// If a custom failure redirect is passed in or it is disabled then we
// won't setup the default route handler.
if (authOptions.failureRedirect !== defaults.failureRedirect) {
authOptions.shouldSetupFailureRoute = false;
}

// Set the options on the app
Expand Down Expand Up @@ -145,6 +137,24 @@ export default function auth(config = {}) {
// Register error handling middleware for redirecting to support
// redirecting on authentication failure.
app.use(middleware.failedLogin(authOptions));

// Setup route handler for default success redirect
if (authOptions.shouldSetupSuccessRoute) {
debug(`Setting up successRedirect route: ${authOptions.successRedirect}`);

app.get(authOptions.successRedirect, function(req, res){
res.sendFile(path.resolve(__dirname, 'public', 'auth-success.html'));
});
}

// Setup route handler for default failure redirect
if (authOptions.shouldSetupFailureRoute) {
debug(`Setting up failureRedirect route: ${authOptions.failureRedirect}`);

app.get(authOptions.failureRedirect, function(req, res){
res.sendFile(path.resolve(__dirname, 'public', 'auth-fail.html'));
});
}
};
}

Expand Down
10 changes: 10 additions & 0 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('Feathers Authentication', () => {
expect(app.get('auth').idField).to.equal('_id');
});

it('sets shouldSetupSuccessRoute', () => {
expect(app.get('auth').shouldSetupSuccessRoute).to.equal(true);
});

it('sets shouldSetupFailureRoute', () => {
expect(app.get('auth').shouldSetupFailureRoute).to.equal(true);
});

it('sets successRedirect', () => {
expect(app.get('auth').successRedirect).to.equal('/auth/success');
});
Expand Down Expand Up @@ -210,11 +218,13 @@ describe('Feathers Authentication', () => {
it('allows disabling successRedirect', () => {
app.configure(authentication({ successRedirect: false }));
expect(app.get('auth').successRedirect).to.equal(false);
expect(app.get('auth').shouldSetupSuccessRoute).to.equal(false);
});

it('allows disabling failureRedirect', () => {
app.configure(authentication({ failureRedirect: false }));
expect(app.get('auth').failureRedirect).to.equal(false);
expect(app.get('auth').shouldSetupFailureRoute).to.equal(false);
});

it('allows overriding tokenEndpoint', () => {
Expand Down

0 comments on commit b3bd9a2

Please sign in to comment.