[javascript] asdasda
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import { createStore } from 'vuex';
- import { jwtDecode } from "jwt-decode";
- import Cookies from 'js-cookie';
- import moment from 'moment';
- import axios from 'axios';
- export default createStore({
- state: {
- apiLink: "production",
- baseUrl: "http://localhost:3000",
- user: null,
- notification: {
- show: false,
- head: "",
- subheader: "",
- success: true,
- }
- },
- mutations: {
- logout(state)
- {
- const userCookie = Cookies.get('user');
- if(userCookie !== undefined)
- {
- Cookies.remove('user');
- }
- state.user = null;
- },
- async restoreSession(state)
- {
- // Restore session from cookie
- const userCookie = Cookies.get('user');
- if(userCookie !== undefined)
- {
- console.log(userCookie);
- const user = JSON.parse(userCookie);
- if(user.data.iat + 86400 >= moment().unix())
- {
- state.user = user;
- try{
- const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
- headers: {
- 'auth-token': state.user.token
- }
- });
- state.user.balance = balance.data.payload.balanceEUR;
- console.log("Session restored", state.user);
- }
- catch(error)
- {
- console.log(error);
- state.user.balance = 0;
- }
- }
- else
- {
- console.log("Old cookies removed!")
- Cookies.remove('user')
- }
- }
- },
- async getCurrentBalance(state)
- {
- if(state.user === null) return;
- try{
- const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
- headers: {
- 'auth-token': state.user.token
- }
- });
- state.user.balance = balance.data.payload.balanceEUR;
- }
- catch(error)
- {
- console.log(error);
- state.user.balance = 0;
- }
- },
- setNotification(state, notification)
- {
- state.notification = notification;
- setTimeout(()=>
- {
- state.notification = {
- show: false,
- head: "",
- subheader: "",
- success: true,
- }
- },3000)
- },
- async setUser(state, jwt)
- {
- // Delete old cookie
- const userCookie = Cookies.get('user');
- if(userCookie !== undefined)
- {
- Cookies.remove('user')
- }
- // Create session
- let decodedJwt = jwtDecode(jwt);
- let user = {
- balance: 0,
- token: jwt,
- validUntil: decodedJwt.iat + 86400,
- data: decodedJwt,
- };
- state.user = user;
- try{
- const balance = await axios.post(`${state.baseUrl}/api/user/balance/get`, {}, {
- headers: {
- 'auth-token': state.user.token
- }
- });
- state.user.balance = balance.data.payload.balanceEUR;
- console.log("Session created", state.user);
- Cookies.set('user', JSON.stringify(user), {expires: 1});
- }
- catch(error)
- {
- console.log(error);
- state.user.balance = 0;
- }
- // Set as cookie
- }
- },
- actions: {
- },
- modules: {
- }
- })
Editor
You can edit this paste and save as new: