From 21ace0f38e5022c03d54e1696db87a06e06a5eb3 Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Thu, 4 Dec 2025 09:45:00 +0800 Subject: [PATCH] Add authentication and community controllers Implemented user registration/login system and community management features. Added controllers for handling posts and community subscriptions. --- app/Http/Controllers/AuthController.php | 66 ++++++++++++++++++++ app/Http/Controllers/CommunityController.php | 63 +++++++++++++++++++ app/Http/Controllers/PostController.php | 55 ++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 app/Http/Controllers/AuthController.php create mode 100644 app/Http/Controllers/CommunityController.php create mode 100644 app/Http/Controllers/PostController.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php new file mode 100644 index 0000000..5fb1bbe --- /dev/null +++ b/app/Http/Controllers/AuthController.php @@ -0,0 +1,66 @@ +validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|string|email|max:255|unique:users', + 'password' => 'required|string|min:8|confirmed', + ]); + + $user = User::create([ + 'name' => $validated['name'], + 'email' => $validated['email'], + 'password' => Hash::make($validated['password']), + 'karma' => 0, + ]); + + Auth::login($user); + + return redirect('/')->with('success', 'Registration successful!'); + } + + public function showLogin() + { + return view('auth.login'); + } + + public function login(Request $request) + { + $credentials = $request->validate([ + 'email' => 'required|email', + 'password' => 'required', + ]); + + if (Auth::attempt($credentials, $request->boolean('remember'))) { + $request->session()->regenerate(); + return redirect()->intended('/'); + } + + return back()->withErrors([ + 'email' => 'The provided credentials do not match our records.', + ])->onlyInput('email'); + } + + public function logout(Request $request) + { + Auth::logout(); + $request->session()->invalidate(); + $request->session()->regenerateToken(); + return redirect('/'); + } +} diff --git a/app/Http/Controllers/CommunityController.php b/app/Http/Controllers/CommunityController.php new file mode 100644 index 0000000..fa641f1 --- /dev/null +++ b/app/Http/Controllers/CommunityController.php @@ -0,0 +1,63 @@ +orderBy('created_at', 'desc') + ->paginate(20); + + return view('communities.index', compact('communities')); + } + + public function show(Community $community) + { + $posts = $community->posts() + ->with(['user', 'comments']) + ->orderBy('votes', 'desc') + ->orderBy('created_at', 'desc') + ->paginate(20); + + return view('communities.show', compact('community', 'posts')); + } + + public function create() + { + return view('communities.create'); + } + + public function store(Request $request) + { + $validated = $request->validate([ + 'name' => 'required|string|max:255|unique:communities', + 'description' => 'nullable|string', + ]); + + $community = Community::create([ + 'name' => $validated['name'], + 'description' => $validated['description'] ?? null, + 'created_by' => auth()->id(), + ]); + + return redirect()->route('communities.show', $community) + ->with('success', 'Community created successfully!'); + } + + public function subscribe(Community $community) + { + auth()->user()->subscribedCommunities()->attach($community->id); + return back()->with('success', 'Subscribed to ' . $community->name); + } + + public function unsubscribe(Community $community) + { + auth()->user()->subscribedCommunities()->detach($community->id); + return back()->with('success', 'Unsubscribed from ' . $community->name); + } +} diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php new file mode 100644 index 0000000..abfdec1 --- /dev/null +++ b/app/Http/Controllers/PostController.php @@ -0,0 +1,55 @@ +get(); + return view('posts.create', compact('communities')); + } + + public function store(Request $request) + { + $validated = $request->validate([ + 'community_id' => 'required|exists:communities,id', + 'title' => 'required|string|max:255', + 'content' => 'nullable|string', + 'url' => 'nullable|url', + 'type' => 'required|in:text,link,image', + ]); + + $post = Post::create([ + 'community_id' => $validated['community_id'], + 'user_id' => auth()->id(), + 'title' => $validated['title'], + 'content' => $validated['content'] ?? null, + 'url' => $validated['url'] ?? null, + 'type' => $validated['type'], + ]); + + return redirect()->route('posts.show', $post) + ->with('success', 'Post created successfully!'); + } + + public function show(Post $post) + { + $post->load(['user', 'community', 'comments.user', 'comments.replies.user']); + return view('posts.show', compact('post')); + } + + public function destroy(Post $post) + { + if ($post->user_id !== auth()->id()) { + abort(403); + } + + $post->delete(); + return redirect('/')->with('success', 'Post deleted successfully!'); + } +}