(For full example code, see my angular-fullstack-tokens repo.)
I have an angular-fullstack based app (angular, node, express, passport),
and I’m using token-based authentication following this great blogpost,
rather than cookie-based, using express-jwt and jsonwebtoken.
I started with daftmonk’s angular-fullstack,
replaced its authentication system with tokens (easy enough), then set
out to include social-based authentication for facebook, using passport-
facebook. I kept running into CSRF-prevention issues, with the error
No ‘Access-Control-Allow-Origin’ header is present on the requested
resource.
Without going too far off topic, this was due to the fact that
I was attempting to make an $http
call from the client to a node route
that I had set up for facebook authentication, rather than simply using
an anchor tag href call on the client. Ajax calls are particular tricky
across domain origins. I had to set it up this way because I wanted my
server to return user info of my making back to the client.
I tried solutions outlined here
and here,
each to no avail. Granted, both were under the “cookie-based auth” umbrella,
but that shouldn’t matter, right?
After searching for nearly two days, I came up with the idea of making
auth request client-side, rather than server-side. Using the ngFacebook
angular module along with grunt-ng-constant gave me the answer I needed.
Here’s how to do it.
Read on