-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAdminUserInitializer.java
More file actions
67 lines (55 loc) · 2.52 KB
/
AdminUserInitializer.java
File metadata and controls
67 lines (55 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.danielagapov.spawn.Config;
import com.danielagapov.spawn.Exceptions.Logger.ILogger;
import com.danielagapov.spawn.Models.User.User;
import com.danielagapov.spawn.Repositories.User.IUserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Date;
import java.util.UUID;
/**
* This class is responsible for initializing the admin user when the application starts.
* It creates an admin user if one doesn't already exist.
*/
@Configuration
@Profile("!test") // Exclude from test profile
public class AdminUserInitializer {
@Value("${ADMIN_USERNAME:admin}")
private String adminUsername;
@Value("${ADMIN_PASSWORD:spawn-admin-secure-password}")
private String adminPassword;
@Bean
public CommandLineRunner initializeAdminUser(
IUserRepository userRepository,
PasswordEncoder passwordEncoder,
ILogger logger) {
return args -> {
// Check if admin user already exists
if (!userRepository.existsByUsername(adminUsername)) {
try {
logger.info("Creating admin user");
// Create a new admin user
User adminUser = new User();
adminUser.setId(UUID.randomUUID());
adminUser.setUsername(adminUsername);
adminUser.setName("Admin User");
adminUser.setEmail("admin@getspawn.com");
adminUser.setBio("Spawn Admin Account");
// Encode the password from environment variable
adminUser.setPassword(passwordEncoder.encode(adminPassword));
// Set as verified and created now
adminUser.setVerified(true);
adminUser.setDateCreated(new Date());
// Save to database
userRepository.save(adminUser);
logger.info("Admin user created successfully");
} catch (Exception e) {
logger.error("Failed to create admin user: " + e.getMessage());
}
}
};
}
}