34 lines
1.0 KiB
PHP
34 lines
1.0 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], 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);
|
|
}
|
|
|
|
}
|
|
}
|