Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { apiConfig } from '../config'
  2. import { Base64 } from 'js-base64'
  3.  
  4. export default {
  5.  
  6.   user: {
  7.     authenticated: false,
  8.     username: ''
  9.   },
  10.  
  11.   login (context, creds, redirect) {
  12.     let header = {
  13.       headers: {
  14.         Authorization: 'Basic ' + Base64.encode(creds.username + ':' + creds.password),
  15.         'System-Id': apiConfig.systemId,
  16.         'Client-Id': apiConfig.clientId
  17.       }
  18.     }
  19.     context.$http.get(apiConfig.apiUrl, header)
  20.       .then(response => {
  21.         localStorage.setItem('sessionId', response.headers['uri-append'])
  22.         this.user.authenticated = true
  23.         this.user.username = creds.username
  24.         context.$router.push({name: redirect})
  25.       })
  26.       .catch(e => {
  27.         console.log(e)
  28.         context.isError = true
  29.         context.credentials.username = ''
  30.         context.credentials.password = ''
  31.       })
  32.     this.authenticated = this.user.authenticated
  33.   },
  34.  
  35.   logout (context) {
  36.     localStorage.removeItem('sessionId')
  37.     this.user.authenticated = false
  38.     this.authenticated = this.user.authenticated
  39.     this.user.username = ''
  40.     context.$router.push({ name: 'home' })
  41.   },
  42.  
  43.   checkAuth (context) {
  44.     const sessionId = localStorage.getItem('sessionId')
  45.     if (sessionId) {
  46.       this.user.authenticated = true
  47.       this.authenticated = this.user.authenticated
  48.       return sessionId
  49.     } else {
  50.       this.user.authenticated = false
  51.       this.authenticated = this.user.authenticated
  52.       this.user.username = ''
  53.       if (context.$router) {
  54.         context.$router.push({name: 'home'})
  55.       }
  56.     }
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement