feat: unauthenticated tab
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { getGlobal } from "reactn";
|
||||
import { navigate } from "../navigation/NavigationService";
|
||||
import { Routes } from "../navigation/Routes";
|
||||
|
||||
const redirectToRegister = () => {
|
||||
try {
|
||||
const activeRoute = getGlobal()?.activeRouteName;
|
||||
if (activeRoute === Routes.Register) {
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("authRedirect: unable to read active route", error);
|
||||
}
|
||||
navigate(Routes.Register);
|
||||
};
|
||||
|
||||
export const ensureAuthenticated = (currentUID, options = {}) => {
|
||||
const { onIntercept } = options || {};
|
||||
if (currentUID) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
if (typeof onIntercept === "function") {
|
||||
onIntercept();
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("authRedirect: onIntercept error", error);
|
||||
}
|
||||
redirectToRegister();
|
||||
return false;
|
||||
};
|
||||
|
||||
export const withAuthGuard = async (currentUID, handler, options = {}) => {
|
||||
if (!ensureAuthenticated(currentUID, options)) {
|
||||
return;
|
||||
}
|
||||
return handler?.();
|
||||
};
|
||||
|
||||
export default ensureAuthenticated;
|
||||
+3
-2
@@ -1,4 +1,5 @@
|
||||
import { arrayRemove, arrayUnion, projectsRef } from "../config/firebase";
|
||||
import { ensureAuthenticated } from "./authRedirect";
|
||||
|
||||
export const LIKE_TARGET = {
|
||||
SONG: "song",
|
||||
@@ -31,7 +32,8 @@ export const toggleProjectLike = async ({
|
||||
currentUID,
|
||||
next,
|
||||
}) => {
|
||||
if (!projectId || !currentUID) return;
|
||||
if (!projectId) return;
|
||||
if (!ensureAuthenticated(currentUID)) return;
|
||||
const fieldPath = getLikeFieldPath(target);
|
||||
await projectsRef.doc(projectId).set(
|
||||
{
|
||||
@@ -42,4 +44,3 @@ export const toggleProjectLike = async ({
|
||||
{ merge: true }
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user