Add authentication and community controllers

Implemented user registration/login system and community management features.
Added controllers for handling posts and community subscriptions.
This commit is contained in:
m1ngsama 2025-12-04 09:45:00 +08:00
parent b453c1020a
commit 21ace0f38e
3 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,66 @@
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
{
public function showRegister()
{
return view('auth.register');
}
public function register(Request $request)
{
$validated = $request->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('/');
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace App\Http\Controllers;
use App\Models\Community;
use Illuminate\Http\Request;
class CommunityController extends Controller
{
public function index()
{
$communities = Community::withCount(['posts', 'subscribers'])
->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);
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers;
use App\Models\Community;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function create()
{
$communities = Community::orderBy('name')->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!');
}
}