This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
52 lines (46 loc) · 1.52 KB
/
index.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
if (typeof Kuzzle === 'undefined') {
throw new Error('kuzzle-sdk-login-oauth-popup needs the Kuzzle Javascript SDK in order to be working.');
}
if (typeof window === 'undefined') {
throw new Error('kuzzle-sdk-login-oauth-popup only work in a browser.');
}
Kuzzle.prototype.loginOauthPopup = function(strategy, options, cb) {
var
oauthWindow,
windowOption = ((options !== undefined && typeof options !== 'function') ? options : 'width=800, height=600');
if (!cb && typeof options === 'function') {
cb = options;
}
this.login(strategy, (err, res) => {
if (err) {
throw new Error(err.message);
}
oauthWindow = window.open(res.headers.Location, 'kuzzleOauthPopup', windowOption);
if (oauthWindow === undefined) {
throw new Error('Cannot open window. Make sure it isn\'t blocked by your browser.');
}
sendCodeToKuzzle(strategy, oauthWindow, cb, this);
});
};
function sendCodeToKuzzle(strategy, oauthWindow, cb, kuzzle) {
var
code;
setTimeout(() => {
try {
code = /code=([a-zA-Z0-9\-_\/]+)/.exec(oauthWindow.location.search);
} catch (ex) {} // eslint-disable-line no-empty
if (code) {
oauthWindow.close();
kuzzle.query({controller: 'auth', action: 'login'}, {strategy: strategy, code: code[1]}, (err, res) => {
if (err) {
cb(err);
return;
}
kuzzle.setJwtToken(res.result.jwt);
cb(undefined, res.result);
});
} else {
sendCodeToKuzzle(strategy, oauthWindow, cb, kuzzle);
}
}, 500);
}