38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?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"
|
|
]);
|
|
|
|
if (Auth::attempt(["email" => $credits["email"], "password" => $credits["password"]])) {
|
|
$user = Auth::user();
|
|
|
|
return response()->json([
|
|
"account" => $user->name,
|
|
"redirect" => "3000",
|
|
"message" => "Login successful. Redirecting in 3 seconds..."
|
|
], 200);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|
|
}
|