sir_thursday
sir_thursday

Reputation: 5419

Passing backend express session variable to Angular

Current code:

<div ng-controller="HeaderCtrl" class="header">
  <div class="logo">intquest</div>

  <div ng-if="signed_in" class="header-actions">
    <a href="/#/random">Random</a>
    <a href="/#/add">Add question</a>
  </div>

  <div ng-if="!signed_in" class="header-actions">
    <a href="/#/register">Register</a>
    <a href="/#/login">Login</a>
  </div>
</div>

I want to somehow pass my Express session var to the $scope.signed_in variable. Because this is an HTML and not a Jade template, I couldn't use JSON.stringify, I don't have a backend API so I don't think I can make a $http or resource request, how should I go about doing this? What are the best practices?

Upvotes: 2

Views: 897

Answers (1)

Jesus is Lord
Jesus is Lord

Reputation: 15399

I can think of two things, assuming you can't create a backend API:

  1. See if a cookie exists. Try injecting the $cookes service into your HeaderCtrl and examining it to see if your Expression session left any cookies. If you did and you know its name (looks like it's 'connect.sid' from this -- unless you overwrote the default value) then just set $scope.signed_in to an expression that returns whether or not the cookie is present with that name. This assumes that the cookie is not present when logged out. I don't know if this is assumption true or not.

  2. If that doesn't work, try to read a value from the cookie if you absolutely cannot make an API call I think you may need to make sure the cookie doesn't have a secret, etc. - then I would assume it would be readable from the client/within Angular.

Let me know if both of those don't work or if you can't/don't want to do them (and why) and I'll see if I can think of an alternative (based on your why/constraints).

Upvotes: 1

Related Questions