-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
63 lines (57 loc) · 1.9 KB
/
login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import queryString from "query-string";
import { authSession, authJwt } from "./authentication";
import { setAuthErrorHandler, setJwt, setApiKey } from "./setup";
class Login {
constructor() {
this._doLogin = this._doLogin.bind(this);
this._refreshToken = authSession.refreshToken();
this._apiKey = authSession.apiKey();
}
_doLogin(dataSources, retry) {
const noAuthenticationTokenError = new Error("No authentication token found");
return Promise.all([this._refreshToken.read(), this._apiKey.read()])
.then(tokens => {
const refreshToken = tokens[0];
const apiKey = tokens[1];
if (refreshToken) {
return authJwt
.create({
refreshToken
})
.then(response => {
setJwt(response.accessToken);
return retry();
});
} else if (apiKey) {
setApiKey(apiKey);
return retry();
}
return Promise.reject(noAuthenticationTokenError);
})
.catch(error => {
if (error === noAuthenticationTokenError || error.message === "Unauthorized") {
return Promise.all([this._refreshToken.delete(), this._apiKey.delete()]).then(() => {
const previousLocation =
this._history.location.pathname !== this._loginRoute
? `?${queryString.stringify({
redirect: this._history.location.pathname
})}`
: "";
this._history.push(`${this._loginRoute}${previousLocation}`);
return Promise.reject(error);
});
} else {
return Promise.reject(error);
}
});
}
_configDataSources() {
setAuthErrorHandler(this._doLogin);
}
setup(history, loginRoute) {
this._history = history;
this._loginRoute = loginRoute;
this._configDataSources();
}
}
export const login = new Login();