2018-03-24 16:51:04 +00:00
|
|
|
import LocalStorageInterface from './local-storage-interface'
|
|
|
|
|
2018-03-24 15:49:00 +00:00
|
|
|
export default class Authentication{
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Constructs an Authentication object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
constructor(){
|
|
|
|
|
2018-03-24 16:51:04 +00:00
|
|
|
/* (1) Default localStorage Interface */
|
2018-04-03 15:21:47 +00:00
|
|
|
this.lsi = new LocalStorageInterface('__auth__', 15*60);
|
2018-03-24 16:51:04 +00:00
|
|
|
|
|
|
|
/* (2) Update token */
|
2018-03-28 17:42:29 +00:00
|
|
|
if( this.token !== null && this.user !== null ){
|
2018-03-24 16:51:04 +00:00
|
|
|
this.lsi.push('token', this.token);
|
2018-03-28 17:42:29 +00:00
|
|
|
this.lsi.push('user', this.user);
|
|
|
|
}
|
2018-03-24 15:49:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-24 16:51:04 +00:00
|
|
|
|
2018-03-28 17:42:29 +00:00
|
|
|
/* (2) facade
|
2018-03-24 16:51:04 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-28 17:42:29 +00:00
|
|
|
// TOKEN
|
2018-03-28 20:54:49 +00:00
|
|
|
get token(){
|
|
|
|
let tok = this.lsi.fetch('token');
|
|
|
|
|
|
|
|
// redirect if on 'auth' page
|
|
|
|
if( tok === null && gs.get.authed )
|
|
|
|
document.location = '';
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2018-03-24 16:51:04 +00:00
|
|
|
set token(_token){ return this.lsi.push('token', _token); }
|
|
|
|
|
2018-03-28 17:42:29 +00:00
|
|
|
// USER DATA
|
|
|
|
get user(){ return this.lsi.fetch('user'); }
|
|
|
|
set user(_user){ return this.lsi.push('user', _user); }
|
|
|
|
|
2018-03-24 15:49:00 +00:00
|
|
|
}
|