forked from postalsys/imapflow
/
login.ts
29 lines (25 loc) · 858 Bytes
/
login.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
import { getStatusCode, getErrorText } from '../tools.js';
// Authenticates user using LOGIN
export const LOGIN = async (connection, username, password) => {
if (connection.state !== connection.states.NOT_AUTHENTICATED) {
// nothing to do here
return;
}
try {
let response = await connection.exec('LOGIN', [
{ type: 'ATOM', value: username },
{ type: 'ATOM', value: password, sensitive: true }
]);
response.next();
connection.authCapabilities.set('LOGIN', true);
return username;
} catch (err) {
let errorCode = getStatusCode(err.response);
if (errorCode) {
err.serverResponseCode = errorCode;
}
err.authenticationFailed = true;
err.response = await getErrorText(err.response);
throw err;
}
};