-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthModule.js
More file actions
77 lines (60 loc) · 1.89 KB
/
Copy pathAuthModule.js
File metadata and controls
77 lines (60 loc) · 1.89 KB
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
'use babel';
import open from 'open';
import { Store } from './Store';
import { HttpModule } from './HttpModule';
import { Logger } from './Logger';
import { CUSTOM_EVENTS, STORE_KEYS } from '../constants/store';
import { PLUGIN } from '../constants/common';
class AuthModule {
async login() {
const { error, sessionToken, loginURL } = await HttpModule.login();
if (error) {
Store.set(STORE_KEYS.loginInProcess, false);
atom.notifications.addError(error.message, { dismissable: true });
return;
}
Store.set(STORE_KEYS.sessionToken, sessionToken);
setTimeout(() => {
this.checkSession();
}, 1000);
await open(loginURL);
}
async checkSession() {
const inProgress = Store.get(STORE_KEYS.loginInProcess);
if (!inProgress) {
return;
}
Logger.log('Check session (login progress)...');
const sessionToken = Store.get(STORE_KEYS.sessionToken);
const { error, isLoggedIn } = await HttpModule.checkSession(sessionToken);
if (error) {
Store.set(STORE_KEYS.loginInProcess, false);
const message = (error && error.message) || 'Login failed';
atom.notifications.addError(message, { dismissable: true });
return;
}
if (isLoggedIn) {
Store.setMany({
[STORE_KEYS.loginInProcess]: false,
[STORE_KEYS.accountType]: 'free',
});
atom.config.set(`${PLUGIN.name}.sessionToken`, sessionToken);
Store.emit(CUSTOM_EVENTS.didLogin);
Logger.log('Login successful!');
return;
}
setTimeout(() => {
this.checkSession();
}, 1000);
}
async checkLogin() {
const sessionToken = Store.get(STORE_KEYS.sessionToken);
const { error, isLoggedIn } = await HttpModule.checkSession(sessionToken);
if (error) {
return false;
}
return isLoggedIn;
}
}
const AuthModuleInstance = new AuthModule();
export { AuthModuleInstance as AuthModule };