2014-06-26 12 views
5

Nie ma zbyt wielu przykładów dla hapi i wtyczki auth-cookie, ale oto, co mam do tej pory, aby zabezpieczyć trasę. Zauważ, że większość przykładów widziałem używasz starszej wersji HAPI które nie wydają się dość zastosowanie do tej sytuacji i im nadzieję im po prostu brakuje czegoś proste:hapi-auth-cookie nie może załadować strategii sesji

var Hapi = require('hapi'); 
var Mongoose = require('mongoose'); 

Mongoose.connect('mongodb://localhost/rfmproducetogo'); 

var server = new Hapi.Server(8080, "localhost"); 

server.pack.register([{ 
    plugin: require("lout") 
}, { 
    plugin: require('hapi-auth-cookie') 
}, { 
    plugin: require("./plugins/togo") 
}, { 
    plugin: require("./plugins/auth") 
}], function(err) { 
    if (err) throw err; 
    server.auth.strategy('session', 'cookie', { 
     password: 'shhasecret', 
     cookie: 'wtfisthisfor', 
     isSecure: false, 
     redirectTo: false 
    }); 
    server.start(function() { 
     console.log("hapi server started @ " + server.info.uri); 
    }); 
}); 

I w moim plugin I Togo mają tę konfigurację trasy do korzystania z sesji

exports.create = function(plugin) { 
    plugin.route({ 
     method: 'POST', 
     path: '/togo/add', 
     handler: function(request, reply) { 
      produce = new Produce(); 
      produce.label = request.payload.label; 
      produce.price = request.payload.price; 
      produce.uom = request.payload.uom; 
      produce.category = request.payload.category; 

      produce.save(function(err) { 
       if (!err) { 
        reply(produce).created('/togo/' + produce._id); 
       } else { 
        reply(err); 
       } 

      }); 
     }, 
     config: { 
      auth: 'session' 
     } 
    }); 
}; 

błąd im widzenie jest taka:

/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421 
    throw new Error(msgs.join(' ') || 'Unknown error'); 
     ^
Error: Unknown authentication strategy: session in path: /togo/add 
    at Object.exports.assert (/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421:11) 
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:123:14 
    at Array.forEach (native) 
    at internals.Auth._setupRoute (/home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:121:24) 
    at new module.exports.internals.Route (/home/adam/Projects/bushhog/node_modules/hapi/lib/route.js:118:43) 
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:110:25 
    at Array.forEach (native) 
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:107:17 
    at Array.forEach (native) 
    at internals.Router.add (/home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:104:13) 

Uruchomienie węzła 0.10.28, hapijs 6.x, Hapi-auth-cookie 1.02

+0

Czy wciąż przeżywa obecnie problem? Znalazłem twoje repozytorium w GitHub i wygląda na to, że działa dobrze. – dylants

+0

hi @dylants Udało mi się ominąć problem, rejestrując strategię auth w samej wtyczce i działa, ale nie jestem pewien, czy to najlepszy sposób. Zawsze będę musiał upewnić się, że wtyczka auth jest załadowana jako pierwsza – battlemidget

Odpowiedz

1

Ten problem występuje, gdy próbuje się użyć strategii uwierzytelniania, zanim będzie faktycznie dostępna.

Masz już dobrą konfigurację aplikacji, dzieląc ją na pojedyncze, małe wtyczki o zadanym zasięgu.


UPDATE: oto dedykowany poradnik dla tego problemu, how to fix „unknown authentication strategy“


Dobrym sposobem, aby skonfigurować uwierzytelnianie i swoje wtyczki, które opierają się na uwierzytelnianie jest stworzenie dodatkowego „auth wtyczki” która dodaje pożądane strategie i może być używana jako zależność w innych wtyczkach.

hapi auth plugin example

exports.register = function (server, options, next) { 

    // declare/register dependencies 
    server.register(require('hapi-auth-cookie'), err => { 

    /** 
    * Register authentication strategies to hapi server 
    * 
    * We’re using hapi-auth-cookie plugin to store user information on 
    * client side to remember user data on every website visit 
    * 
    * For sure, we could and will add more authentication strategies. 
    * What’s next: JWT (we highly welcome pull requests to add JWT functionality!) 
    */ 
    server.auth.strategy('session', 'cookie', { 
     password: 'ThisIsASecretPasswordThisIsASecretPassword', 
     cookie: 'hapi-rethink-dash', 
     redirectTo: '/login', 
     isSecure: false 
    }); 

    server.log('info', 'Plugin registered: cookie authentication with strategy »session«') 

    next() 

    }) 

} 

exports.register.attributes = { 
    name: 'authentication', 
    version: '1.0.0' 
} 

w twojej /plugins/togo ustawić authentication plugin jako zależność (z server.dependency([array-of-deps])), co oznacza Hapi rejestruje wtyczki auth pierwszy i potem te zależności.

zarejestrować swoje wtyczki tak:

server.register([{ 
    plugin: require('./plugins/authentication') 
}, { 
    plugin: require("./plugins/togo") 
}], function(err) { 
    // handle callback 
}) 

Sprawdź hapi-rethinkdb-dash do szczegółowego przykładu.

Nadzieję, że pomaga!

0

pamiętać, jeśli używasz server.dependency wewnątrz wtyczki jak Marcus Poehls nie, należy również zarejestrować się, że zależność

server.register([{ 
    plugin: require('hapi-auth-cookie') 
},{ 
    plugin: require('./plugins/authentication') 
}, { 
    plugin: require("./plugins/togo") 
}], function(err) { 
    // handle callback 
}) 
Powiązane problemy