My backend is setup on AWS and is running an expess webserver. It is configured with passportjs.
On the frontend I want all users to login through linkedin. When they press the button it issues a http GET command using angular's $http.get service. The basic workflow I am trying to achieve is the following:
1) Client opens the app and hits sign in with linkedin and the get request is issued to the backend express server
2) The express server uses passportjs for linkedin oauth2 authentication which subsequently forwards the request to the linkedin server
3) Linkedin's server then responds to the client with a login page in response to the clients request
4) Once the page in step 3 is successfully verified with the users login credentials, the access token is sent to the express server since I configured the linkedin redirect URL to it.
5) The express server then receives the user profile details and then sends across some data to the client.
Since I am using the ionic Framework I would like to receive the data and then load up a page defined in the ionic templates folder(local to the frontend) and populate it with data received from the server at step 5.
In order to do this I am trying to issue an $http.get request which fails with the error: 404 on Step 2.
$http.get('http://www.example.com:3000/auth/linkedin/').
success(function(data, status, headers, config) {
console.log('success');
}).
error(function(data, status, headers, config) {
console.log('failure '+data+', ' +status +', '+headers+', '+config)
});
This prints out:
"failure , 404, function (name) {
"use strict";
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
}, [object Object]"
I am not sure if this is the way to go about it.
The other method that gave more success was using window.location('http://www.example.com:3000/auth/linkedin/'
). This was able to follow up the redirect to the linkedin login page and upon successful login, the profile was returned to the express server. This method worked fine upto step 4. The problem was at step 5, I am not able redirect the user back to the other templates defined in the app.
Instead of firing GET requests all over the place, You should write an angular service to manage authentication.
Such a service should expose functions like login()
, is_autheticated()
, get_username()
and etc'.
Then, inject the LoginService into your controllers and you won't have a hard time with requests and promises.
As for your redirect problem, take a look at ui-router.