Skip to content

Commit

Permalink
use Storage on Auth. add storage.removeItem()
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed May 25, 2019
1 parent db8ee66 commit c0fe69b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.10.6",
"version": "0.10.7",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"colyseus",
Expand Down
7 changes: 4 additions & 3 deletions src/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { get, post, put, del } from "httpie";
import { getItem, setItem, removeItem } from "./Storage";

const TOKEN_STORAGE = "colyseus-auth-token";

Expand Down Expand Up @@ -81,7 +82,7 @@ export class Auth implements IUser {

constructor(endpoint: string) {
this.endpoint = endpoint.replace("ws", "http");
this.token = localStorage.getItem(TOKEN_STORAGE);
getItem(TOKEN_STORAGE, (token) => this.token = token);
}

get hasToken() {
Expand Down Expand Up @@ -112,7 +113,7 @@ export class Auth implements IUser {

// set & cache token
this.token = data.token;
localStorage.setItem(TOKEN_STORAGE, this.token);
setItem(TOKEN_STORAGE, this.token);

for (let attr in data) {
if (this.hasOwnProperty(attr)) { this[attr] = data[attr]; }
Expand Down Expand Up @@ -189,7 +190,7 @@ export class Auth implements IUser {

logout() {
this.token = undefined;
localStorage.removeItem(TOKEN_STORAGE);
removeItem(TOKEN_STORAGE);
this.unregisterPingService();
}

Expand Down
7 changes: 6 additions & 1 deletion src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

let storage: any;

function getStorage() {
function getStorage(): Storage {
if (!storage) {
storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
Expand All @@ -16,6 +16,7 @@ function getStorage() {
cache: {},
setItem: function(key, value) { this.cache[key] = value; },
getItem: function(key) { this.cache[key]; },
removeItem: function(key) { delete this.cache[key]; },
};

}
Expand All @@ -26,6 +27,10 @@ export function setItem(key: string, value: string) {
getStorage().setItem(key, value);
}

export function removeItem(key: string) {
getStorage().removeItem(key);
}

export function getItem(key: string, callback: Function) {
const value: any = getStorage().getItem(key);

Expand Down

0 comments on commit c0fe69b

Please sign in to comment.