forked from postalsys/imapflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logout.ts
32 lines (30 loc) · 904 Bytes
/
logout.ts
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
// Logs out user and closes connection
export const LOGOUT = async connection => {
if (connection.state === connection.states.LOGOUT) {
// nothing to do here
return false;
}
if (connection.state === connection.states.NOT_AUTHENTICATED) {
connection.state = connection.states.LOGOUT;
connection.close();
return false;
}
let response;
try {
response = await connection.exec('LOGOUT');
return true;
} catch (err) {
if (err.code === 'NoConnection') {
return true;
}
connection.log.warn({ err, cid: connection.id });
return false;
} finally {
// close even if command failed
connection.state = connection.states.LOGOUT;
if (response && typeof response.next === 'function') {
response.next();
}
connection.close();
}
};