forked from kallaspriit/node-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
87 lines (72 loc) · 2.17 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// return whether the script is running on iOS platform
function isIOS() {
return navigator.userAgent.match(/iPhone/i) ||navigator.userAgent.match(/iPod/i);
}
// return whether the script is running on iOS platform
function isAndroid() {
return navigator.userAgent.match(/Android/i);
}
// return whether the script is running on iOS platform
function loadScript(src, callback) {
var script,
loaded = false;
script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = script.onreadystatechange = function() {
if (!loaded && (!this.readyState || this.readyState == 'complete')) {
loaded = true;
if (typeof(callback) === 'function') {
callback();
}
}
};
document.getElementsByTagName('head')[0].appendChild(script);
}
// logs the user in
function login(username, password, callback) {
var req = new XMLHttpRequest(),
success;
req.onreadystatechange = function() {
if (req.readyState === 4) {
success = req.status === 200 ? true : false;
callback(success);
}
};
req.open('POST', '/login', true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send('username=' + username + '&password=' + password);
}
// logs out
function logout(callback) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === 4) {
success = req.status === 200 ? true : false;
callback(success);
}
};
req.open('GET', '/logout', true);
req.send();
}
// called when platform is ready
function bootstrap() {
// implement ssoReady that is called when SSO information can be requested
if (typeof(ssoReady) === 'function') {
ssoReady();
}
}
// load platform code and bootstrap the application
if (isIOS()) {
loadScript('ios/SSO.js', function() {
bootstrap();
});
} else if (isAndroid()) {
loadScript('android/SSO.js', function() {
bootstrap();
});
} else {
loadScript('browser/SSO.js', function() {
bootstrap();
});
}