Changeset 140d098


Ignore:
Timestamp:
02/09/26 20:20:12 (5 months ago)
Author:
Andrej <asumanovski@…>
Branches:
master
Children:
3ebe47c
Parents:
793ce2c
Message:

Add auth

Files:
17 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • backend/gitignore

    r793ce2c r140d098  
    3232### VS Code ###
    3333.vscode/
     34
     35### Environment Variables ###
     36.env
     37.env.local
     38.env.*.local
  • backend/pom.xml

    r793ce2c r140d098  
    3232    <dependencies>
    3333        <dependency>
     34            <groupId>io.github.cdimascio</groupId>
     35            <artifactId>dotenv-java</artifactId>
     36            <version>3.0.0</version>
     37        </dependency>
     38        <dependency>
    3439            <groupId>org.springframework.boot</groupId>
    3540            <artifactId>spring-boot-starter-data-jpa</artifactId>
     
    4752            <artifactId>spring-boot-starter-webmvc</artifactId>
    4853        </dependency>
    49 
     54        <dependency>
     55            <groupId>io.jsonwebtoken</groupId>
     56            <artifactId>jjwt-api</artifactId>
     57            <version>0.12.6</version>
     58        </dependency>
     59        <dependency>
     60            <groupId>io.jsonwebtoken</groupId>
     61            <artifactId>jjwt-impl</artifactId>
     62            <version>0.12.6</version>
     63            <scope>runtime</scope>
     64        </dependency>
     65        <dependency>
     66            <groupId>io.jsonwebtoken</groupId>
     67            <artifactId>jjwt-jackson</artifactId>
     68            <version>0.12.6</version>
     69            <scope>runtime</scope>
     70        </dependency>
    5071        <dependency>
    5172            <groupId>org.postgresql</groupId>
  • backend/src/main/java/com/trekr/backend/BackendApplication.java

    r793ce2c r140d098  
    11package com.trekr.backend;
    22
     3import io.github.cdimascio.dotenv.Dotenv;
    34import org.springframework.boot.SpringApplication;
    45import org.springframework.boot.autoconfigure.SpringBootApplication;
     
    89
    910    public static void main(String[] args) {
    10         SpringApplication.run(BackendApplication.class, args);
     11        // Load .env file BEFORE Spring Boot starts
     12        // This ensures system properties are available for ${} placeholder resolution
     13        Dotenv dotenv = loadEnvironmentVariables();
     14       
     15        // Create SpringApplication instance
     16        SpringApplication app = new SpringApplication(BackendApplication.class);
     17       
     18        // Set system properties and default properties from .env file
     19        if (dotenv != null) {
     20            java.util.Map<String, Object> defaultProps = new java.util.HashMap<>();
     21           
     22            dotenv.entries().forEach(entry -> {
     23                String key = entry.getKey();
     24                String value = entry.getValue();
     25                if (value != null && !value.trim().isEmpty()) {
     26                    // Set as system property (for ${} placeholders in application.properties)
     27                    System.setProperty(key, value);
     28                    // Collect for default properties
     29                    defaultProps.put(key, value);
     30                }
     31            });
     32           
     33            // Set all default properties at once
     34            if (!defaultProps.isEmpty()) {
     35                app.setDefaultProperties(defaultProps);
     36            }
     37        }
     38       
     39        app.run(args);
     40    }
     41   
     42    private static Dotenv loadEnvironmentVariables() {
     43        Dotenv dotenv = null;
     44        try {
     45            // Try current directory (backend/)
     46            dotenv = Dotenv.configure()
     47                    .directory("./")
     48                    .ignoreIfMissing()
     49                    .load();
     50        } catch (Exception e) {
     51            // Try parent directory if current doesn't work
     52            try {
     53                dotenv = Dotenv.configure()
     54                        .directory("../")
     55                        .ignoreIfMissing()
     56                        .load();
     57            } catch (Exception e2) {
     58                System.err.println("WARNING: Could not load .env file. Make sure .env exists in backend/ directory.");
     59                System.err.println("Error: " + e2.getMessage());
     60                return null;
     61            }
     62        }
     63       
     64        // Verify critical properties are loaded
     65        if (dotenv != null) {
     66            String url = dotenv.get("SPRING_DATASOURCE_URL");
     67            String username = dotenv.get("SPRING_DATASOURCE_USERNAME");
     68            String password = dotenv.get("SPRING_DATASOURCE_PASSWORD");
     69           
     70            if (url != null && !url.trim().isEmpty()) {
     71                System.out.println("✓ Database URL loaded from .env");
     72            } else {
     73                System.err.println("✗ ERROR: SPRING_DATASOURCE_URL not found in .env file!");
     74            }
     75            if (username != null && !username.trim().isEmpty()) {
     76                System.out.println("✓ Database username loaded from .env");
     77            } else {
     78                System.err.println("✗ ERROR: SPRING_DATASOURCE_USERNAME not found in .env file!");
     79            }
     80            if (password != null && !password.trim().isEmpty()) {
     81                System.out.println("✓ Database password loaded from .env");
     82            } else {
     83                System.err.println("✗ ERROR: SPRING_DATASOURCE_PASSWORD not found in .env file!");
     84            }
     85        }
     86       
     87        return dotenv;
    1188    }
    1289
Note: See TracChangeset for help on using the changeset viewer.