Templates check if authenticated

I’m trying to check if a user is authenticated from within templates and then render appropriate content, can you point me to any examples of accomplishing this?


The ServiceStack Filters contains a number of AuthFilters to test for Authentication the Chat Web App uses endIfAuthenticated to only show login links if they’re on Authenticated with:

{{ ['twitter', 'facebook', 'github'] | endIfAuthenticated 
   | select: <a href="/auth/{ it }" class="{ it }"></a> }}

The Info Filters also contains filters for accessing the Users Session, checking if they have roles/permissions, etc.

Thanks, I’m playing around with the syntax for a few of the Auth filters to get a better handle on the syntax but I cant seem to get my head around using isAuthenticated and ifAuthenticated.

I’ve tried
{{ isAuthenticated | ['link1', 'link2', 'link3'] | select: <a href="/xxx/{ it }" class="{ it }">{ it } </a> }}

but its printing out the line on the html page. Where am I going wrong with the syntax?

[isAuthenticated][1] returns a boolean, a value needs to be passed into an filter, it can’t be passed directly into another value like an array.

The inverse filter of endIfAuthenticated is onlyIfAuthenticated so you can do:

{{ ['link1', 'link2', 'link3'] | onlyIfAuthenticated 
   | select: <a href="/xxx/{ it }" class="{ it }">{ it } </a> }}

To display links for authenticated users.

You can also use ifAuthenticated when you want to start executing a filter:

{{ ifAuthenticated | show: <a href="/foo">bar<a> }

[`show` is an alias for `use`][2] which reads better when it's at the end of an expression to display something when the previous filter returns a value. `use` reads better when it's in the middle of an expression to start using a value when the previous filter returns a value, e.g:

{{ ifAuthenticated | use(['link1','link2','link3']) | select: <a href="{it}">{it}<a> }

Another way to write the above that uses the `isAuthenticated` boolean [binding][3] would be:

{{ isAuthenticated   | ifUse(['link1','link2']) | select: <a href="{it}">{it}<a> }}
{{ ['link1','link2'] | useIf(isAuthenticated)   | select: <a href="{it}">{it}<a> }}

  [1]: http://templates.servicestack.net/docs/filters-reference?nameContains=Auth&tab=ss-filters
  [2]: http://templates.servicestack.net/docs/default-filters#control-execution
  [3]: http://templates.servicestack.net/docs/default-filters#as-bindings