I'm setting up a Facebook registration for my Angular2 web app.
Once the application accepted via Facebook (after being redirected to the Facebook authorization page), it redirect to my webapp with the token and code as url params:
http://localhost:55976/?#access_token=MY_ACCESS_TOKEN&code=MY_CODE
But once the page is loaded, the params are removed. The url become:
http://localhost:55976/
How can I extract the parameters (access_token and code) before they are removed?
My routing configuration contains:
{ path: 'login', component: LoginComponent },
{ path: 'login/:access_token/:code', component: LoginComponent },
EDIT:
Here is how I redirect to facebook in my login.component:
html:
Sign in with Facebook
Typescript:
login_facebook() {
let url = 'https://www.facebook.com/dialog/oauth?'+
'client_id=my_client_id' +
'&redirect_uri=' + encodeURIComponent('http://localhost:55976/#/login/') +
'&response_type=code%20token';
console.log(url);
window.location.href = url;
}
The facebook api redirect to http://localhost:55976/#/login/, this is where I try to get the access_token and code parameters.
EDIT 2:
If I remove the sharp in the redirect url, facebook redirect me to the good URL, but without the sharp, angular cannot resolve the url.
Before removing '#':
redirect_uri: http://localhost:55976/#/login/
facebook return: http://localhost:55976/?#access_token=MY_ACCESS_TOKEN&code=MY_CODE
After removing '#':
redirect_uri: http://localhost:55976/login/
facebook return: http://localhost:55976/login/?#access_token=MY_ACCESS_TOKEN&code=MY_CODE
That mean that the problem comes from the sharp. But without the sharp, angular returns HTTP Error 404.0 - Not Found.
解决方案
Here is my final working code:
Imports:
import { Router, NavigationCancel } from '@angular/router';
import { URLSearchParams, } from '@angular/http';
Constructor:
constructor(public router: Router) {
router.events.subscribe(s => {
if (s instanceof NavigationCancel) {
let params = new URLSearchParams(s.url.split('#')[1]);
let access_token = params.get('access_token');
let code = params.get('code');
}
});
}