UNPKG

@rtbjs/use-state

Version:

`@rtbjs/use-state` is a state management tool that can act as a local state and be easily turned into a global redux state. It is an innovative approach to state management that combines the advantages of both React's useState and Redux's state management

119 lines 5.58 kB
"use strict"; exports.__esModule = true; exports.useResetPasswordMutation = exports.useForgotPasswordMutation = exports.useVerifyEmailMutation = exports.useLogoutUserMutation = exports.useRegisterUserMutation = exports.useLoginUserMutation = exports.authApi = void 0; var tslib_1 = require("tslib"); var user_slice_1 = require("../features/user-slice"); var main_api_1 = require("./main-api"); var user_api_1 = require("./user-api"); exports.authApi = main_api_1.mainApi.injectEndpoints({ endpoints: function (builder) { return ({ registerUser: builder.mutation({ query: function (data) { return { url: 'auth/register', method: 'POST', body: data }; } }), loginUser: builder.mutation({ query: function (data) { return { url: 'auth/login', method: 'POST', body: data, credentials: 'same-origin' }; }, onQueryStarted: function (_args, _a) { var dispatch = _a.dispatch, queryFulfilled = _a.queryFulfilled; return tslib_1.__awaiter(this, void 0, void 0, function () { var data, error_1; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); return [4 /*yield*/, queryFulfilled]; case 1: data = (_b.sent()).data; localStorage.setItem('toolboxjs_access_token', data.access_token); localStorage.setItem('toolboxjs_refresh_token', data.refresh_token); dispatch((0, user_slice_1.setLoggedIn)(true)); return [4 /*yield*/, dispatch(user_api_1.userApi.endpoints.getMe.initiate(null))]; case 2: _b.sent(); return [3 /*break*/, 4]; case 3: error_1 = _b.sent(); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); } }), logoutUser: builder.mutation({ query: function () { return { url: 'auth/logout', credentials: 'same-origin' }; }, onQueryStarted: function (_args, _a) { var dispatch = _a.dispatch; return tslib_1.__awaiter(this, void 0, void 0, function () { var error_2; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); localStorage.removeItem('toolboxjs_access_token'); localStorage.removeItem('toolboxjs_refresh_token'); dispatch((0, user_slice_1.setLoggedIn)(false)); return [4 /*yield*/, dispatch(user_api_1.userApi.endpoints.getMe.initiate(null))]; case 1: _b.sent(); return [3 /*break*/, 3]; case 2: error_2 = _b.sent(); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }, invalidatesTags: [{ type: 'Users' }] }), verifyEmail: builder.mutation({ query: function (verificationCode) { return { url: "auth/verifyemail/".concat(verificationCode), credentials: 'same-origin' }; } }), forgotPassword: builder.mutation({ query: function (body) { return { url: "auth/forgotpassword", method: 'POST', credentials: 'same-origin', body: body }; } }), resetPassword: builder.mutation({ query: function (_a) { var resetToken = _a.resetToken, password = _a.password, passwordConfirm = _a.passwordConfirm; return { url: "auth/resetpassword/".concat(resetToken), method: 'PATCH', body: { password: password, passwordConfirm: passwordConfirm }, credentials: 'same-origin' }; } }) }); } }); exports.useLoginUserMutation = exports.authApi.useLoginUserMutation, exports.useRegisterUserMutation = exports.authApi.useRegisterUserMutation, exports.useLogoutUserMutation = exports.authApi.useLogoutUserMutation, exports.useVerifyEmailMutation = exports.authApi.useVerifyEmailMutation, exports.useForgotPasswordMutation = exports.authApi.useForgotPasswordMutation, exports.useResetPasswordMutation = exports.authApi.useResetPasswordMutation; //# sourceMappingURL=auth-api.js.map