Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. export default function(Vue) {
  2. Vue.auth = {
  3. setToken (token, expiration) {
  4. localStorage.setItem('token', token)
  5. localStorage.setItem('expiration', expiration)
  6. },
  7.  
  8. getToken () {
  9. var token = localStorage.getItem('token');
  10. var expiration = localStorage.getItem('expiration');
  11.  
  12. if(! token || ! expiration){
  13. return null;
  14. }
  15.  
  16. if(Date.now() > parseInt(expiration)){
  17. this.destroyToken()
  18. return null
  19. }else{
  20. return token;
  21. }
  22. },
  23.  
  24. destroyToken () {
  25. localStorage.removeItem('expiration')
  26. localStorage.removeItem('token')
  27. },
  28.  
  29. isAuthenticated () {
  30. if(this.getToken()){
  31. return true
  32. }else{
  33. return false
  34. }
  35. }
  36.  
  37.  
  38. }
  39.  
  40.  
  41. Object.defineProperties(Vue.prototype, {
  42. $auth:{
  43. get () {
  44. return Vue.auth;
  45. }
  46. }
  47. })
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement