Skip to content

Commit 50e2bc6

Browse files
committed
Only logged in users can create topics
1 parent df6f27b commit 50e2bc6

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

app/controllers/topics_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class TopicsController < ApplicationController
2+
before_action :verify_user, except: [ :index, :show ]
3+
24
def index
35
topics = Topic.all
46
.includes(:user, :category, messages: :user)
@@ -45,4 +47,8 @@ def create
4547
def topic_params
4648
params.require(:topic).permit(:title, :category_id, messages_attributes: [ :body ])
4749
end
50+
51+
def verify_user
52+
redirect_to topics_path unless current_user
53+
end
4854
end

app/frontend/pages/categories/show.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import { Link } from "@inertiajs/react";
22
import TopicsTable from "../../components/TopicsTable"
33
import AppLayout from "../../layouts/AppLayout"
4-
import { Category } from "../../types"
4+
import { Category, User } from "../../types"
55

6-
function CategoriesShow({ category }: { category: Category }) {
6+
function CategoriesShow({ category, current_user: currentUser }: { category: Category, current_user: User }) {
77
const { topics } = category;
88

99
return (
1010
<AppLayout>
1111
<div className="flex items-center justify-between mb-6">
1212
<h1 className="text-2xl font-bold text-gray-900">Category: {category.name}</h1>
13-
<Link
13+
{currentUser && (
14+
<Link
1415
href={'/topics/new'}
1516
data={{topic: { category_id: category.id }}}
1617
className="bg-sky-600 hover:bg-sky-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors shadow inline-block text-center"
1718
>
18-
New Topic
19-
</Link>
19+
New Topic
20+
</Link>
21+
)}
2022
</div>
2123

2224
<div className="max-w-7xl mx-auto">

app/frontend/pages/my_topics/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { Head, Link } from '@inertiajs/react'
22
import AppLayout from '../../layouts/AppLayout'
3-
import { Topic } from '../../types'
3+
import { Topic, User } from '../../types'
44
import TopicsTable from '../../components/TopicsTable'
55

6-
function MyTopicsIndex({ topics }: { topics: Topic[] }) {
6+
function MyTopicsIndex({ topics, current_user: currentUser }: { topics: Topic[], current_user: User }) {
77
return (
88
<AppLayout>
99
<Head title="My Topics - Pups & Pourovers" />
1010

1111
<div className="flex items-center justify-between mb-6">
1212
<h1 className="text-2xl font-bold text-gray-900">My Topics</h1>
13-
<Link
13+
{currentUser && (
14+
<Link
1415
href="/topics/new"
1516
className="bg-sky-600 hover:bg-sky-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors shadow inline-block text-center"
1617
>
17-
New Topic
18-
</Link>
18+
New Topic
19+
</Link>
20+
)}
1921
</div>
2022

2123
<div className="max-w-7xl mx-auto">

app/frontend/pages/topics/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { Head, Link } from '@inertiajs/react'
22
import AppLayout from '../../layouts/AppLayout'
3-
import { Topic } from '../../types'
3+
import { Topic, User } from '../../types'
44
import TopicsTable from '../../components/TopicsTable'
55

6-
function TopicsIndex({ topics }: { topics: Topic[] }) {
6+
function TopicsIndex({ topics, current_user: currentUser }: { topics: Topic[], current_user: User }) {
77
return (
88
<AppLayout>
99
<Head title="Topics - Pups & Pourovers" />
1010

1111
<div className="max-w-7xl mx-auto">
1212
<div className="flex justify-end mb-6">
13-
<Link
13+
{currentUser && (
14+
<Link
1415
href="/topics/new"
1516
className="bg-sky-600 hover:bg-sky-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors shadow inline-block text-center"
1617
>
17-
New Topic
18-
</Link>
18+
New Topic
19+
</Link>
20+
)}
1921
</div>
2022
<div className="bg-white shadow-sm rounded-lg overflow-hidden">
2123
<TopicsTable topics={topics} />

0 commit comments

Comments
 (0)