Skip to content

Commit ec66314

Browse files
committed
wip
1 parent 4f15d7d commit ec66314

File tree

7 files changed

+138
-3
lines changed

7 files changed

+138
-3
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace CommunityWithLegends\Http\Controllers;
4+
5+
use CommunityWithLegends\Http\Requests\LoginRequest;
6+
use CommunityWithLegends\Http\Requests\RegisterRequest;
7+
use CommunityWithLegends\Models\User;
8+
use Illuminate\Http\RedirectResponse;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Auth;
11+
use Illuminate\Support\Facades\Hash;
12+
use Symfony\Component\HttpFoundation\Response as Status;
13+
14+
class AuthController extends Controller
15+
{
16+
public function login(LoginRequest $loginRequest): \Illuminate\Http\JsonResponse
17+
{
18+
if (Auth::attempt($loginRequest->validated())) {
19+
$loginRequest->session()->regenerate();
20+
21+
return response()->json([
22+
"message" => "success",
23+
])->setStatusCode(Status::HTTP_OK);
24+
}
25+
26+
return response()->json([
27+
"message" => "The provided credentials do not match our records.",
28+
])->setStatusCode(Status::HTTP_FORBIDDEN);
29+
}
30+
public function logout(Request $request): \Illuminate\Http\JsonResponse
31+
{
32+
if (Auth::user()) {
33+
Auth::logout();
34+
35+
$request->session()->invalidate();
36+
37+
$request->session()->regenerateToken();
38+
39+
return response()->json([
40+
"message" => "success",
41+
])->setStatusCode(Status::HTTP_OK);
42+
}
43+
44+
return response()->json([
45+
"message" => "You are not logged in.",
46+
])->setStatusCode(Status::HTTP_UNAUTHORIZED);
47+
}
48+
public function register(RegisterRequest $registerRequest)
49+
{
50+
$validated = $registerRequest->validated();
51+
$userExist = User::query()->where('email', $validated['email'])->exists();
52+
53+
if(!$userExist){
54+
$user = new User($validated);
55+
$user->password = Hash::make($validated['password']);
56+
$user->save();
57+
58+
Auth::login($user);
59+
}
60+
61+
return response()->json([
62+
"message" => "success",
63+
])->setStatusCode(Status::HTTP_OK);
64+
}
65+
66+
}

app/Http/Requests/LoginRequest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CommunityWithLegends\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class LoginRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'email' => ['required', 'email', 'max:225'],
26+
'password' => ['required', 'min:8', 'max:225', 'string'],
27+
];
28+
}
29+
}

app/Http/Requests/RegisterRequest.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CommunityWithLegends\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class RegisterRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
"name" => ['required', 'max:225'],
26+
"email" => ['required', 'email:rfc,dns', 'max:225', 'string'],
27+
"password" => ['required', 'min:8', 'max:225', 'string'],
28+
];
29+
}
30+
}

bootstrap/app.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Application;
66
use Illuminate\Foundation\Configuration\Exceptions;
77
use Illuminate\Foundation\Configuration\Middleware;
8+
use Illuminate\Session\Middleware\StartSession;
89

910
return Application::configure(basePath: dirname(__DIR__))
1011
->withRouting(
@@ -15,6 +16,7 @@
1516
)
1617
->withMiddleware(function (Middleware $middleware): void {
1718
$middleware->statefulApi();
19+
$middleware->append(StartSession::class);
1820
})
1921
->withExceptions(function (Exceptions $exceptions): void {
2022
})->create();

config/cors.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030

3131
"max_age" => 0,
3232

33-
"supports_credentials" => false,
33+
"supports_credentials" => true,
3434
];

routes/api.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use CommunityWithLegends\Http\Controllers\AuthController;
56
use CommunityWithLegends\Models\User;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Hash;
@@ -26,4 +27,11 @@
2627
return $user->createToken($request->device_name)->plainTextToken;
2728
});
2829

29-
Route::get("/user", fn(Request $request) => $request->user())->middleware("auth:sanctum");
30+
Route::middleware('auth:sanctum')->group(function () {
31+
Route::get("/user", fn(Request $request) => $request->user());
32+
33+
Route::post("/auth/logout", [AuthController::class, 'logout']);
34+
});
35+
36+
Route::post("/auth/login", [AuthController::class, 'login'])->name('login');
37+
Route::post("/auth/register", [AuthController::class, 'register']);

routes/web.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
Route::get("/", fn(): JsonResponse => response()->json([
99
"message" => "Welcome",
10-
]));
10+
]))->name('dashboard');

0 commit comments

Comments
 (0)