Guest User

Untitled

a guest
Dec 1st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import Vue from 'vue'
  2. import BootstrapVue from "bootstrap-vue"
  3. import App from './App.vue'
  4. import "bootstrap/dist/css/bootstrap.min.css"
  5. import "bootstrap-vue/dist/bootstrap-vue.css"
  6. import axios from 'axios'
  7. import VueAxios from 'vue-axios'
  8. import * as jwt_decode from 'jwt-decode'
  9. import Vuex from 'vuex'
  10.  
  11. Vue.use(Vuex);
  12. Vue.use(VueAxios, axios);
  13. Vue.use(BootstrapVue);
  14.  
  15. //Am I suposed to initiate this here?
  16. let t = JWT(token);
  17.  
  18. const store = new Vuex.Store({
  19. state: {
  20. jwt: localStorage.getItem('t'),
  21. endpoints: {
  22. obtainJWT: 'http://0.0.0.0:10000/auth/obtain_token',
  23. refreshJWT: 'http://0.0.0.0:10000/auth/refresh_token'
  24. }
  25. },
  26. mutations: {
  27. updateToken(state, newToken) {
  28. localStorage.setItem('t', newToken);
  29. state.jwt = newToken;
  30. },
  31. removeToken(state) {
  32. localStorage.removeItem('t');
  33. state.jwt = null;
  34. }
  35. },
  36. actions: {
  37. obtainToken(username, password) {
  38. const payload = {
  39. username: username,
  40. password: password
  41. }
  42. axios.post(this.state.endpoints.obtainJWT, payload)
  43. .then((response) => {
  44. this.commit('updateToken', response.data.token);
  45. })
  46. .catch((error) => {
  47. console.log(error);
  48. })
  49. },
  50. refreshToken() {
  51. const payload = {
  52. token: this.state.jwt
  53. }
  54. axios.post(this.state.endpoints.refreshJWT, payload)
  55. .then((response) => {
  56. this.commit('updateToken', response.data.token)
  57. })
  58. .catch((error) => {
  59. console.log(error)
  60. })
  61. },
  62. inspectToken() {
  63. const token = this.state.jwt;
  64. if (token) {
  65. const decoded = jwt_decode(token);
  66. const exp = decoded.exp
  67. const orig_iat = decode.orig_iat
  68. if (exp - (Date.now() / 1000) < 1800 && (Date.now() / 1000) - orig_iat < 628200) {
  69. this.dispatch('refreshToken')
  70. } else if (exp - (Date.now() / 1000) < 1800) {
  71. // DO NOTHING, DO NOT REFRESH
  72. } else {
  73. // PROMPT USER TO RE-LOGIN, THIS ELSE CLAUSE COVERS THE CONDITION WHERE A TOKEN IS EXPIRED AS WELL
  74. }
  75. }
  76. }
  77. }
  78. })
  79.  
  80. new Vue({
  81. el: '#app',
  82. render: h => h(App)
  83. })
Add Comment
Please, Sign In to add comment