login()

in salesforce/lib/connection.js [179:229]


    login(options) {
        let body = [
            "<se:Envelope xmlns:se='http://schemas.xmlsoap.org/soap/envelope/'>",
            "<se:Header/>",
            "<se:Body>",
            "<login xmlns='urn:partner.soap.sforce.com'>",
            "<username>" + esc(options.username) + "</username>",
            "<password>" + esc([options.password, options.security_token].join("")) + "</password>",
            "</login>",
            "</se:Body>",
            "</se:Envelope>"
        ].join("");

        let soapLoginEndpoint = [this.loginUrl, "services/Soap/u", this.version].join("/");

        return request({
            method: "POST",
            url: soapLoginEndpoint,
            body: body,
            headers: {
                "Content-Type": "text/xml",
                "SOAPAction": '""'
            },
            resolveWithFullResponse: true
        }).then((response) => {
            this.logger.debug("SOAP response = " + response.body);
            let parsedResponse = parseXML(response.body);
            let responseBody = parsedResponse["soapenv:Envelope"]["soapenv:Body"];
            let { serverUrl, sessionId, userId, userInfo: { organizationId } } = responseBody.loginResponse.result;
            let idUrl = soapLoginEndpoint.split("/").slice(0, 3).join("/") + "/id/" + organizationId + "/" + userId;
            let userInfo = {
                id: userId,
                organizationId: organizationId,
                url: idUrl
            };
            this.initialize({
                serverUrl: serverUrl.split("/").slice(0, 3).join("/"),
                sessionId: sessionId,
                userInfo: userInfo
            });
            this.logger.debug("<login> completed. user id = " + userId + ", org id = " + organizationId);
            return userInfo;

        })
            .catch(err => {
                this.logger.debug("<login> error. error details = " + err);
                const error = err.error.match(/<faultstring>([^<]+)<\/faultstring>/);
                const faultstring = error && error[1];
                throw new Error(faultstring || err.message);
            });
    }