From f7d274c3f3a98b4dfec17727a681085e145d576d Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 27 Oct 2024 13:25:09 -0700 Subject: [PATCH] Update --- .env.local.backup | 12 ++++++ scripts/{smtp_config.sh => mail_config.sh} | 43 +++++++++++++++++++ .../modules/usermanagement/domain/User.java | 2 +- .../main/resources/config/application-dev.yml | 2 +- .../resources/config/application-prod.yml | 6 +-- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 .env.local.backup rename scripts/{smtp_config.sh => mail_config.sh} (61%) diff --git a/.env.local.backup b/.env.local.backup new file mode 100644 index 00000000..acb03d50 --- /dev/null +++ b/.env.local.backup @@ -0,0 +1,12 @@ +POSTGRES_PASSWORD='flexwork123' +JWT_BASE64_SECRET='anhsREVnM1hDbTdSQnhHMGpQMW80eHFoZmV5blIxZG9QMVBlT0RIUVZlY3RpcHBqZHlqbGZraGU2NW8zekR6dk9VbkJYckI3VUh0eTJjckhpZnNsVjQ1c1VX' +spring.mail.host=smtp.gmail.com +spring.mail.username=haiphucnguyen@gmail.com +spring.mail.port=587 +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.required=true +spring.mail.properties.mail.smtp.port=587 +spring.mail.password=fhbq nfoj baku hszc +spring.mail.properties.mail.smtp.auth=true +flexwork.mail.from=noreply@flexwork.io +flexwork.mail.base-url=http://localhost:3000 diff --git a/scripts/smtp_config.sh b/scripts/mail_config.sh similarity index 61% rename from scripts/smtp_config.sh rename to scripts/mail_config.sh index 2280c648..a84f0eba 100755 --- a/scripts/smtp_config.sh +++ b/scripts/mail_config.sh @@ -23,6 +23,45 @@ read -p "Enter your password: " smtp_password # Ask if SMTP requires STARTTLS read -p "Does SMTP require STARTTLS (y/n)? " requires_starttls +# Prompt the user to enter the email address that will be used to send emails +email_regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" + +# Loop until a valid email is entered +while true; do + read -p "Please enter the email address that will be used as the sender for outgoing emails: " sender_email + + # Validate email format + if [[ $sender_email =~ $email_regex ]]; then + break # Exit the loop if the email is valid + else + echo "Invalid email address. Please enter a valid email." + fi +done + + +# Prompt the user to enter the base url for email template + +# Regular expression for validating a URL with optional port, allowing localhost and IPs +url_regex="^(https?://)(localhost|([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]{1,5})?(/.*)?$" + +# Loop until a valid URL is entered +while true; do + read -p "Please enter the base URL that will be used for the email template: " base_url_email + + # Validate URL format + if [[ $base_url_email =~ $url_regex ]]; then + break # Exit the loop if the URL is valid + else + echo "Invalid URL. Please enter a valid URL." + echo "Examples of valid URLs:" + echo "- https://example.com" + echo "- http://localhost:3000" + echo "- http://192.168.1.1:8080/path" + fi +done + +# Continue with the rest of the script here + # Define the output script that will store the sensitive data output_script=".env.local" @@ -50,6 +89,8 @@ add_or_update_env_var() { fi } + + # Add or update entries add_or_update_env_var "spring.mail.host" "$smtp_host" add_or_update_env_var "spring.mail.port" "$smtp_port" @@ -57,6 +98,8 @@ add_or_update_env_var "spring.mail.properties.mail.smtp.port" "$smtp_port" add_or_update_env_var "spring.mail.username" "$smtp_username" add_or_update_env_var "spring.mail.password" "$smtp_password" add_or_update_env_var "spring.mail.properties.mail.smtp.auth" "true" +add_or_update_env_var "flexwork.mail.from" $sender_email +add_or_update_env_var "flexwork.mail.base-url" $base_url_email # Add STARTTLS settings if required if [[ "$requires_starttls" == "y" ]]; then diff --git a/server/src/main/java/io/flexwork/modules/usermanagement/domain/User.java b/server/src/main/java/io/flexwork/modules/usermanagement/domain/User.java index 1b581d3e..cc17aff2 100644 --- a/server/src/main/java/io/flexwork/modules/usermanagement/domain/User.java +++ b/server/src/main/java/io/flexwork/modules/usermanagement/domain/User.java @@ -113,7 +113,7 @@ public class User extends AbstractAuditingEntity implements Serializable { public LocalDateTime getLastLoginTime() { if (lastLoginTime == null) return null; - ZoneId userZone = ZoneId.of(timezone); + ZoneId userZone = (timezone != null) ? ZoneId.of(timezone) : ZoneId.of("America/Los_Angeles"); return lastLoginTime.atZone(ZoneOffset.UTC).withZoneSameInstant(userZone).toLocalDateTime(); } } diff --git a/server/src/main/resources/config/application-dev.yml b/server/src/main/resources/config/application-dev.yml index 8e754783..a02290a6 100644 --- a/server/src/main/resources/config/application-dev.yml +++ b/server/src/main/resources/config/application-dev.yml @@ -40,7 +40,7 @@ server: flexwork: mail: from: flexwork-app@localhost.com - base-url: http://127.0.0.1:8080 + base-url: http://127.0.0.1:3000 # CORS is only enabled by default with the "dev" profile cors: # Allow Ionic for JHipster by default (* no longer allowed in Spring Boot 2.4+) diff --git a/server/src/main/resources/config/application-prod.yml b/server/src/main/resources/config/application-prod.yml index 500032e2..ff2239e0 100644 --- a/server/src/main/resources/config/application-prod.yml +++ b/server/src/main/resources/config/application-prod.yml @@ -75,8 +75,8 @@ server: flexwork: mail: - from: - base-url: + from: + base-url: security: authentication: jwt: @@ -84,7 +84,7 @@ flexwork: # As this is the PRODUCTION configuration, you MUST change the default key, and store it securely: # - In the Consul configserver # - In a separate `application-prod.yml` file, in the same folder as your executable JAR file - base64-secret: + base64-secret: # Token is valid 24 hours token-validity-in-seconds: 86400 token-validity-in-seconds-for-remember-me: 2592000