|
import Vue from 'vue'; |
|
import VueResource from 'vue-resource'; |
|
|
|
Vue.use(VueResource); |
|
|
|
class Cookie |
|
{ |
|
/** |
|
* Get cookie value by the cookie name. |
|
* |
|
* @param string name |
|
* @return {string|mixed} |
|
*/ |
|
get(name) { |
|
if( (this._getCookieValue('token_refresh_needed') == '') && (name == 'api_token') ) { |
|
Vue.http.get('/api/refresh/token?token=' + this._getCookieValue('api_token')) |
|
.then( (response) => { |
|
let token = JSON.parse(response.body).token; |
|
|
|
this._setTokenRefreshTimer(); |
|
|
|
this.set('api_token', token, this._getCookieValue('token_expires_time'), this._getCookieValue('remember_me')); |
|
|
|
return this._getCookieValue(name); |
|
}) |
|
} else { |
|
return this._getCookieValue(name); |
|
} |
|
} |
|
|
|
/** |
|
* Set cookie value. |
|
* |
|
* @param string name |
|
* @param mixed value |
|
* @param int expires |
|
* @param bool rememberMe |
|
*/ |
|
set( name, value, expires = null, rememberMe = false ) { |
|
|
|
let now = new Date(); |
|
|
|
this._setTokenRefreshTimer(); |
|
|
|
// if remember me is true |
|
// then set expires time with large number |
|
if(rememberMe) { |
|
now.setTime(now.getTime() + 1 * 9999 * 9999 * 1000); |
|
|
|
document.cookie = name + "=" + value + "; expires=" + now.toUTCString() + "; path=/"; |
|
document.cookie = "remember_me=true; expires=" + now.toUTCString() + "; path=/"; |
|
} |
|
|
|
// if expires time is integer |
|
// then expires data will be set |
|
else if( _.isInteger(expires) ) { |
|
now.setTime(now.getTime() + 1 * expires * 1000); |
|
|
|
document.cookie = name + "=" + value + "; expires=" + now.toUTCString() + "; path=/"; |
|
document.cookie = "token_expires_time=" + expires + "; expires=" + now.toUTCString() + "; path=/"; |
|
} |
|
|
|
// by default no expires time will be set |
|
else { |
|
document.cookie = name + "=" + value + "; path=/"; |
|
} |
|
} |
|
|
|
/** |
|
* Get cookie value. |
|
* |
|
* @param string name |
|
* @return {mixed} |
|
* @private |
|
*/ |
|
_getCookieValue( name ) { |
|
let cookieName = name; |
|
|
|
name = cookieName + "="; |
|
|
|
let documentCookie = document.cookie.split(';'); |
|
|
|
for(var i = 0; i < documentCookie.length; i++) { |
|
let cookie = documentCookie[i]; |
|
|
|
while (cookie.charAt(0)==' ') { |
|
cookie = cookie.substring(1); |
|
} |
|
|
|
if (cookie.indexOf(name) == 0) { |
|
return cookie.substring(name.length, cookie.length) |
|
} |
|
} |
|
|
|
return ''; |
|
} |
|
|
|
/** |
|
* Set token_refresh_needed cookie to determine |
|
* the api_token value need to refresh or not |
|
* |
|
* @private |
|
*/ |
|
_setTokenRefreshTimer() { |
|
let now = new Date(); |
|
|
|
now.setTime(now.getTime() + 1 * 3000 * 1000); |
|
|
|
document.cookie = "token_refresh_needed=false; expires=" + now.toUTCString() + "; path=/"; |
|
} |
|
|
|
/** |
|
* Delete cookie. |
|
* |
|
* @param string name |
|
*/ |
|
delete( name ) { |
|
let now = new Date(); |
|
|
|
now.setTime(now.getTime() - 1); |
|
|
|
document.cookie = name + "=" + "; expires=" + now.toUTCString() + "; path=/"; |
|
} |
|
} |
|
|
|
|
|
module.exports = new Cookie; |