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