File size: 1,950 Bytes
91d9d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
var utils = require("../utils");

module.exports = function (http, apis, ctx) {
    return function logout(callback) {
        var pCallback;
        var returnPromise = new Promise(function (resolve, reject) {
            pCallback = error => error ? reject(error) : resolve();
        });

        if (typeof callback !== "function")
            callback = pCallback

        var form = {
            pmid: "0"
        }

        http
            .post("https://www.facebook.com/bluebar/modern_settings_menu/?help_type=364455653583099&show_contextual_help=1", ctx.jar, form)
            .then(utils.parseAndCheckLogin(ctx, http))
            .then(function (res) {
                var elem = res.jsmods.instances[0][2][0].filter(item => item.value === "logout")[0];
                var body = res.jsmods.markup.filter(item => item[0] === elem.markup.__m)[0][1].__html;
                var match = [...body.matchAll(/<input[^>]*name="([^"]+)"[^>]*value="([^"]+)"[^>]*>/g)].map(item => item[2]);
                var form = {
                    jazoest: match[0],
                    fb_dtsg: match[1],
                    ref: match[2],
                    h: match[3]
                }

                return http.post("https://www.facebook.com/logout.php", ctx.jar, form);
            })
            .then(function (res) {
                if (!res.headers)
                    throw {
                        error: "An error occurred when logging out."
                    }

                return http.get(res.headers.location, ctx.jar);
            })
            .then(function () {
                ctx.isLogin = false;
                return callback();
            })
            .catch(function (error) {
                if (error.type === "logout.")
                    ctx.isLogin = false;
                return callback(error);
            });

        return returnPromise;
    }
}