[javascript] asdasda

Viewer

  1. import { createStore } from 'vuex';
  2. import { jwtDecode } from "jwt-decode";
  3. import Cookies from 'js-cookie';
  4. import moment from 'moment';
  5. import axios from 'axios';
  6.  
  7. export default createStore({
  8.   state: {
  9.     apiLink: "production",
  10.     baseUrl: "http://localhost:3000",
  11.     user: null,
  12.     notification: {
  13.       show: false,
  14.       head: "",
  15.       subheader: "",
  16.       success: true,
  17.     }
  18.   },
  19.   mutations: {
  20.     logout(state)
  21.     {
  22.       const userCookie = Cookies.get('user');
  23.       if(userCookie !== undefined)
  24.       {
  25.         Cookies.remove('user');
  26.       }
  27.       state.user = null;
  28.     },
  29.     async restoreSession(state)
  30.     {
  31.       // Restore session from cookie
  32.       const userCookie = Cookies.get('user');
  33.       if(userCookie !== undefined)
  34.       {
  35.         console.log(userCookie);
  36.         const user = JSON.parse(userCookie);
  37.         if(user.data.iat + 86400 >= moment().unix())
  38.         {
  39.           state.user = user;
  40.           try{
  41.             const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
  42.               headers: {
  43.                 'auth-token': state.user.token
  44.               }
  45.             });
  46.             state.user.balance = balance.data.payload.balanceEUR;
  47.             console.log("Session restored", state.user);
  48.           }
  49.           catch(error)
  50.           {
  51.             console.log(error);
  52.             state.user.balance = 0;
  53.           }
  54.           
  55.         }
  56.         else
  57.         {
  58.           console.log("Old cookies removed!")
  59.           Cookies.remove('user')
  60.         }
  61.       }
  62.     },
  63.     async getCurrentBalance(state)
  64.     {
  65.       if(state.user === null) return;
  66.       try{
  67.         const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
  68.           headers: {
  69.             'auth-token': state.user.token
  70.           }
  71.         });
  72.         state.user.balance = balance.data.payload.balanceEUR;
  73.       }
  74.       catch(error)
  75.       {
  76.         console.log(error);
  77.         state.user.balance = 0;
  78.       }
  79.     },
  80.     setNotification(state, notification)
  81.     {
  82.       state.notification = notification;
  83.       setTimeout(()=>
  84.       {
  85.         state.notification = {
  86.           show: false,
  87.           head: "",
  88.           subheader: "",
  89.           success: true,
  90.         }
  91.       },3000)
  92.     },
  93.     async setUser(state, jwt)
  94.     {
  95.       // Delete old cookie
  96.       const userCookie = Cookies.get('user');
  97.       if(userCookie !== undefined)
  98.       {
  99.         Cookies.remove('user')
  100.       }
  101.       // Create session
  102.       let decodedJwt = jwtDecode(jwt);
  103.       let user = {
  104.         balance: 0,
  105.         token: jwt,
  106.         validUntil: decodedJwt.iat + 86400,
  107.         data: decodedJwt,
  108.       };
  109.       state.user = user;
  110.       try{
  111.         const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
  112.           headers: {
  113.             'auth-token': state.user.token
  114.           }
  115.         });
  116.         state.user.balance = balance.data.payload.balanceEUR;
  117.         console.log("Session created", state.user);
  118.         Cookies.set('user', JSON.stringify(user), {expires: 1});
  119.       }
  120.       catch(error)
  121.       {
  122.         console.log(error);
  123.         state.user.balance = 0;
  124.       }
  125.       // Set as cookie
  126.       
  127.     }
  128.   },
  129.   actions: {
  130.   },
  131.   modules: {
  132.   }
  133. })
  134.  

Editor

You can edit this paste and save as new:


File Description
  • asdasda
  • Paste Code
  • 23 Apr-2024
  • 3.27 Kb
You can Share it: