Skip to content

Commit

Permalink
updated the Cookie configuration to dynamically use the domain name a…
Browse files Browse the repository at this point in the history
…nd secure attributes based on the application environment
  • Loading branch information
rajumb0232 committed Nov 8, 2024
1 parent 9c1abe7 commit 4aad34c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions E-Stores-API/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// use this template to create the .env file.
DOMAIN=
IS_HTTPS=
MAIL_USERNAME=
MAIL_PASSWORD=
POSTGRES_URL=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class AppEnv {

private String baseURL;
private String domain;
private boolean isHttps;
private Jwt jwt;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package com.devb.estores.util;

import com.devb.estores.config.AppEnv;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class CookieManager {

private final AppEnv appEnv;

public String configure(String name,String value, long maxAge) {
ResponseCookie cookie = ResponseCookie.from(name, value)
.domain("localhost")
.domain(appEnv.getDomain())
.path("/")
.httpOnly(true)
.sameSite("Lax")
.secure(false)
.secure(appEnv.isHttps())
.maxAge(maxAge)
.build();
return cookie.toString();
}

public String invalidate(String name){
ResponseCookie cookie = ResponseCookie.from(name, "")
.domain("localhost")
.domain(appEnv.getDomain())
.path("/")
.httpOnly(true)
.sameSite("Lax")
.secure(false)
.secure(appEnv.isHttps())
.maxAge(0)
.build();
return cookie.toString();
Expand Down
2 changes: 2 additions & 0 deletions E-Stores-API/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ server:
#Application Configuration
app:
base-url: /api/fkv1
domain: ${DOMAIN}
is-https: ${IS_HTTPS}
jwt:
secret: ${JWT_SECRET}
access-expiration-seconds: 3600
Expand Down

0 comments on commit 4aad34c

Please sign in to comment.