1

I'm up against a challenge. I got a WordPress website where users sign up to the website using the built-in WordPress user login system. And as you may know, this places a WordPress authentication cookie!

There are no issues so far.

Now, I'm building an Angular app (on the same domain where this WordPress website site is installed ) AND the users of this Angular app will be the WordPress users.

which means....

When a logged in user visits the Angular app's home page, that home page will know the fact the user is a logged-in-user from the get-go and can even display that user's username on the screen.

How is this possible?

I created a PHP page ( get-username.php ) that reads the Wordpress's authentication cookie. It can read the username without a hassle. Since that cookie is expired and destroyed whenever the user logs out from the WordPress, I can always use that as the proof or the receipt of the fact that I'm dealing with a logged in or logged out user.

So, I thought, well, let's tap into that... but it did not dawn on me that this is impossible. Cause when I do this HTTP call, the get-username.php always and always returns NADA! As if the user is not logged in. Here is the Angular code that is expected and hoped to work:

ngOnInit() {
  console.log('service is about to run...');
  this.requestService.getRequest('http://example.com/angularAppFolder/get-username.php').subscribe(
    (response: Response) => {
      console.log('response:' + response);
    },
    (error: any) => {
      console.log('an error must have happened.'); // I cannot seem to log anything from the console.
    }
  );
}

The problem is ... the get-username.php PHP page cannot read the cookie when accessed thru this way. All bets are off as far as cookies and sessions are concerned. But if you were to load the get-username.php, it will always read the cookie to you - so long as you logged in!

Is there a way to beat this? Could you recommend an alternative way to get the login status of that user from within this same-domain Angular app?

Thank you

  • the Angular application is JavaScript code in a different directory that WordPress, isn't it ? it's not server side code ? – mmm Sep 30 '17 at 18:56
  • I believe this is more an Angular related question. Check stackoverflow.com/questions/17064791/… – marcochiesi Sep 30 '17 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.