Welcome Guest, Not a member yet? Register   Sign In
how to handle csrf exception with ajax
#1

Hello community members. In the documentation, i could not find an example for handling exception for ajax calls, as documentation is telling me that whenever csrf token fails, it will throw exception, but i could not catch exception by using the following code in my controller

try {
       //My Code here, I skipped here.
} catch (\CodeIgniter\Security\Exceptions\SecurityException $e) {
//I also tried catch in this way catch(\Exception $e) {, but didnot work
       //code to handle exception here
}

//Note that I am trying this with ajax calls only.


I always get this in reply
{
    "title": "CodeIgniter\\Security\\Exceptions\\SecurityException",
    "type": "CodeIgniter\\Security\\Exceptions\\SecurityException",
    "code": 403,
    "message": "The action you requested is not allowed.",
    "file": "/Users/qamarhafeez/www/pos/new-pos/system/Security/Exceptions/SecurityException.php",
    "line": 10,
    "trace": [
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/system/Security/Security.php",
            "line": 233,
            "function": "forDisallowedAction",
            "class": "CodeIgniter\\Security\\Exceptions\\SecurityException",
            "type": "::",
            "args": []
        },
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/system/Filters/CSRF.php",
            "line": 88,
            "function": "CSRFVerify",
            "class": "CodeIgniter\\Security\\Security",
            "type": "->",
            "args": [
                {
                    "uri": {},
                    "config": {
                        "baseURL": "http://point-of-sale.localhost/new-pos/public",
                        "indexPage": "",
                        "uriProtocol": "REQUEST_URI",
                        "defaultLocale": "en",
                        "negotiateLocale": false,
                        "supportedLocales": [
                            "en"
                        ],
                        "appTimezone": "Asia/Karachi",
                        "charset": "UTF-8",
                        "forceGlobalSecureRequests": false,
                        "sessionDriver": "CodeIgniter\\Session\\Handlers\\FileHandler",
                        "sessionCookieName": "ci_session",
                        "sessionExpiration": 7200,
                        "sessionSavePath": "/Users/qamarhafeez/www/pos/new-pos/writable/session",
                        "sessionMatchIP": false,
                        "sessionTimeToUpdate": 300,
                        "sessionRegenerateDestroy": false,
                        "cookiePrefix": "",
                        "cookieDomain": "",
                        "cookiePath": "/",
                        "cookieSecure": false,
                        "cookieHTTPOnly": false,
                        "proxyIPs": "",
                        "CSRFTokenName": "csrf_token_name",
                        "CSRFHeaderName": "X-CSRF-TOKEN",
                        "CSRFCookieName": "csrf_cookie_name",
                        "CSRFExpire": 7200,
                        "CSRFRegenerate": true,
                        "CSRFRedirect": true,
                        "CSPEnabled": false
                    }
                }
            ]
        },
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/system/Filters/Filters.php",
            "line": 173,
            "function": "before",
            "class": "CodeIgniter\\Filters\\CSRF",
            "type": "->",
            "args": [
                {
                    "uri": {},
                    "config": {
                        "baseURL": "http://point-of-sale.localhost/new-pos/public",
                        "indexPage": "",
                        "uriProtocol": "REQUEST_URI",
                        "defaultLocale": "en",
                        "negotiateLocale": false,
                        "supportedLocales": [
                            "en"
                        ],
                        "appTimezone": "Asia/Karachi",
                        "charset": "UTF-8",
                        "forceGlobalSecureRequests": false,
                        "sessionDriver": "CodeIgniter\\Session\\Handlers\\FileHandler",
                        "sessionCookieName": "ci_session",
                        "sessionExpiration": 7200,
                        "sessionSavePath": "/Users/qamarhafeez/www/pos/new-pos/writable/session",
                        "sessionMatchIP": false,
                        "sessionTimeToUpdate": 300,
                        "sessionRegenerateDestroy": false,
                        "cookiePrefix": "",
                        "cookieDomain": "",
                        "cookiePath": "/",
                        "cookieSecure": false,
                        "cookieHTTPOnly": false,
                        "proxyIPs": "",
                        "CSRFTokenName": "csrf_token_name",
                        "CSRFHeaderName": "X-CSRF-TOKEN",
                        "CSRFCookieName": "csrf_cookie_name",
                        "CSRFExpire": 7200,
                        "CSRFRegenerate": true,
                        "CSRFRedirect": true,
                        "CSPEnabled": false
                    }
                },
                null
            ]
        },
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/system/CodeIgniter.php",
            "line": 382,
            "function": "run",
            "class": "CodeIgniter\\Filters\\Filters",
            "type": "->",
            "args": [
                "user/ajax_handle",
                "before"
            ]
        },
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/system/CodeIgniter.php",
            "line": 312,
            "function": "handleRequest",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->",
            "args": [
                null,
                {
                    "handler": "file",
                    "backupHandler": "dummy",
                    "storePath": "/Users/qamarhafeez/www/pos/new-pos/writable/cache/",
                    "cacheQueryString": false,
                    "prefix": "",
                    "memcached": {
                        "host": "127.0.0.1",
                        "port": 11211,
                        "weight": 1,
                        "raw": false
                    },
                    "redis": {
                        "host": "127.0.0.1",
                        "password": null,
                        "port": 6379,
                        "timeout": 0,
                        "database": 0
                    },
                    "validHandlers": {
                        "dummy": "CodeIgniter\\Cache\\Handlers\\DummyHandler",
                        "file": "CodeIgniter\\Cache\\Handlers\\FileHandler",
                        "memcached": "CodeIgniter\\Cache\\Handlers\\MemcachedHandler",
                        "predis": "CodeIgniter\\Cache\\Handlers\\PredisHandler",
                        "redis": "CodeIgniter\\Cache\\Handlers\\RedisHandler",
                        "wincache": "CodeIgniter\\Cache\\Handlers\\WincacheHandler"
                    }
                },
                false
            ]
        },
        {
            "file": "/Users/qamarhafeez/www/pos/new-pos/public/index.php",
            "line": 45,
            "function": "run",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->",
            "args": []
        }
    ]
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB