forked from jnsahaj/tweakcn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-guards.ts
More file actions
66 lines (53 loc) · 1.6 KB
/
use-guards.ts
File metadata and controls
66 lines (53 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { authClient } from "@/lib/auth-client";
import { useAuthStore } from "@/store/auth-store";
import { toast } from "@/hooks/use-toast";
import { AI_REQUEST_FREE_TIER_LIMIT } from "@/lib/constants";
import { PostLoginActionType } from "./use-post-login-action";
import { useSubscription } from "./use-subscription";
export function useGuards() {
const { checkValidSession } = useSessionGuard();
const { checkValidSubscription } = useSubscriptionGuard();
return {
checkValidSession,
checkValidSubscription,
};
}
export function useSessionGuard() {
const { data: session } = authClient.useSession();
const { openAuthDialog } = useAuthStore();
const checkValidSession = (
mode: "signin" | "signup" = "signin",
postLoginActionType?: PostLoginActionType,
postLoginActionData?: any
) => {
if (!session) {
openAuthDialog(mode, postLoginActionType, postLoginActionData);
return false;
}
return true;
};
return {
checkValidSession,
};
}
export function useSubscriptionGuard() {
const { subscriptionStatus, isPending } = useSubscription();
const checkValidSubscription = () => {
if (isPending) return false;
if (!subscriptionStatus) return false;
const { requestsRemaining } = subscriptionStatus;
if (requestsRemaining <= 0) {
toast({
title: "Daily limit reached",
description:
`You've used all ${AI_REQUEST_FREE_TIER_LIMIT} free AI requests for today. Come back tomorrow!`,
variant: "destructive",
});
return false;
}
return true;
};
return {
checkValidSubscription,
};
}