Multiple login paths for everyauth - node.js

I have a node.js + express project that's using everyauth. I'm in the process of reworking the routes and I would like to have multiple routes for the login and logout paths so two api versions can coexist at the same time.
Calling postLoginPath multiple times just makes the last call "win".
I need the login code to respond to both "/login" and "/v1/login". I was trying to register the alternate route and do some sort of server-side forward but can't find the '/login' path (which is supplied to postLoginPath) in the registered routes in 'app.routes'.

Ended up using this: https://npmjs.org/package/connect-modrewrite
Here's some sample code for my example:
app.use(modRewrite([
'/v1/login /login',
'/v1/logout /logout'
]))

You can use everyauth also.
You can have array of value for the entryPath variable.
everyauth.facebook
.entryPath(['/url/path1','/url/path2'])
.callbackPath('/auth/facebook/callback')
.scope('email')
.fields('id,name,email,picture')
Also you can get the entryPath value by
everyauth.facebook.entryPath();
Hope this will be helpful.

Related

Express restful routes pathing issue

I'm having an issue with my Rest service. The two routes I'm trying to have are:
router.get('/stocks/:ticker', function(req, res){
and
router.get('/stocks/watchlist/', function(req, res){
when I call
http://localhost:8000/stocks/watchlist
my API believes 'watchlist' is the ticker symbol and goes to the incorrect route. I'm sure I'm missing something fundamental here.
Move the watchlist route in front of the other one.
That way it will get matched before the more open ended one gets to see the request at all. Routes are compared in the order they were defined and the first one to match gets it first. If it doesn't call next() to continue routing, then it will be the only one to see the route.

Looking for passportjs-local simple example using MEAN only

I am looking for a very simple example for using Passportjs (Local api) in my MEAN application. I took a reference from one example on github. There they have used jade to render the page after authentication. But I just want to use my home.html to show home page after authentication.
I have searched many example but in all they are using either jade or ejs. I don't want to use any engine to render the home page.
If anyone can provide a very simple example just using MEAN that would be a great help.
I don't want to use jade or ejs. Just simple html to render page.
Just setup a route similar to this:
app.route("/login")
.post(passport.authenticate("local"), function (req, res) {
res.json(req.user);
});
Then call that as an post call from the function you bind the form to in angular, you don't need to use jade/pug/ejs or any of it. You can simply use express as an API server. Just have angular redirect to the route you want after the successful return of the authentication (you should get a user object back)

How to create href links in a view, pointing to Express routes?

I want to create href links inside my views, but I do not want to point to a static address. I want to dynamically create href urls based on routes.
Example:
// app.js:
app.get('/test', function testPage(req, res, next) {
// do something here
});
// inside view.html:
<a href="path('testPage')" /> // something similar
<a href="/test" /> I do NOT want this!
Am I missing something, or there is no way to do that easily?
I've searched and I found that app._router.stack has all routes. But they are just a plain ARRAY so they don't have any kind of id or smth! What I came up with, is a function that parses all routes (from stack), gets their name, which is set only if the function has a name (http://expressjs.com/4x/api.html#req.route) and then create an object with key (route name) -> path.
Then I can use this function (somehow) inside views to create paths, right?
Any better ideas?
Coming from PHP frameworks background, I am used to two way routes. However, from what I've seen in tutorials, people just use the static links. I think it has to do with the nature of Node.js web applications. Lots of them are SPAs, so you just build an API and then refer to it statically from the frontend application.
When researching this topic, I've came accross this little library called 2-way router which can be used with Express. I haven't experimented with it yet, but it might be useful to you.
EDIT: Tried to re-word my search and I've found similar thread about url generation and express route naming.
EDIT: You can also implement django-like routing.

express-jwt handling specific secret passphrase by routes

Here is my use case.
In my express app using express-jwt module, I have 2 mains routes. I would like to secure my routes with 2 distincts passphrase.
app.use('/api/v1/admin', jwt({secret: "blabla1"}).unless({path:['/api/v1/admin/login']}));
app.use('/api/v1', jwt({secret: "blabla2"}).unless({path: ['/api/v1/login']}));
In this case, it doesn't work as I was expecting to...
Is there a way to achieve this in only one express app ?
Thanks in advance for your helps guys!
Your syntax is a little off, what you are doing above is setting the secret for the whole app. If you wanted to protect a certain route you could do something like below.
app.all('/api/v1', jwt({secret: "blabla2"}).unless({path: ['/api/v1/login']}));
app.all('/api/v1/admin', jwt({secret: "blabla1"}).unless({path:['/api/v1/admin/login']}));
The above code allows you define different secrets for a particular route. The call to app.all catches every type of HTTP call.

Redirecting client with NodeJS and Restify

I'm building a REST backend for an SPA with NodeJS, Restify and PassportJS for authentication. Everything's working except the last step, which is redirecting the client from the backends /login/facebook/callback to the home page of the application.
I've searched online and found lots of answers for ExpressJS but nothing useful for Node-Restify yet. I've managed to pick up a few snippets of code and this is what I'm attempting at the moment:
app.get('/api/v1/login/facebook/cb', passport.authenticate('facebook', { scope: 'email' }), function(req, res) {
req.session.user = req.user._id;
res.header('Location', '/#/home');
res.send();
});
The response is sent but the location header is not included and the client is presented with a white screen. How do I do a proper redirect using the Node-Restify API?
Restify's Response interface now has a redirect method.
As of this writing, there's a test showing how to use it here.
The contents of that test are:
server.get('/1', function (req, res, next) {
res.redirect('https://www.foo.com', next);
});
Many folks who use Restify are more familiar with ExpressJS. It's important to understand that (again, as of this writing) one of the three main public API differences affecting porting of Express plugins is that the res.redirect method in Restify requires you to pass next (or an InternalError is thrown). I've personally ported several modules from Express to Restify and the main API differences at first are (in Restify):
server.use is only for path & HTTP-method-agnostic middleware
res.redirect requires that you pass next
Some members or the Request interface are methods rather than values, such as req.path. req.path is an alias of req.getPath in Restify
I am NOT saying that under-the-hood they are similar, but that the above three things are the main obstacles to porting over Express plugins. Under-the-hood, Restify has many advantages over Express in my experience using it in both large enterprise applications and personal projects.
You need to use redirection status code 302.
res.send(302); or res.send(302, 'your response');

Resources