Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
fix: Redirect to login when trying to add/remove from cart (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore authored Apr 13, 2024
1 parent a9fd401 commit fa25afa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions app/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function add(Request $request): JsonResponse {
"amount" => ["required", "integer", "gt:0"]
]);

if(!Auth::check())
return response()->json(["redirect" => $request->has("origin") ? "/login?redirect=" . urlencode($request->origin) : "/login"], 302);

$result = Auth::user()->addCartItem(Product::find($args["product"]), $args["amount"]);

return response()->json($result->toJson());
Expand All @@ -40,6 +43,9 @@ public function remove(Request $request): JsonResponse {
"amount" => ["required", "integer", "gt:0"]
]);

if(!Auth::check())
return response()->json(["redirect" => $request->has("origin") ? "/login?redirect=" . urlencode($request->origin) : "/login"], 302);

$result = Auth::user()->removeCartItem(Product::find($args["product"]), $args["amount"]);

return response()->json($result->toJson());
Expand Down
1 change: 1 addition & 0 deletions public/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function sendJSON(url, data, method = "POST") {

xhr.onerror = e => reject(xhr.statusText)

data.origin = window.location.href
xhr.send(JSON.stringify(data))
})
}
Expand Down
4 changes: 2 additions & 2 deletions resources/views/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<li class="error">{{ $message }}</li>
@enderror
<label for="email">Email: </label>
<input type="email" name="email" required class="@error('password') invalid @enderror"/>
@error("password")
<input type="email" name="email" required class="@error('email') invalid @enderror"/>
@error("email")
<li class="error">{{ $message }}</li>
@enderror
<label for="first_name">Prenom: </label>
Expand Down
7 changes: 3 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"page_to_load" => "admin_users",
"title" => "Gestion utilisateurs"
]);})->middleware("admin");


Route::post("/auth/login", [AuthController::class, "login"]);
Route::get("/auth/logout", [AuthController::class, "logout"]);
Expand All @@ -110,12 +110,11 @@

Route::post("/auth/reset-password", [AuthController::class, "resetPassword"]);

Route::post("/cart/add", [CartController::class, "add"])->middleware("auth");
Route::post("/cart/remove", [CartController::class, "remove"])->middleware("auth");
Route::post("/cart/add", [CartController::class, "add"]);
Route::post("/cart/remove", [CartController::class, "remove"]);
Route::post("/cart/delete", [CartController::class, "delete"])->middleware("auth");
Route::post("/cart/clear", [CartController::class, "clear"])->middleware("auth");
Route::post("/cart/buy", [CartController::class, "buy"])->middleware(["auth", "verified"]);
Route::post("/cart/buy", [CartController::class, "buy"])->middleware("auth");
Route::get("/cart/get", [CartController::class, "getCart"])->middleware("auth");

Route::get("/catalog/products", [CatalogController::class, "getProducts"]);
Expand Down

0 comments on commit fa25afa

Please sign in to comment.