fix(api): cookies not set on http connections
Frontend uses .env URL.
This commit is contained in:
@@ -32,6 +32,7 @@ services:
|
||||
container_name: vod_frontend
|
||||
environment:
|
||||
VITE_API_URL: ${BACKEND_URL}
|
||||
VITE_FRONTEND_URL: ${FRONTEND_URL}
|
||||
VITE_GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
|
||||
depends_on:
|
||||
- backend
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { defineConfig, loadEnv } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
import tailwindcss from '@tailwindcss/vite'
|
||||
import flowbiteReact from "flowbite-react/plugin/vite";
|
||||
|
||||
export default defineConfig(({ mode }) => { // change to function form
|
||||
const env = loadEnv(mode, process.cwd(), '') // load all env vars
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
return {
|
||||
plugins: [react(), tailwindcss(), flowbiteReact()],
|
||||
preview: {
|
||||
port: 5173,
|
||||
@@ -15,7 +16,8 @@ export default defineConfig({
|
||||
port: 5173,
|
||||
strictPort: true,
|
||||
host: true,
|
||||
origin: "http://0.0.0.0:5173",
|
||||
origin: env.VITE_FRONTEND_URL || "http://0.0.0.0:5173", // use it here
|
||||
allowedHosts: true,
|
||||
proxy: {
|
||||
'/api/v1': {
|
||||
target: 'http://localhost:8080',
|
||||
@@ -24,4 +26,5 @@ export default defineConfig({
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -6,6 +6,7 @@ import com.ddf.vodsystem.entities.User;
|
||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||
import com.ddf.vodsystem.services.UserService;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -41,14 +42,15 @@ public class UserController {
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<APIResponse<Token>> login(@RequestBody Token token,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
String jwt = userService.login(token.token());
|
||||
|
||||
ResponseCookie cookie = ResponseCookie.from("token", jwt)
|
||||
.httpOnly(true)
|
||||
.maxAge(jwtExpiration / 1000)
|
||||
.sameSite("None")
|
||||
.secure(true)
|
||||
.sameSite(request.isSecure() ? "None" : "Lax")
|
||||
.secure(request.isSecure())
|
||||
.path("/")
|
||||
.build();
|
||||
|
||||
@@ -60,12 +62,13 @@ public class UserController {
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public ResponseEntity<APIResponse<Void>> logout(HttpServletResponse response) {
|
||||
public ResponseEntity<APIResponse<Void>> logout(HttpServletResponse response,
|
||||
HttpServletRequest request) {
|
||||
ResponseCookie cookie = ResponseCookie.from("token", "")
|
||||
.httpOnly(true)
|
||||
.maxAge(0)
|
||||
.sameSite("None")
|
||||
.secure(true)
|
||||
.sameSite(request.isSecure() ? "None" : "Lax")
|
||||
.secure(request.isSecure())
|
||||
.path("/")
|
||||
.build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user