UNPKG

3.85 kBJavaScriptView Raw
1var AwesomeAuth, HuntingWebsocket, config, json, storage,
2 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
3
4json = require('./util').json;
5
6storage = window.localStorage;
7
8config = require('./config');
9
10HuntingWebsocket = require('./hunting-websocket');
11
12AwesomeAuth = (function() {
13 function AwesomeAuth(opts) {
14 this.logOut = __bind(this.logOut, this);
15 this.register = __bind(this.register, this);
16 this.confirmationPoller = __bind(this.confirmationPoller, this);
17 if (opts.user) {
18 setTimeout(function() {
19 return opts.onAuthStatusChange({
20 user: opts.user
21 });
22 }, opts.pollWait || 1000);
23 return;
24 }
25 this.appName = opts.appName;
26 this.token = JSON.parse(storage.getItem("aa-token-" + this.appName));
27 this.registrationSocket = new HuntingWebsocket(opts.registrationServers || config.registrationServers);
28 this.onAuthStatusChange = opts.onAuthStatusChange;
29 this.previousStatus = null;
30 this.currentUser = null;
31 this.registerCallback = null;
32 if (!this.token) {
33 console.log("Failed to authenticate user: No token found");
34 this.onAuthStatusChange({
35 status: "user-not-found"
36 });
37 }
38 this.registrationSocket.onmessage = (function(_this) {
39 return function(event) {
40 var error, message;
41 try {
42 message = JSON.parse(event.data);
43 if (message.registrationToken) {
44 console.log(message.registrationToken);
45 _this.token = {
46 token: message.registrationToken,
47 confirmed: false
48 };
49 storage.setItem("aa-token-" + _this.appName, JSON.stringify(_this.token));
50 if (_this.registerCallback) {
51 _this.registerCallback(null, message.registrationToken);
52 }
53 return _this.confirmationPoller();
54 }
55 if (message.registrationError) {
56 if (_this.registerCallback) {
57 _this.registerCallback(message.registrationError);
58 }
59 }
60 if (message.user) {
61 _this.currentUser = message.user;
62 if (_this.onAuthStatusChange) {
63 _this.onAuthStatusChange(message);
64 }
65 return console.log(message.user);
66 } else {
67 if (_this.previousStatus !== message.status) {
68 _this.onAuthStatusChange(message);
69 console.log("Failed to authenticate user: ", message);
70 }
71 return _this.previousStatus = message.status;
72 }
73 } catch (_error) {
74 error = _error;
75 return console.log(error);
76 }
77 };
78 })(this);
79 this.registrationSocket.onopen = (function(_this) {
80 return function() {
81 if (_this.token) {
82 return _this.confirmationPoller();
83 }
84 };
85 })(this);
86 }
87
88 AwesomeAuth.prototype.confirmationPoller = function() {
89 if (this.currentUser) {
90 return;
91 }
92 console.log("Polling for auth");
93 this.registrationSocket.send(json({
94 appMapping: this.appName,
95 token: this.token.token,
96 event: "authenticate"
97 }));
98 return setTimeout(this.confirmationPoller, 3000);
99 };
100
101 AwesomeAuth.prototype.register = function(registrationData, callback) {
102 this.registrationSocket.send(json({
103 appMapping: this.appName,
104 registrationData: registrationData,
105 event: "register"
106 }));
107 return this.registerCallback = callback;
108 };
109
110 AwesomeAuth.prototype.logOut = function() {
111 this.registrationSocket.send(json({
112 appMapping: this.appName,
113 token: this.token.token,
114 event: "logOut"
115 }));
116 this.currentUser = null;
117 return storage.removeItem("aa-token-" + this.appName);
118 };
119
120 return AwesomeAuth;
121
122})();
123
124module.exports = AwesomeAuth;