@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
96 lines • 4.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useAuth;
const react_1 = require("react");
const hooks_1 = require("../../store/hooks");
const authSlice_1 = require("../../store/slices/authSlice");
const authThunks_1 = require("../../store/slices/authThunks");
const useProject_1 = __importDefault(require("../projects/useProject"));
function useAuth() {
const dispatch = (0, hooks_1.useReplykeDispatch)();
const { projectId } = (0, useProject_1.default)();
// Selectors
const initialized = (0, hooks_1.useReplykeSelector)(authSlice_1.selectInitialized);
const accessToken = (0, hooks_1.useReplykeSelector)(authSlice_1.selectAccessToken);
const refreshToken = (0, hooks_1.useReplykeSelector)(authSlice_1.selectRefreshToken);
// Actions
const handleSignUpWithEmailAndPassword = (0, react_1.useCallback)(async (props) => {
if (!projectId) {
throw new Error("No projectId available.");
}
const result = await dispatch((0, authThunks_1.signUpWithEmailAndPasswordThunk)({
projectId,
...props,
}));
if (authThunks_1.signUpWithEmailAndPasswordThunk.rejected.match(result)) {
throw new Error(result.payload);
}
}, [dispatch, projectId]);
const handleSignInWithEmailAndPassword = (0, react_1.useCallback)(async (props) => {
if (!projectId) {
throw new Error("No projectId available.");
}
const result = await dispatch((0, authThunks_1.signInWithEmailAndPasswordThunk)({
projectId,
...props,
}));
if (authThunks_1.signInWithEmailAndPasswordThunk.rejected.match(result)) {
throw new Error(result.payload);
}
}, [dispatch, projectId]);
const handleSignOut = (0, react_1.useCallback)(async () => {
if (!projectId) {
throw new Error("No projectId available.");
}
const result = await dispatch((0, authThunks_1.signOutThunk)({ projectId }));
if (authThunks_1.signOutThunk.rejected.match(result)) {
throw new Error(result.payload);
}
}, [dispatch, projectId]);
const handleChangePassword = (0, react_1.useCallback)(async (props) => {
if (!projectId) {
throw new Error("No projectId available.");
}
const result = await dispatch((0, authThunks_1.changePasswordThunk)({
projectId,
...props,
}));
if (authThunks_1.changePasswordThunk.rejected.match(result)) {
throw new Error(result.payload);
}
}, [dispatch, projectId]);
const handleRequestNewAccessToken = (0, react_1.useCallback)(async () => {
if (!projectId)
return;
const result = await dispatch((0, authThunks_1.requestNewAccessTokenThunk)({ projectId }));
if (authThunks_1.requestNewAccessTokenThunk.fulfilled.match(result)) {
return result.payload;
}
}, [dispatch, projectId]);
const handleSetRefreshToken = (0, react_1.useCallback)((token) => {
// Handle both direct value and function setter patterns
if (typeof token === 'function') {
const currentToken = refreshToken;
const newToken = token(currentToken);
dispatch((0, authSlice_1.setRefreshToken)(newToken));
}
else {
dispatch((0, authSlice_1.setRefreshToken)(token));
}
}, [dispatch, refreshToken]);
return {
initialized,
accessToken,
refreshToken,
setRefreshToken: handleSetRefreshToken,
signUpWithEmailAndPassword: handleSignUpWithEmailAndPassword,
signInWithEmailAndPassword: handleSignInWithEmailAndPassword,
signOut: handleSignOut,
changePassword: handleChangePassword,
requestNewAccessToken: handleRequestNewAccessToken,
};
}
//# sourceMappingURL=useAuth.js.map