Skip to content

Commit

Permalink
Merge pull request #15 from Haventec/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
htclifford authored May 29, 2019
2 parents c54621d + 884e489 commit 6d84db4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haventec/authenticate-web-sdk",
"version": "1.1.0",
"version": "1.1.1",
"description": "Haventec Authenticate Web SDK",
"keywords": [],
"author": "Haventec <support@haventec.com>",
Expand Down
3 changes: 2 additions & 1 deletion src/api/haventec.authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class HaventecAuthenticate {

private ht_dataService: HT_DataService;

constructor() {
constructor(username?: string) {
this.ht_dataService = new HT_DataService(username);
}

public initialiseStorage(username: string) {
Expand Down
37 changes: 25 additions & 12 deletions src/helpers/ht_data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ import { IHaventecAuthenticateResponseObject } from '../model/haventec.authentic

export class HT_DataService {

private username: string;
private _username: string;
private username_key: string = 'haventec_username';
private session_key: string;
private local_key: string;

constructor(username: string) {
username = username.toLowerCase();
this.setUsername(username);
let data = this.getData();
if (!data.saltBits) {
data.saltBits = JSON.stringify(HaventecCommon.generateSalt());
this.setData(data);
constructor(username?: string) {

let localUsername = username;

if ( !localUsername ) {
const haventec_username = localStorage.getItem(this.username_key);
if ( haventec_username ) {
localUsername = haventec_username.toString();
}
}

if ( localUsername ) {
localUsername = localUsername.toLowerCase().replace(/\"/g, '');

this.setUsername(localUsername);
let data: HT_Data = this.getData();
if (!data.saltBits) {
data.saltBits = JSON.stringify(HaventecCommon.generateSalt());
this.setData(data);
}
}
}

Expand Down Expand Up @@ -58,7 +71,7 @@ export class HT_DataService {
}

public getUsername(): string {
return this.username;
return this._username;
}

public getActiveUsernames(): string[] {
Expand All @@ -70,11 +83,11 @@ export class HT_DataService {
.map(key => (<HT_Data>HT_LocalStorage.getItem(key)).username);
}

private setUsername(username: string): void {
this.username = username;
private setUsername(username): void {
this._username = username;
this.session_key = 'ht_' + username + '_sessiondata';
this.local_key = 'ht_' + username + '_localdata';
HT_LocalStorage.setItem(this.username_key, username);
localStorage.setItem(this.username_key, username);
}

public removeUsername(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/model/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export class HT_Error {
public errorCode: ErrorCode,
public message: ErrorMessage){
}
}
}
9 changes: 7 additions & 2 deletions src/test/HT_Authenticate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IHaventecAuthenticateResponseObject } from "../model/haventec.authentic

describe("HT_Authenticate", function () {

let haventecAuthenticate: HaventecAuthenticate = new HaventecAuthenticate();
let haventecAuthenticate: HaventecAuthenticate = new HaventecAuthenticate("username");

beforeAll(() => {
haventecAuthenticate.initialiseStorage("username");
Expand All @@ -19,6 +19,11 @@ describe("HT_Authenticate", function () {
spyOn(HT_SessionStorage, 'getItem');
});

it("calls getAccessToken() without initialisation to ensure this is valid but returns undefined", function () {
const haventecAuthenticate2: HaventecAuthenticate = new HaventecAuthenticate();
expect(haventecAuthenticate2.getAccessToken()).toBeUndefined();
});

it("throws error if user tries to create an object with undefined/null value for username", function () {
try {
haventecAuthenticate.initialiseStorage(undefined);
Expand All @@ -28,7 +33,7 @@ describe("HT_Authenticate", function () {
}
});



it("calls the associated function in dataservice if updatestorage is called", function () {
try {
Expand Down

0 comments on commit 6d84db4

Please sign in to comment.