Skip to content

Commit f61ddcf

Browse files
committed
use appAuth for auth
1 parent 684c513 commit f61ddcf

5 files changed

Lines changed: 21 additions & 26 deletions

File tree

TechStacks.Client/src/app/posts/[id]/[slug]/PostDetailClient.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import { formatDistanceToNow } from 'date-fns';
77
import { PrimaryButton } from '@servicestack/react';
88
import { useAuthorization } from '@/lib/hooks/useAuthorization';
99
import { useAppStore } from '@/lib/stores/useAppStore';
10+
import { appAuth } from '@/lib/auth';
1011
import routes from '@/lib/utils/routes';
1112
import * as gateway from '@/lib/api/gateway';
1213
import { TechnologyTags } from '@/components/TechnologyTags';
1314
import { Avatar } from '@/components/ui/Avatar';
1415

1516
export default function PostDetailClient() {
1617
const { canEditPost, canDeleteComment } = useAuthorization();
17-
const { sessionInfo, isAuthenticated } = useAppStore();
18+
const { isAuthenticated } = appAuth();
19+
const { sessionInfo } = useAppStore();
1820
const [post, setPost] = useState<any>(null);
1921
const [comments, setComments] = useState<any[]>([]);
2022
const [loading, setLoading] = useState(true);
@@ -48,7 +50,7 @@ export default function PostDetailClient() {
4850
setComments(response.comments || []);
4951

5052
// Load user's comment votes if authenticated
51-
if (isAuthenticated()) {
53+
if (isAuthenticated) {
5254
try {
5355
const votes = await gateway.getUserPostCommentVotes(postId);
5456
setUpVotedCommentIds(votes.upVotedCommentIds || []);
@@ -71,7 +73,7 @@ export default function PostDetailClient() {
7173
const handleCommentVote = async (commentId: number, weight: number, e: React.MouseEvent) => {
7274
e.stopPropagation();
7375

74-
if (!isAuthenticated()) {
76+
if (!isAuthenticated) {
7577
return;
7678
}
7779

@@ -120,7 +122,7 @@ export default function PostDetailClient() {
120122
const handleSubmitComment = async (e: React.FormEvent) => {
121123
e.preventDefault();
122124

123-
if (!isAuthenticated() || !newComment.trim()) {
125+
if (!isAuthenticated || !newComment.trim()) {
124126
return;
125127
}
126128

@@ -227,7 +229,7 @@ export default function PostDetailClient() {
227229
: 'hover:text-green-600'
228230
}`}
229231
title={isUpVoted ? 'Remove upvote' : 'Upvote'}
230-
disabled={!isAuthenticated()}
232+
disabled={!isAuthenticated}
231233
>
232234
233235
</button>
@@ -240,7 +242,7 @@ export default function PostDetailClient() {
240242
: 'hover:text-red-600'
241243
}`}
242244
title={isDownVoted ? 'Remove downvote' : 'Downvote'}
243-
disabled={!isAuthenticated()}
245+
disabled={!isAuthenticated}
244246
>
245247
246248
</button>
@@ -296,7 +298,7 @@ export default function PostDetailClient() {
296298
dangerouslySetInnerHTML={{ __html: comment.contentHtml || comment.content }}
297299
/>
298300
<div className="flex gap-3 text-sm text-gray-600">
299-
{isAuthenticated() && (
301+
{isAuthenticated && (
300302
<button
301303
onClick={() => setReplyToId(comment.id)}
302304
className="hover:text-blue-600"
@@ -327,7 +329,7 @@ export default function PostDetailClient() {
327329
</div>
328330

329331
{/* Reply Form */}
330-
{isReplyingTo && isAuthenticated() && (
332+
{isReplyingTo && isAuthenticated && (
331333
<div className="ml-12 mt-3">
332334
<form onSubmit={handleSubmitComment}>
333335
<div className="mb-2">
@@ -460,7 +462,7 @@ export default function PostDetailClient() {
460462
</h2>
461463

462464
{/* Add Top-Level Comment Form */}
463-
{isAuthenticated() && !replyToId && (
465+
{isAuthenticated && !replyToId && (
464466
<form onSubmit={handleSubmitComment} className="mb-6">
465467
<div className="mb-2">
466468
<textarea

TechStacks.Client/src/components/posts/PostsList.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useRouter } from 'next/navigation';
66
import { formatDistanceToNow } from 'date-fns';
77
import routes from '@/lib/utils/routes';
88
import * as gateway from '@/lib/api/gateway';
9-
import { useAppStore } from '@/lib/stores/useAppStore';
9+
import { appAuth } from '@/lib/auth';
1010
import { TechnologyTags } from '@/components/TechnologyTags';
1111
import { Post } from '@/shared/dtos';
1212

@@ -17,14 +17,14 @@ interface PostsListProps {
1717

1818
export function PostsList({ posts }: PostsListProps) {
1919
const router = useRouter();
20-
const { isAuthenticated } = useAppStore();
20+
const { isAuthenticated } = appAuth();
2121
const [upVotedPostIds, setUpVotedPostIds] = useState<number[]>([]);
2222
const [downVotedPostIds, setDownVotedPostIds] = useState<number[]>([]);
2323
const [localPoints, setLocalPoints] = useState<Record<number, number>>({});
2424

2525
useEffect(() => {
2626
const loadUserActivity = async () => {
27-
if (!isAuthenticated()) return;
27+
if (!isAuthenticated) return;
2828

2929
try {
3030
const activity = await gateway.getUserPostActivity();
@@ -41,7 +41,7 @@ export function PostsList({ posts }: PostsListProps) {
4141
const handleVote = async (postId: number, weight: number, e: React.MouseEvent) => {
4242
e.stopPropagation(); // Prevent card click when voting
4343

44-
if (!isAuthenticated()) {
44+
if (!isAuthenticated) {
4545
// Optionally redirect to login
4646
return;
4747
}

TechStacks.Client/src/lib/hooks/useAuthorization.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use client';
22

33
import { useAppStore } from '@/lib/stores/useAppStore';
4+
import { appAuth } from '../auth';
45

56
export function useAuthorization() {
6-
const { sessionInfo, isAdmin } = useAppStore();
7+
const { isAdmin } = appAuth();
8+
const { sessionInfo } = useAppStore();
79

810
const canEditTechnology = (tech: any) => {
911
if (!sessionInfo) return false;

TechStacks.Client/src/lib/hooks/useRequireAuth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import { useEffect } from 'react';
44
import { useRouter } from 'next/navigation';
5-
import { useAppStore } from '@/lib/stores/useAppStore';
5+
import { appAuth } from '@/lib/auth';
66

77
export function useRequireAuth(redirectTo?: string) {
88
const router = useRouter();
9-
const isAuthenticated = useAppStore((state) => state.isAuthenticated());
10-
9+
const { isAuthenticated } = appAuth();
10+
1111
useEffect(() => {
1212
if (!isAuthenticated && redirectTo) {
1313
// Only redirect if explicitly specified to avoid infinite loops

TechStacks.Client/src/lib/stores/useAppStore.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ interface AppState {
1010
// Session & Auth
1111
sessionInfo: any | null;
1212
setSessionInfo: (session: any | null) => void;
13-
isAuthenticated: () => boolean;
14-
isAdmin: () => boolean;
1513

1614
// Config & Overview
1715
config: any;
@@ -72,13 +70,6 @@ export const useAppStore = create<AppState>()(
7270
setConfig: (config) => set({ config }),
7371
setOverview: (overview) => set({ overview }),
7472

75-
// Computed
76-
isAuthenticated: () => get().sessionInfo !== null,
77-
isAdmin: () => {
78-
const roles = get().sessionInfo?.roles || [];
79-
return roles.includes('Admin');
80-
},
81-
8273
// Initialize app
8374
initialize: async () => {
8475
set({ loading: true });

0 commit comments

Comments
 (0)