"use strict";
(() => {
var exports = {};
exports.id = 8230;
exports.ids = [8230];
exports.modules = {

/***/ 4957:
/***/ ((module) => {

module.exports = require("cookie-cutter");

/***/ }),

/***/ 1853:
/***/ ((module) => {

module.exports = require("next/router");

/***/ }),

/***/ 6689:
/***/ ((module) => {

module.exports = require("react");

/***/ }),

/***/ 997:
/***/ ((module) => {

module.exports = require("react/jsx-runtime");

/***/ }),

/***/ 9648:
/***/ ((module) => {

module.exports = import("axios");;

/***/ }),

/***/ 3590:
/***/ ((module) => {

module.exports = import("react-toastify");;

/***/ }),

/***/ 3039:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* unused harmony exports AuthDataContext, AuthDataProvider, useAuth */
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(997);
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6689);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var react_toastify__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3590);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4957);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(cookie_cutter__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1853);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(next_router__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _UserDataContext__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(7781);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([react_toastify__WEBPACK_IMPORTED_MODULE_2__]);
react_toastify__WEBPACK_IMPORTED_MODULE_2__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];






const AuthDataContext = /*#__PURE__*/ (/* unused pure expression or super */ null && (createContext(undefined)));
const AuthDataProvider = ({ children  })=>{
    const router = useRouter();
    const { exit  } = useUser();
    const login = async (user)=>{
        cookieCutter.set("token", user.token, {
            expires: "86400",
            path: "/"
        });
    };
    const logout = async ()=>{
        toast.success("خروج با موفقیت انجام شد!");
        await cookieCutter.set("token", "", {
            expires: "Thu, 01 Jan 1970 00:00:01 GMT"
        });
        await exit();
        await router.push("/Login");
    };
    const value = useMemo(()=>({
            // cookies,
            login,
            logout
        }), []);
    return /*#__PURE__*/ _jsx(AuthDataContext.Provider, {
        value: value,
        children: children
    });
};
const useAuth = ()=>{
    return useContext(AuthDataContext);
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 7781:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* unused harmony exports UserDataContext, UserDataProvider, useUser */
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(997);
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6689);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4957);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(cookie_cutter__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(1853);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(next_router__WEBPACK_IMPORTED_MODULE_3__);




const UserDataContext = /*#__PURE__*/ (/* unused pure expression or super */ null && (createContext(undefined)));
const UserDataProvider = ({ children  })=>{
    const [userData, setUserData] = useState({
        data: {
            id: "",
            name: "",
            email: "",
            created_at: "",
            updated_at: ""
        },
        status: false
    });
    const refetch = async ()=>{
        const token = cookieCutter.get("token");
        if (token && token.length > 0) {
            return await fetch("/api/user/me", {
                headers: {
                    Authorization: "Bearer " + token
                }
            }).then(async (r)=>{
                if (r.status === 200) {
                    const item = await r.json();
                    await setUserData({
                        data: {
                            ...item
                        },
                        status: true
                    });
                } else {
                    setUserData({
                        ...userData,
                        status: false
                    });
                    cookieCutter.set("token", "", {
                        expires: "Thu, 01 Jan 1970 00:00:01 GMT",
                        path: "/"
                    });
                }
            });
        }
        return false;
    };
    const permissions = ()=>{
        let perms = [];
        userData.data.roles.map((role)=>{
            role.permissions.map((item)=>{
                perms.push(item);
            });
        });
        return perms;
    };
    const exit = ()=>{
        setUserData({
            data: {
                id: "",
                name: "",
                email: "",
                created_at: "",
                updated_at: ""
            },
            status: false
        });
    };
    const value = useMemo(()=>({
            userData,
            refetch,
            exit,
            permissions
        }), [
        userData
    ]);
    return /*#__PURE__*/ _jsx(UserDataContext.Provider, {
        value: value,
        children: children
    });
};
const useUser = ()=>{
    return useContext(UserDataContext);
};


/***/ }),

/***/ 9006:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9648);
/* harmony import */ var _context_AuthDataContext__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3039);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([axios__WEBPACK_IMPORTED_MODULE_0__, _context_AuthDataContext__WEBPACK_IMPORTED_MODULE_1__]);
([axios__WEBPACK_IMPORTED_MODULE_0__, _context_AuthDataContext__WEBPACK_IMPORTED_MODULE_1__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);


async function handler(req, res) {
    const token = req.headers.authorization;
    const config = {
        method: "get",
        url: "https://dashboard-apis.iotacademy.ir" + "/users/me",
        ContentType: "application/x-www-form-urlencoded",
        headers: {
            Authorization: token
        }
    };
    const items = await (0,axios__WEBPACK_IMPORTED_MODULE_0__["default"])(config).then(function(response) {
        return response;
    }).catch(function(error) {
        return error;
    });
    if (items.status === 200) {
        res.status(200).json(items.data);
    } else if (items.response.status === 500) {
        res.status(500).json(items.response.statusText);
    } else if (items.response.status === 404) {
        res.status(404).json(items.response.statusText);
    } else {
        res.status(406).json(items.response.statusText);
    }
}

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = (__webpack_exec__(9006));
module.exports = __webpack_exports__;

})();