Skip to content

Optionally add JWT to localStorage #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged

Optionally add JWT to localStorage #19

merged 1 commit into from
Nov 27, 2017

Conversation

richmolj
Copy link
Contributor

If used from the browser, you can now get/set the JWT from localStorage:

Config.setup({
  jwtOwners: [...],
  jwtLocalStorage: 'jwt' // or whatever key you'd like to set
})

jwtLocalStorage is defaulted to jwt, meaning this functionality is
opt-out. Pass false to opt-out.

When enabled, this will automatically set the JWT via local storage, and
update it when the server contains the X-JWT header.

@richmolj
Copy link
Contributor Author

@wadetandy

@@ -15,15 +15,23 @@ export default class Config {
static models: Array<typeof Model> = [];
static typeMapping: Object = {};
static logger: Logger = new Logger();
static jwtLocalStorage: string | boolean = 'jwt';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more correct is string | false since a value of true would throw an error when using it to set the value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also while it makes sense to default on, there is no localStorage API on the server, so we should probably have it default more intelligently depending on whether the API exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config.jwtLocalStorage would == 'jwt', but no localStorage logic would fire. So I think this is OK.

Good catch on false.

If used from the browser, you can now get/set the JWT from localStorage:

```ts
Config.setup({
  jwtOwners: [...],
  jwtLocalStorage: 'jwt' // or whatever key you'd like to set
})
```

`jwtLocalStorage` is defaulted to `jwt`, meaning this functionality is
**opt-out**. Pass `false` to opt-out.

When enabled, this will automatically set the JWT via local storage, and
update it when the server contains the `X-JWT` header.
@wadetandy
Copy link
Contributor

wadetandy commented Nov 27, 2017 via email

@wadetandy wadetandy merged commit 7d7803d into jsonapi-suite:master Nov 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants