NCORE_Smart_Firewall/app/Http/Controllers/AuthController.php

38 lines
1.1 KiB
PHP
Raw Normal View History

2024-12-19 08:02:32 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
public function authenticate(Request $request)
{
try {
$credits = $request->validate([
"email" => "required|email",
"password" => "required"
2024-12-19 08:02:32 +00:00
]);
if (Auth::attempt(["email" => $credits["email"], "password" => $credits["password"]])) {
$user = Auth::user();
2024-12-19 08:02:32 +00:00
return response()->json([
"account" => $user->name,
"redirect" => "3000",
"message" => "Login successful. Redirecting in 3 seconds..."
], 200);
2024-12-19 08:02:32 +00:00
}
return response()->json(["error" => "Invalid email or password."], 401);
} catch (\Illuminate\Validation\ValidationException $exception) {
return response()->json(['error' => $exception->errors()], 422);
} catch (\Exception $exception) {
return response()->json(['error' => 'An unexpected error occurred.'], 500);
}
}
}