ソースを参照

Login with persistence

Nikatlas 6 年 前
コミット
96e3e31f2e

+ 1 - 2
package.json

@@ -28,9 +28,8 @@
     "eslint-plugin-react": "^7.7.0"
   },
   "proxy": {
-    // Matches any request starting with /api
     "/api": {
-      "target": "<url_1>",
+      "target": "http://localhost:3000/",
       "ws": true,
       "pathRewrite": {
         "^/api": "/"

+ 3 - 4
src/Game/services/Net.js

@@ -28,16 +28,15 @@ class Net {
 
 	post(url, body) {
 		let urb = this.baseURL + url;
-		return window.fetch({
-			url: urb,
+		return window.fetch(urb, {
 			method: 'POST',
 			headers: {
 				'Content-Type':'application/json',
-				'Token' : UserService.getToken || ''
+				'Token' : UserService.getToken() || ''
 			},
 			body: JSON.stringify(body)
 		});
 	}
 }
 
-export default new Net('http://localhost:3000/api');
+export default new Net('/api/');

+ 18 - 3
src/Game/services/UserService.js

@@ -4,7 +4,7 @@ import Net from './Net.js';
 class UserService {
 
 	constructor() {
-	
+		this.__enablePeristence = true;
 	}
 
 	login(username, password) {
@@ -12,7 +12,8 @@ class UserService {
 			username,
 			password
 		};
-		return Net.post('login', data)
+		return Net.post('users/login', data)
+				  .then((res) => res.json())
 				  .then((response) => {
 				  	return this._setUser(response);
 				  });
@@ -33,6 +34,9 @@ class UserService {
 
 	_unsetUser() {
 		// clear this singleton
+		this.username = null;
+		this.token = null;
+		this.__persistence();
 	}
 	
 	_setUser(data) {
@@ -48,11 +52,22 @@ class UserService {
 		this.username = username;
 		this.token = token;
 
+		this.__persistence();
 		return data;
 	}
 
+	__persistence() {
+		if(!this.__enablePeristence)return;
+		localStorage.setItem('user', JSON.stringify({
+			username: this.username,
+			token   : this.token
+		}));
+	}
+
 	_checkDataIntegrity(data) {
-		return true;
+		let tkn = data.token.length;
+		let username = data.username.length;
+		return tkn && username;
 	}
 }
 

+ 3 - 1
src/Game/views/base/CollectionHolder.js

@@ -39,7 +39,7 @@ class CollectionHolder extends CardHolder {
         this.unsetEvents();
         this.refresh();
 
-        EventManager.on('CardAddedToDeck', this.deckAdd);
+        //EventManager.on('CardAddedToDeck', this.deckAdd);
         EventManager.on('CardRemovedFromDeck', this.deckRemove);
     }
 
@@ -55,6 +55,7 @@ class CollectionHolder extends CardHolder {
 
     createCard() {
         let card = new Card({GameLayer: this.GameLayer, id:this.cardNumber});
+        this.addChild(card);
         card.zIndex = this.z;
         card.attach(this);
         this.cards.push(card);
@@ -89,6 +90,7 @@ class CollectionHolder extends CardHolder {
 
     createCardGrey(){
         let card = new Card({GameLayer: this.GameLayer, id: this.id});
+        this.addChild(card);
         card.zIndex = this.z - 5;
         card.attach(this);
         card.unsetEvents();

+ 6 - 4
src/Game/views/base/DeckCardHolder.js

@@ -15,14 +15,12 @@ class DeckCardHolder extends CardHolder {
     constructor(props) {
         super(props);
 
-        // Properties Component 
-
+        // Properties Component
         this.construct(props);
 
         this.appear();
         this.uncloak();
         this.show();
-        
     }
 
     lock(card = null) {
@@ -31,12 +29,16 @@ class DeckCardHolder extends CardHolder {
         this._card = card;
         if(card) {
             card.attach(this);
-            EventManager.trigger('CardAddedToDeck');
+            //EventManager.trigger('CardAddedToDeck');
             EventManager.trigger('increaseDeckCards', this._card.id);
             //if(this._lockable)card.unsetEvents();
         }
     }
 
+    _onDrop(card) {
+        card.parent.deckAdd();
+    }
+
     unlock() {
         EventManager.trigger('CardRemovedFromDeck');
         EventManager.trigger('decreaseDeckCards',this._card.id);

+ 4 - 8
src/Game/views/demo/Login.js

@@ -11,14 +11,12 @@ class Login extends PIXI.Container{
     constructor(props) {
         super();
         let {GameLayer, Gui} = props;
-        
-
 
         let email = new TextInput({GameLayer, width: 200});
         this.addChild(email);
         let password = new TextInput({GameLayer, width: 200});
         this.addChild(password);
-        
+
         let emailText = new Text({GameLayer, width: 200});
         this.addChild(emailText);
         let passwordText = new Text({GameLayer, width: 200});
@@ -26,7 +24,7 @@ class Login extends PIXI.Container{
 
         let loginBtn = new Button({GameLayer, width: 100});
         this.addChild(loginBtn);
-        
+
         // Set Properties
         emailText.setText("Username/Email");
         passwordText.setText("Password");
@@ -44,8 +42,6 @@ class Login extends PIXI.Container{
         this.email = email;
         this.password = password;
         loginBtn.onClick((e) => this.login(e));
-
-
     }
 
     login() {
@@ -53,9 +49,9 @@ class Login extends PIXI.Container{
     	let p = this.password.getValue();
 
     	// UserService Singleton to be called
-    	UserService.login()
+    	UserService.login(e,p)
     	.then((data) => {
-    		alert(data);
+            console.log(data.body());
     		return true;
     	})
     	.catch((err) => {