1

I'm trying to build a small facebook login with angular 2 but well I'm having problem, after logging in the facebook the user would be redirected to /home

app.get('/home', function (req, res) {
    res.sendfile(path.join(__dirname, '../', 'views', 'index.html'), { user: req.user});
    console.log({ user: req.user});
});

I'm trying to display the user information in angular2 page but I keep getting undefined. console logging the object works fine.

I used the same method with ejs files using res.render and using this inside the ejs

<%= user.facebook.name %>

and it works fine. I tried to use {{}} in the angular 2 but not working. any help please?

thanks

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
Vay
  • 23
  • 4

2 Answers2

0

I would define this element when bootstrapping your application. Because this hint is only available within your index.html page, I would use the following processing.

Update the file that bootstraps your application:

import {bootstrap} from '...';
import {provide} from '...';
import {AppComponent} from '...';

export function main(facebookUser) {
  bootstrap(AppComponent, [
    provide('user', { useValue: facebookUser })
  ]);
}

Provide this value from the index.html file this way:

<script>
  var facebookUser = '<%= user.facebook.name %>';
  System.import('app/main').then((module) => {
    module.main(facebookUser);
  });
</script>

This question could interest you:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
0

ok thanks for the replies i found the answer for my question. this code will call the api that and get the user details

constructor(private http: Http) { }

ngOnInit() {
    this.getUser();
}

getUser() {
    this.http.get('/api/user')
        .map((res:Response) => res.json())
        .do(data => console.log('data: ' + JSON.stringify(data)))
        .subscribe(
            data => { this.user = data.user.facebook.name},
            err => console.error(err),
            () => console.log('done')
        );

}
Vay
  • 23
  • 4