0

I am a newbie to MEAN stack and developing an end-to-end application using MEAN. I have a small confusion on how to handle authentication tokens on client side (i.e. in Angularjs).

I created an authentication token after either the user signup's or login to the application. I followed this link for creating authentication token. I set the authentication token in response headers and saving it on the browsers localStorage on client side. I accessed it whenever i did a REST call.

Is it a good practice to access tokens directly from the localStorage or i need to access the token by creating either a factory or a service ?. I have seen many SO answers, in which they access the tokens by creating either a factory or a service, but my confusion is if i can able to access the token straight away from localStorage why i need to create a factory or service ? Is there any best practices for it ?

khex
  • 2,778
  • 6
  • 32
  • 56
MdKamil
  • 41
  • 3
  • 10
  • to much words - no code, no charts) – khex Feb 21 '15 at 01:57
  • My doubt was , is it a good practice to access the tokens directly using , `$window.localStorage.getItem('tokenName');` (or) should i use a service to do that like ` app.factory('getTokens' function(){ return { // code to access browsers local strage . } }):` ?? – MdKamil Feb 21 '15 at 02:12

1 Answers1

0

MEAN.IO provide methodology of Test-Driving-Development and has several Karma test cases from the box. For unit testing you should to write factory app.factory('getTokens) and then write test case for it like:

  • User Logged In -> Ok.
  • User Not Logged In -> Throw Error.

But If you, for some reason, does not support TDD - you shouldn't.

khex
  • 2,778
  • 6
  • 32
  • 56