mean-expert-official / loopback-sdk-builder

Tool for auto-generating Software Development Kits (SDKs) for LoopBack
Other
399 stars 178 forks source link

LoopBackAuth not persisting, BaseStorage methods empty #555

Open kyle-zadara opened 6 years ago

kyle-zadara commented 6 years ago

What type of issue are you creating?

What version of this module are you using?

Write other if any: 2.1.2

Please add a description for your issue:

I don't know if this is a bug or I am missing something. I was attempting for the last few days to get the social passport integration section of the wiki working but for the life of me I could not. No cookie or localStorage properties were set, i could not log in or access my LoopBack api. After diving into each call I found that "persist" in "LoopBackAuth" was using InternalStorage get, set and remove. InternalStorage extends BaseStorage. This was my auto-generated BaseStorage (removing comments for brevity):

"storage.swap.ts"

export class BaseStorage {

  get(key: string): any { }

  set(key: string, value: any, expires?: Date): void { }

  remove(key: string): void { }
}

As you can see those methods are completely empty, they do nothing. If I inject StorageBrowser and pass the get, set, and remove from there into BaseStorage, all works fine.

export class BaseStorage {
  constructor(@Inject(StorageBrowser) protected storage: StorageBrowser)
  {

  }
  get(key: string): any {
    return this.storage.get(key);
  }
  set(key: string, value: any, expires?: Date): void {
    this.storage.set(key, value, expires);
  }
  remove(key: string): void {
    this.storage.remove(key)
  }
}

Did I miss a step in my config or build to get BaseStorage methods to populate? Manually editing the generated files will not work for my build process.

mikeycrostar commented 6 years ago

I had same issues but I figure out how to fix it, If you set RememberMe to true, and don't give a TTL to your token, the expiration date of your cookie will be now... so you don't see them appear on your cookies logger, and the session doesn't persist... By default the ttl is null, I let you guess what happen when you do that...

in Auth.service.ts: let expires = new Date(today.getTime() + (this.token.ttl * 1000));

I hope it can help...

juleskreutzer commented 5 years ago

I was facing the same issue today, thanks to @mikeycrostar info about the access token, I was able to solve it with the following piece of code:

if (token.id && token.userId) {
    const access_token_object = {
          id: token.id,
          userId: token.userId,
          ttl: 1209600
    };
    const access_token = new SDKToken(access_token_object);
    this.lbAuth.setToken(access_token);
    this.lbAuth.setRememberMe(true);
    this.lbAuth.save();
}

(It could be possible that the above code can be enhanced)