"use strict";
/*
 * ATTENTION: An "eval-source-map" devtool has been used.
 * This devtool is neither made for production nor for readable output files.
 * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
 * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
 * or disable the default devtool with "devtool: false".
 * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
 */
self["webpackHotUpdate_N_E"]("app/layout",{

/***/ "(app-pages-browser)/./context/AuthContext.tsx":
/*!*********************************!*\
  !*** ./context/AuthContext.tsx ***!
  \*********************************/
/***/ (function(module, __webpack_exports__, __webpack_require__) {

eval(__webpack_require__.ts("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */   AuthProvider: function() { return /* binding */ AuthProvider; },\n/* harmony export */   useAuth: function() { return /* binding */ useAuth; }\n/* harmony export */ });\n/* harmony import */ var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ \"(app-pages-browser)/./node_modules/next/dist/compiled/react/jsx-dev-runtime.js\");\n/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react */ \"(app-pages-browser)/./node_modules/next/dist/compiled/react/index.js\");\n/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);\n/* provided dependency */ var process = __webpack_require__(/*! process */ \"(app-pages-browser)/./node_modules/next/dist/build/polyfills/process.js\");\n/* __next_internal_client_entry_do_not_use__ AuthProvider,useAuth auto */ \nvar _s = $RefreshSig$(), _s1 = $RefreshSig$();\n\nvar _process_env_NEXT_PUBLIC_API_BASE_URL;\nconst API_BASE_URL = (_process_env_NEXT_PUBLIC_API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL) !== null && _process_env_NEXT_PUBLIC_API_BASE_URL !== void 0 ? _process_env_NEXT_PUBLIC_API_BASE_URL : \"http://localhost:8080\";\nconst AuthContext = /*#__PURE__*/ (0,react__WEBPACK_IMPORTED_MODULE_1__.createContext)(undefined);\nconst TOKEN_KEY = \"ballistic_auth_token\";\nconst USER_KEY = \"ballistic_auth_user\";\nfunction AuthProvider(param) {\n    let { children } = param;\n    _s();\n    const [user, setUser] = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)(null);\n    const [token, setToken] = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)(null);\n    const [loading, setLoading] = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)(true); // start true while we hydrate + reuse for calls\n    // hydrate from localStorage\n    (0,react__WEBPACK_IMPORTED_MODULE_1__.useEffect)(()=>{\n        try {\n            if (false) {}\n            const storedToken = window.localStorage.getItem(TOKEN_KEY);\n            const storedUser = window.localStorage.getItem(USER_KEY);\n            if (storedToken && storedUser) {\n                setToken(storedToken);\n                setUser(JSON.parse(storedUser));\n            }\n        } catch (e) {\n        // ignore\n        } finally{\n            setLoading(false);\n        }\n    }, []);\n    const setSession = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)((newToken, newUser)=>{\n        setToken(newToken);\n        setUser(newUser);\n        if (true) {\n            window.localStorage.setItem(TOKEN_KEY, newToken);\n            window.localStorage.setItem(USER_KEY, JSON.stringify(newUser));\n        }\n    }, []);\n    const clearSession = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)(()=>{\n        setToken(null);\n        setUser(null);\n        if (true) {\n            window.localStorage.removeItem(TOKEN_KEY);\n            window.localStorage.removeItem(USER_KEY);\n        }\n    }, []);\n    const register = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)(async (params)=>{\n        setLoading(true);\n        try {\n            const res = await fetch(\"\".concat(API_BASE_URL, \"/api/auth/register\"), {\n                method: \"POST\",\n                headers: {\n                    \"Content-Type\": \"application/json\"\n                },\n                body: JSON.stringify({\n                    email: params.email,\n                    password: params.password,\n                    displayName: params.displayName\n                })\n            });\n            if (!res.ok) {\n                const text = await res.text();\n                throw new Error(text || \"Failed to create account\");\n            }\n            const data = await res.json();\n            var _data_displayName;\n            setSession(data.token, {\n                email: data.email,\n                displayName: (_data_displayName = data.displayName) !== null && _data_displayName !== void 0 ? _data_displayName : null,\n                role: data.role\n            });\n        } finally{\n            setLoading(false);\n        }\n    }, [\n        setSession\n    ]);\n    const login = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)(async (params)=>{\n        setLoading(true);\n        try {\n            const res = await fetch(\"\".concat(API_BASE_URL, \"/api/auth/login\"), {\n                method: \"POST\",\n                headers: {\n                    \"Content-Type\": \"application/json\"\n                },\n                body: JSON.stringify({\n                    email: params.email,\n                    password: params.password\n                })\n            });\n            if (!res.ok) {\n                const text = await res.text();\n                throw new Error(text || \"Failed to log in\");\n            }\n            const data = await res.json();\n            var _data_displayName;\n            setSession(data.token, {\n                email: data.email,\n                displayName: (_data_displayName = data.displayName) !== null && _data_displayName !== void 0 ? _data_displayName : null,\n                role: data.role\n            });\n        } finally{\n            setLoading(false);\n        }\n    }, [\n        setSession\n    ]);\n    const logout = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)(()=>{\n        clearSession();\n    }, [\n        clearSession\n    ]);\n    const getAuthHeaders = (0,react__WEBPACK_IMPORTED_MODULE_1__.useCallback)(()=>{\n        if (!token) return {};\n        return {\n            Authorization: \"Bearer \".concat(token)\n        };\n    }, [\n        token\n    ]);\n    const value = {\n        user,\n        loading,\n        login,\n        register,\n        logout,\n        getAuthHeaders\n    };\n    return /*#__PURE__*/ (0,react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__.jsxDEV)(AuthContext.Provider, {\n        value: value,\n        children: children\n    }, void 0, false, {\n        fileName: \"/Users/sean/Dev/gunbuilder-prototype/context/AuthContext.tsx\",\n        lineNumber: 181,\n        columnNumber: 10\n    }, this);\n}\n_s(AuthProvider, \"FyBF3GfQ0aSoDuYjEKqpAbOwwL4=\");\n_c = AuthProvider;\nfunction useAuth() {\n    _s1();\n    const ctx = (0,react__WEBPACK_IMPORTED_MODULE_1__.useContext)(AuthContext);\n    if (!ctx) {\n        throw new Error(\"useAuth must be used within an AuthProvider\");\n    }\n    return ctx;\n}\n_s1(useAuth, \"/dMy7t63NXD4eYACoT93CePwGrg=\");\nvar _c;\n$RefreshReg$(_c, \"AuthProvider\");\n\n\n;\n    // Wrapped in an IIFE to avoid polluting the global scope\n    ;\n    (function () {\n        var _a, _b;\n        // Legacy CSS implementations will `eval` browser code in a Node.js context\n        // to extract CSS. For backwards compatibility, we need to check we're in a\n        // browser context before continuing.\n        if (typeof self !== 'undefined' &&\n            // AMP / No-JS mode does not inject these helpers:\n            '$RefreshHelpers$' in self) {\n            // @ts-ignore __webpack_module__ is global\n            var currentExports = module.exports;\n            // @ts-ignore __webpack_module__ is global\n            var prevSignature = (_b = (_a = module.hot.data) === null || _a === void 0 ? void 0 : _a.prevSignature) !== null && _b !== void 0 ? _b : null;\n            // This cannot happen in MainTemplate because the exports mismatch between\n            // templating and execution.\n            self.$RefreshHelpers$.registerExportsForReactRefresh(currentExports, module.id);\n            // A module can be accepted automatically based on its exports, e.g. when\n            // it is a Refresh Boundary.\n            if (self.$RefreshHelpers$.isReactRefreshBoundary(currentExports)) {\n                // Save the previous exports signature on update so we can compare the boundary\n                // signatures. We avoid saving exports themselves since it causes memory leaks (https://github.com/vercel/next.js/pull/53797)\n                module.hot.dispose(function (data) {\n                    data.prevSignature =\n                        self.$RefreshHelpers$.getRefreshBoundarySignature(currentExports);\n                });\n                // Unconditionally accept an update to this module, we'll check if it's\n                // still a Refresh Boundary later.\n                // @ts-ignore importMeta is replaced in the loader\n                module.hot.accept();\n                // This field is set when the previous version of this module was a\n                // Refresh Boundary, letting us know we need to check for invalidation or\n                // enqueue an update.\n                if (prevSignature !== null) {\n                    // A boundary can become ineligible if its exports are incompatible\n                    // with the previous exports.\n                    //\n                    // For example, if you add/remove/change exports, we'll want to\n                    // re-execute the importing modules, and force those components to\n                    // re-render. Similarly, if you convert a class component to a\n                    // function, we want to invalidate the boundary.\n                    if (self.$RefreshHelpers$.shouldInvalidateReactRefreshBoundary(prevSignature, self.$RefreshHelpers$.getRefreshBoundarySignature(currentExports))) {\n                        module.hot.invalidate();\n                    }\n                    else {\n                        self.$RefreshHelpers$.scheduleUpdate();\n                    }\n                }\n            }\n            else {\n                // Since we just executed the code for the module, it's possible that the\n                // new exports made it ineligible for being a boundary.\n                // We only care about the case when we were _previously_ a boundary,\n                // because we already accepted this update (accidental side effect).\n                var isNoLongerABoundary = prevSignature !== null;\n                if (isNoLongerABoundary) {\n                    module.hot.invalidate();\n                }\n            }\n        }\n    })();\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 11308 characters]\n//# sourceURL=webpack-internal:///(app-pages-browser)/./context/AuthContext.tsx\n"));

/***/ })

});