Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kencckw committed Oct 2, 2018
1 parent 65886cc commit e336a27
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ function* refreshToken(action) {
```
3. Remove your token when user logs out
```typescript
function* loginSaga(action) {
const {username, password} = action.payload;
const tokenObject: ITokenObject = yield call(yourLoginApi, username, password)
yield put(myAppActions.set(tokenObject));

function* logoutSaga() {
yield put(myAppActions.remove());
yield call(yourLogoutApi);
}

```
4. Selectors
> If token is null, isTokenExpired will return true
Expand All @@ -94,7 +95,7 @@ interface IJWTConfig<S> {
}

const defaultConfigs: IJWTConfig<any> = {
getTokens: () => JSON.parse(localStorage.getItem("jwt")),
getTokens: () => JSON.parse(localStorage.getItem("jwt") || null),
setTokens: tokens => localStorage.setItem("jwt", JSON.stringify(tokens)),
stateSelector: state => state.jwt,
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-saga-jwt",
"version": "1.0.1",
"version": "1.0.2",
"description": "Token management for single page application",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/defaultConfigs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ describe("Default config", () => {
localStorage.setItem("jwt", JSON.stringify({test: "123"}));
expect(defaultConfigs.getTokens()).toEqual({test: "123"});
});

it("should not throw error if localStorage is undefined", () => {
localStorage.removeItem("jwt");
expect(defaultConfigs.getTokens()).toEqual(null);
});
});

describe("setToken", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/defaultConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IJWTConfig } from "./interface";

export const defaultConfigs: IJWTConfig<any> = {
getTokens: () => JSON.parse(localStorage.getItem("jwt")),
getTokens: () => JSON.parse(localStorage.getItem("jwt") || null),
setTokens: tokens => localStorage.setItem("jwt", JSON.stringify(tokens)),
stateSelector: state => state.jwt,
};
Expand Down

0 comments on commit e336a27

Please sign in to comment.