Skip to content

Commit

Permalink
Add support for NodeJS
Browse files Browse the repository at this point in the history
Now cookies are only used if the package is used
in browser.
  • Loading branch information
schettn committed Nov 27, 2020
1 parent 2e5a67d commit b7f7b86
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
32 changes: 26 additions & 6 deletions src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface ISession {
class Session implements ISession {
sessions: { [id: string]: ISession } = {};
tokenName: string = "token";
_token: string | undefined = undefined;

/**
* Initializes a base Session.
Expand All @@ -41,9 +42,11 @@ class Session implements ISession {
* @returns {string | undefined} A users JWT if set
*/
get token(): string | undefined {
const token = Cookies.get(this.tokenName);

return token ? token : undefined;
if (this.checkPlatform() === "WEB") {
return Cookies.get(this.tokenName);
} else {
return this._token;
}
}

//> Setter
Expand All @@ -55,14 +58,31 @@ class Session implements ISession {
* the cookie will be removed.
*/
set token(value: string | undefined) {
if (value) {
Cookies.set(this.tokenName, value, { secure: true, sameSite: "Lax" });
if (this.checkPlatform() === "WEB") {
if (value) {
Cookies.set(this.tokenName, value ? value : "", {
/* Expire time is set to 4 minutes */
expires: 4 / 1440,
});
} else {
Cookies.remove(this.tokenName);
}
} else {
Cookies.remove(this.tokenName);
this._token = value;
}
}

//> Methods
/**
* Check if snek-client is used by node or web.
*/
checkPlatform() {
if (typeof window === "undefined") {
return "NODE";
} else {
return "WEB";
}
}
/**
* Add a subSession to a session.
*
Expand Down
34 changes: 17 additions & 17 deletions src/session/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GithubSession extends Session {
/** @class CookieSession extends token session handling with cookies */
class CookieSession extends Session {
refreshTokenName: string = "refresh";
_refreshToken: string | undefined = undefined;

/**
* Initializes a cookie session.
Expand Down Expand Up @@ -94,9 +95,11 @@ class CookieSession extends Session {
* @returns {string | undefined} A users JWT if set
*/
get refreshToken(): string | undefined {
const token = Cookies.get(this.refreshTokenName);

return token ? token : undefined;
if (this.checkPlatform() === "WEB") {
return Cookies.get(this.refreshTokenName);
} else {
return this._token;
}
}

//> Setter
Expand All @@ -109,14 +112,7 @@ class CookieSession extends Session {
* minutes.
*/
set token(value: string | undefined) {
if (value) {
Cookies.set(this.tokenName, value ? value : "", {
/* Expire time is set to 4 minutes */
expires: 4 / 1440,
});
} else {
Cookies.remove(this.tokenName);
}
super.token = value;
}

/**
Expand All @@ -128,13 +124,17 @@ class CookieSession extends Session {
* set to six days.
*/
set refreshToken(value: string | undefined) {
if (value) {
Cookies.set(this.refreshTokenName, value, {
/* Expire time is set to 6 days */
expires: 6,
});
if (this.checkPlatform() === "WEB") {
if (value) {
Cookies.set(this.refreshTokenName, value ? value : "", {
/* Expire time is set to 6 days */
expires: 6,
});
} else {
Cookies.remove(this.refreshTokenName);
}
} else {
Cookies.remove(this.refreshTokenName);
this._refreshToken = value;
}
}
}
Expand Down

0 comments on commit b7f7b86

Please sign in to comment.