Skip to content

Commit 62db573

Browse files
fix README
1 parent f49443c commit 62db573

File tree

3 files changed

+102
-24
lines changed

3 files changed

+102
-24
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ Authmanager.config.getToken = function(credentials) {}
2525
```
2626

2727
**react-authmanager** needs:
28-
- to know how to login the user from the server and get a token back ([`config.getToken`](#gettokencredentials-async))
29-
- to know how to get the current logged user informations from the server ([`config.getUser`](#getuser-async))
28+
- to know how to login the user from the server and get a token back ([`config.getToken`](#configgettokencredentials-async))
29+
- to know how to get the current logged user informations from the server ([`config.getUser`](#configgetuser-async))
3030

3131
**you** will need:
32-
- to include the current token in your requests headers authorization ([`utils.getToken`](#gettoken))
32+
- to include the current token in your requests headers authorization ([`utils.getToken`](#utilsgettoken))
3333

3434
### Minimal configuration for the toolkit
3535
```js
@@ -59,11 +59,11 @@ fetch(..., {
5959
});
6060
```
6161

62-
*For more configurations, please read the [advanced configuration](#5---advanced-configuration) section below.*<br/>
62+
*For more configurations, please read the [advanced configuration](#5---advanced-configuration) section below.*
6363

6464
## 2 - Authenticate users
6565
**withAuth** HOC injects in your component helpers to manage authentication: **login**, **logout** and **auth**.<br/>
66-
**login** and **logout** are functions to log users. **auth** is an object that contains a state of operations.<br/>
66+
**login** and **logout** are functions to log users. **auth** is an object that contains a state of operations.
6767

6868
| prop | default | description |
6969
|:-----------|:--------|:--------------------------------------------------------------|
@@ -110,14 +110,14 @@ class LogoutComponent extends React.Component {
110110

111111
## 3 - Access user informations
112112
**withUser** HOC will automatically inject an user object in your component props.<br/>
113-
This object contains informations about the current user :<br/>
113+
This object contains informations about the current user:
114114

115-
| prop | default | description |
116-
|:-----------|:--------|:----------------------------------------------------------------------------------------------------|
117-
| user: | | `object` containing current user informations |
118-
| -- loading | false | `bool` is user currently loaded from the server |
119-
| -- logged | false | `bool` is the current user logged in (setted by [`config.isUserLogged`](#isuserloggeduser-async)) |
120-
| -- ... | null | `any` informations about the user sent by the server (setted by [`config.getUser`](#getuser-async)) |
115+
| prop | default | description |
116+
|:-----------|:--------|:--------------------------------------------------------------------------------------------------------|
117+
| user: | | `object` containing current user informations |
118+
| -- loading | false | `bool` is user currently loaded from the server |
119+
| -- logged | false | `bool` is the current user logged in (setted by [`config.isUserLogged`](#configisuserloggeduser-async)) |
120+
| -- ... | null | `any` informations about the user sent by the server (setted by [`config.getUser`](#configgetuser-async)) |
121121

122122
```js
123123
import { withUser } from 'react-authmanager';
@@ -138,7 +138,7 @@ class MyComponent extends React.Component {
138138
## 4 - Secure components
139139
**withGuard** HOC helps you protect your components in a flexible way. By example, you can render a login form instead of a component if no user is logged in.<br/>
140140
It needs a guard as parameter. A guard is just a function that returns a component, so you can easily create your own guards.<br/>
141-
A guard function has some parameters:<br/>
141+
A guard function has parameters:
142142

143143
| prop | description |
144144
|:------|:---------------------------------------------------------|
@@ -172,15 +172,15 @@ class MyComponent extends React.Component {
172172
```
173173

174174
## 5 - Advanced configuration
175-
To edit default configuration of **react-authmanager**, you have to import the `Authmanager` and override the config object :
175+
To edit default configuration of **react-authmanager**, you have to import the `Authmanager` and override the config object:
176176
```js
177177
import Authmanager from 'react-authmanager';
178178

179179
// will change the way how the toolkit will login the user and get a token back, see below
180180
Authmanager.config.getToken = function(credentials) {}
181181
```
182182

183-
### `getToken([credentials]) [async]`
183+
### `config.getToken([credentials]) [async]`
184184
Get an authentication token when an user tries to login. `getToken` is called when the auth login function is executed to store the token in *localStorage*.
185185

186186
**Parameters**
@@ -204,7 +204,7 @@ Authmanager.config.getToken = async credentials => {
204204
}
205205
```
206206

207-
### `getUser() [async]`
207+
### `config.getUser() [async]`
208208
Get the current authenticated user. `getUser` is called when the toolkit initialize its store and after an user login.
209209

210210
**Return *(Object)***
@@ -225,7 +225,7 @@ Authmanager.config.getUser = async () => {
225225
}
226226
```
227227

228-
### `isUserLogged([user]) [async]`
228+
### `config.isUserLogged([user]) [async]`
229229
Define if the current user (returned by `getUser`) is logged. `isUserLogged` is called after each user state change. The result is set in `user.logged`.
230230

231231
**Parameters**
@@ -259,7 +259,7 @@ import Authmanager from 'react-authmanager';
259259
const Authmanager.utils.getToken()
260260
```
261261

262-
### `getToken()`
262+
### `utils.getToken()`
263263
Returns the current stored token (in *localStorage*). You should use `getToken` to authorize your requests to the server
264264

265265
**Return *(String)***

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if (!window.localStorage) {
7171

7272
class Utils {
7373
getToken = () => {
74-
return localStorage.getItem(JWT_NAME)
74+
return localStorage.getItem(JWT_NAME);
7575
}
7676

7777
setToken = token => {

utils.js

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66

7-
var _flexibleCookies = require('flexible-cookies');
8-
97
var _redux = require('redux');
108

119
var _reduxThunk = require('redux-thunk');
@@ -24,19 +22,99 @@ var _userActions = require('./actions/userActions');
2422

2523
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2624

25+
const JWT_NAME = 'AUTHMANAGER-JWT-AUTH';
26+
27+
// make localStorage compatible for old browsers
28+
if (!window.localStorage) {
29+
Object.defineProperty(window, "localStorage", new function () {
30+
var aKeys = [],
31+
oStorage = {};
32+
Object.defineProperty(oStorage, "getItem", {
33+
value: function (sKey) {
34+
return sKey ? this[sKey] : null;
35+
},
36+
writable: false,
37+
configurable: false,
38+
enumerable: false
39+
});
40+
Object.defineProperty(oStorage, "key", {
41+
value: function (nKeyId) {
42+
return aKeys[nKeyId];
43+
},
44+
writable: false,
45+
configurable: false,
46+
enumerable: false
47+
});
48+
Object.defineProperty(oStorage, "setItem", {
49+
value: function (sKey, sValue) {
50+
if (!sKey) {
51+
return;
52+
}
53+
document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
54+
},
55+
writable: false,
56+
configurable: false,
57+
enumerable: false
58+
});
59+
Object.defineProperty(oStorage, "length", {
60+
get: function () {
61+
return aKeys.length;
62+
},
63+
configurable: false,
64+
enumerable: false
65+
});
66+
Object.defineProperty(oStorage, "removeItem", {
67+
value: function (sKey) {
68+
if (!sKey) {
69+
return;
70+
}
71+
document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
72+
},
73+
writable: false,
74+
configurable: false,
75+
enumerable: false
76+
});
77+
this.get = function () {
78+
var iThisIndx;
79+
for (var sKey in oStorage) {
80+
iThisIndx = aKeys.indexOf(sKey);
81+
if (iThisIndx === -1) {
82+
oStorage.setItem(sKey, oStorage[sKey]);
83+
} else {
84+
aKeys.splice(iThisIndx, 1);
85+
}
86+
delete oStorage[sKey];
87+
}
88+
for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) {
89+
oStorage.removeItem(aKeys[0]);
90+
}
91+
for (var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/); nIdx < aCouples.length; nIdx++) {
92+
aCouple = aCouples[nIdx].split(/\s*=\s*/);
93+
if (aCouple.length > 1) {
94+
oStorage[iKey = unescape(aCouple[0])] = unescape(aCouple[1]);
95+
aKeys.push(iKey);
96+
}
97+
}
98+
return oStorage;
99+
};
100+
this.configurable = false;
101+
this.enumerable = true;
102+
}());
103+
}
104+
27105
class Utils {
28106
constructor() {
29107
this.getToken = () => {
30-
return _flexibleCookies.Cookies.get('JWT-AUTH');
108+
return localStorage.getItem(JWT_NAME);
31109
};
32110

33111
this.setToken = token => {
34-
_flexibleCookies.Cookies.set('JWT-AUTH', token);
112+
localStorage.setItem(JWT_NAME, token);
35113
return true;
36114
};
37115

38116
this.deleteToken = () => {
39-
_flexibleCookies.Cookies.delete('JWT-AUTH');
117+
localStorage.setItem(JWT_NAME, null);
40118
return true;
41119
};
42120

0 commit comments

Comments
 (0)