|
@@ -3,72 +3,90 @@ import Net from './Net.js';
|
|
|
|
|
|
class UserService {
|
|
class UserService {
|
|
|
|
|
|
- constructor() {
|
|
|
|
- this.__enablePeristence = true;
|
|
|
|
- }
|
|
|
|
|
|
+ constructor() {
|
|
|
|
+ this.__enablePeristence = true;
|
|
|
|
+ this.__load();
|
|
|
|
+ }
|
|
|
|
|
|
- login(username, password) {
|
|
|
|
- let data = {
|
|
|
|
- username,
|
|
|
|
- password
|
|
|
|
- };
|
|
|
|
- return Net.post('users/login', data)
|
|
|
|
- .then((res) => res.json())
|
|
|
|
- .then((response) => {
|
|
|
|
- return this._setUser(response);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ login(username, password) {
|
|
|
|
+ let data = {
|
|
|
|
+ username,
|
|
|
|
+ password
|
|
|
|
+ };
|
|
|
|
+ return Net.post('users/login', data)
|
|
|
|
+ .then((res) => res.json())
|
|
|
|
+ .then((response) => {
|
|
|
|
+ return this._setUser(response);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- logout() {
|
|
|
|
- // To request Token removal
|
|
|
|
- this._unsetUser();
|
|
|
|
- }
|
|
|
|
|
|
+ logout() {
|
|
|
|
+ // To request Token removal
|
|
|
|
+ this._unsetUser();
|
|
|
|
+ }
|
|
|
|
|
|
- getUsername() {
|
|
|
|
- return this.username;
|
|
|
|
- }
|
|
|
|
|
|
+ getUsername() {
|
|
|
|
+ return this.username;
|
|
|
|
+ }
|
|
|
|
|
|
- getToken() {
|
|
|
|
- return this.token;
|
|
|
|
- }
|
|
|
|
|
|
+ getToken() {
|
|
|
|
+ return this.token;
|
|
|
|
+ }
|
|
|
|
|
|
- _unsetUser() {
|
|
|
|
- // clear this singleton
|
|
|
|
- this.username = null;
|
|
|
|
- this.token = null;
|
|
|
|
- this.__persistence();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- _setUser(data) {
|
|
|
|
- if(!this._checkDataIntegrity(data)) {
|
|
|
|
- throw data;
|
|
|
|
- }
|
|
|
|
|
|
+ _unsetUser() {
|
|
|
|
+ // clear this singleton
|
|
|
|
+ this.username = null;
|
|
|
|
+ this.token = null;
|
|
|
|
+ this.__persistence();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _setUser(data) {
|
|
|
|
+ if(!this._checkDataIntegrity(data)) {
|
|
|
|
+ throw data;
|
|
|
|
+ }
|
|
|
|
|
|
- let {
|
|
|
|
- username,
|
|
|
|
- token
|
|
|
|
- } = data;
|
|
|
|
|
|
+ let {
|
|
|
|
+ username,
|
|
|
|
+ token
|
|
|
|
+ } = data;
|
|
|
|
|
|
- this.username = username;
|
|
|
|
- this.token = token;
|
|
|
|
|
|
+ this.username = username;
|
|
|
|
+ this.token = token;
|
|
|
|
|
|
- this.__persistence();
|
|
|
|
- return data;
|
|
|
|
- }
|
|
|
|
|
|
+ this.__persistence();
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
|
|
- __persistence() {
|
|
|
|
- if(!this.__enablePeristence)return;
|
|
|
|
- localStorage.setItem('user', JSON.stringify({
|
|
|
|
- username: this.username,
|
|
|
|
- token : this.token
|
|
|
|
- }));
|
|
|
|
- }
|
|
|
|
|
|
+ __persistence() {
|
|
|
|
+ if(!this.__enablePeristence)return;
|
|
|
|
+ localStorage.setItem('user', JSON.stringify({
|
|
|
|
+ username: this.username,
|
|
|
|
+ token : this.token
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ __load() {
|
|
|
|
+ if(!this.__enablePeristence)return;
|
|
|
|
+ let user = localStorage.getItem('user');
|
|
|
|
+ try {
|
|
|
|
+ let json = JSON.parse(user);
|
|
|
|
+ this.username = json.username;
|
|
|
|
+ this.token = json.token;
|
|
|
|
+ } catch (e) {
|
|
|
|
+ this._unsetUser();
|
|
|
|
+ console.log(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- _checkDataIntegrity(data) {
|
|
|
|
- let tkn = data.token.length;
|
|
|
|
- let username = data.username.length;
|
|
|
|
- return tkn && username;
|
|
|
|
- }
|
|
|
|
|
|
+ isLogged() {
|
|
|
|
+ return this.username && this.username.length && this.token && this.token.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _checkDataIntegrity(data) {
|
|
|
|
+ let tkn = data.token && data.token.length;
|
|
|
|
+ let username = data.username && data.username.length;
|
|
|
|
+ return tkn && username;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
export default new UserService();
|
|
export default new UserService();
|