kybernus 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/AuthController.java.hbs +38 -42
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/ItemController.java.hbs +42 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/PaymentsController.java.hbs +65 -22
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/model/Item.java.hbs +38 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/model/User.java.hbs +41 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/repository/ItemRepository.java.hbs +9 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/repository/UserRepository.java.hbs +13 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/service/AuthService.java.hbs +62 -0
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/service/StripeService.java.hbs +18 -18
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/{{projectNamePascalCase}}Application.java.hbs +2 -0
- package/templates/nestjs/mvc/package.json.hbs +6 -2
- package/templates/nestjs/mvc/prisma/schema.prisma.hbs +31 -0
- package/templates/nestjs/mvc/src/app.module.ts.hbs +3 -1
- package/templates/nestjs/mvc/src/auth/auth.service.ts.hbs +34 -31
- package/templates/nestjs/mvc/src/payments/payments.service.ts.hbs +26 -6
- package/templates/nestjs/mvc/src/prisma/prisma.module.ts.hbs +9 -0
- package/templates/nestjs/mvc/src/prisma/prisma.service.ts.hbs +15 -0
- package/templates/nestjs/mvc/src/services/items.service.ts.hbs +33 -20
- package/templates/nextjs/mvc/package.json.hbs +1 -0
- package/templates/nextjs/mvc/prisma/schema.prisma.hbs +60 -6
- package/templates/nextjs/mvc/src/app/api/webhook/route.ts.hbs +23 -18
- package/templates/nodejs-express/mvc/package.json.hbs +8 -4
- package/templates/nodejs-express/mvc/prisma/schema.prisma.hbs +31 -0
- package/templates/nodejs-express/mvc/src/config/database.ts.hbs +2 -9
- package/templates/nodejs-express/mvc/src/controllers/auth.controller.ts.hbs +40 -58
- package/templates/nodejs-express/mvc/src/controllers/items.controller.ts.hbs +29 -0
- package/templates/nodejs-express/mvc/src/models/README.md.hbs +10 -0
- package/templates/nodejs-express/mvc/src/prisma/client.ts.hbs +3 -0
- package/templates/nodejs-express/mvc/src/services/auth.service.ts.hbs +71 -0
- package/templates/nodejs-express/mvc/src/services/stripe.service.ts.hbs +35 -25
- package/templates/python-fastapi/mvc/app/controllers/auth.py.hbs +25 -16
- package/templates/python-fastapi/mvc/app/controllers/items.py.hbs +9 -7
- package/templates/python-fastapi/mvc/app/controllers/payments.py.hbs +42 -15
- package/templates/python-fastapi/mvc/app/database.py.hbs +17 -0
- package/templates/python-fastapi/mvc/app/main.py.hbs +4 -0
- package/templates/python-fastapi/mvc/app/models/item.py.hbs +11 -8
- package/templates/python-fastapi/mvc/app/models/user.py.hbs +15 -0
- package/templates/python-fastapi/mvc/app/repositories/item_repository.py.hbs +15 -0
- package/templates/python-fastapi/mvc/app/repositories/user_repository.py.hbs +15 -0
- package/templates/python-fastapi/mvc/app/services/item_service.py.hbs +17 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kybernus",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The Ultimate Scaffolding CLI for Modern Developers",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -76,4 +76,4 @@
|
|
|
76
76
|
"overrides": {
|
|
77
77
|
"rimraf": "^6.1.2"
|
|
78
78
|
}
|
|
79
|
-
}
|
|
79
|
+
}
|
package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/AuthController.java.hbs
CHANGED
|
@@ -2,68 +2,64 @@ package {{packageName}}.controller;
|
|
|
2
2
|
|
|
3
3
|
import {{packageName}}.dto.AuthRequest;
|
|
4
4
|
import {{packageName}}.dto.AuthResponse;
|
|
5
|
-
import {{packageName}}.
|
|
5
|
+
import {{packageName}}.model.User;
|
|
6
|
+
import {{packageName}}.service.AuthService;
|
|
6
7
|
import org.springframework.http.ResponseEntity;
|
|
7
|
-
import org.springframework.
|
|
8
|
+
import org.springframework.http.HttpStatus;
|
|
8
9
|
import org.springframework.web.bind.annotation.*;
|
|
10
|
+
import org.springframework.security.core.context.SecurityContextHolder;
|
|
11
|
+
import org.springframework.security.core.Authentication;
|
|
9
12
|
|
|
10
|
-
import java.util.HashMap;
|
|
11
13
|
import java.util.Map;
|
|
12
|
-
import java.util.UUID;
|
|
13
14
|
|
|
14
15
|
@RestController
|
|
15
16
|
@RequestMapping("/api/auth")
|
|
16
17
|
public class AuthController {
|
|
17
18
|
|
|
18
|
-
private final
|
|
19
|
-
private final PasswordEncoder passwordEncoder;
|
|
19
|
+
private final AuthService authService;
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
public AuthController(JwtTokenProvider tokenProvider, PasswordEncoder passwordEncoder) {
|
|
25
|
-
this.tokenProvider = tokenProvider;
|
|
26
|
-
this.passwordEncoder = passwordEncoder;
|
|
21
|
+
public AuthController(AuthService authService) {
|
|
22
|
+
this.authService = authService;
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
@PostMapping("/register")
|
|
30
|
-
public ResponseEntity
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return ResponseEntity.
|
|
26
|
+
public ResponseEntity<?> register(@RequestBody AuthRequest request) {
|
|
27
|
+
try {
|
|
28
|
+
AuthResponse response = authService.register(request);
|
|
29
|
+
return ResponseEntity.ok(response);
|
|
30
|
+
} catch (IllegalArgumentException e) {
|
|
31
|
+
return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
|
|
34
32
|
}
|
|
35
|
-
|
|
36
|
-
String userId = UUID.randomUUID().toString();
|
|
37
|
-
String hashedPassword = passwordEncoder.encode(request.password());
|
|
38
|
-
|
|
39
|
-
users.put(request.email(), Map.of(
|
|
40
|
-
"id", userId,
|
|
41
|
-
"email", request.email(),
|
|
42
|
-
"password", hashedPassword
|
|
43
|
-
));
|
|
44
|
-
|
|
45
|
-
String token = tokenProvider.generateToken(userId, request.email());
|
|
46
|
-
|
|
47
|
-
return ResponseEntity.ok(new AuthResponse(token, userId, request.email()));
|
|
48
33
|
}
|
|
49
34
|
|
|
50
35
|
@PostMapping("/login")
|
|
51
36
|
public ResponseEntity<?> login(@RequestBody AuthRequest request) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
37
|
+
try {
|
|
38
|
+
AuthResponse response = authService.login(request);
|
|
39
|
+
return ResponseEntity.ok(response);
|
|
40
|
+
} catch (IllegalArgumentException e) {
|
|
41
|
+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Map.of("error", e.getMessage()));
|
|
56
42
|
}
|
|
57
|
-
|
|
58
|
-
String token = tokenProvider.generateToken(user.get("id"), user.get("email"));
|
|
59
|
-
|
|
60
|
-
return ResponseEntity.ok(new AuthResponse(token, user.get("id"), user.get("email")));
|
|
61
43
|
}
|
|
62
44
|
|
|
63
45
|
@GetMapping("/me")
|
|
64
|
-
public ResponseEntity
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
46
|
+
public ResponseEntity<?> me() {
|
|
47
|
+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
48
|
+
if (authentication == null || !authentication.isAuthenticated()) {
|
|
49
|
+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
String userId = (String) authentication.getPrincipal(); // Assuming custom jwt filter sets principal to id
|
|
54
|
+
User user = authService.getMe(userId);
|
|
55
|
+
return ResponseEntity.ok(Map.of(
|
|
56
|
+
"id", user.getId(),
|
|
57
|
+
"email", user.getEmail(),
|
|
58
|
+
"name", user.getName()
|
|
59
|
+
));
|
|
60
|
+
} catch (Exception e) {
|
|
61
|
+
// Keep original behavior if principal isn't id
|
|
62
|
+
return ResponseEntity.ok(Map.of("message", "Authenticated"));
|
|
63
|
+
}
|
|
68
64
|
}
|
|
69
|
-
}
|
|
65
|
+
}
|
package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/ItemController.java.hbs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
package {{packageName}}.controller;
|
|
2
|
+
|
|
3
|
+
import {{packageName}}.model.Item;
|
|
4
|
+
import {{packageName}}.repository.ItemRepository;
|
|
5
|
+
import org.springframework.http.ResponseEntity;
|
|
6
|
+
import org.springframework.web.bind.annotation.*;
|
|
7
|
+
|
|
8
|
+
import java.util.List;
|
|
9
|
+
|
|
10
|
+
@RestController
|
|
11
|
+
@RequestMapping("/api/items")
|
|
12
|
+
public class ItemController {
|
|
13
|
+
|
|
14
|
+
private final ItemRepository itemRepository;
|
|
15
|
+
|
|
16
|
+
public ItemController(ItemRepository itemRepository) {
|
|
17
|
+
this.itemRepository = itemRepository;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@GetMapping
|
|
21
|
+
public List<Item> getAllItems() {
|
|
22
|
+
return itemRepository.findAll();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@PostMapping
|
|
26
|
+
public Item createItem(@RequestBody Item item) {
|
|
27
|
+
return itemRepository.save(item);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@GetMapping("/{id}")
|
|
31
|
+
public ResponseEntity<Item> getItem(@PathVariable String id) {
|
|
32
|
+
return itemRepository.findById(id)
|
|
33
|
+
.map(ResponseEntity::ok)
|
|
34
|
+
.orElse(ResponseEntity.notFound().build());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@DeleteMapping("/{id}")
|
|
38
|
+
public ResponseEntity<Void> deleteItem(@PathVariable String id) {
|
|
39
|
+
itemRepository.deleteById(id);
|
|
40
|
+
return ResponseEntity.ok().build();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,49 +1,92 @@
|
|
|
1
1
|
package {{packageName}}.controller;
|
|
2
2
|
|
|
3
|
+
import com.stripe.exception.SignatureVerificationException;
|
|
4
|
+
import com.stripe.model.Event;
|
|
5
|
+
import com.stripe.net.Webhook;
|
|
6
|
+
import {{packageName}}.model.User;
|
|
7
|
+
import {{packageName}}.repository.UserRepository;
|
|
3
8
|
import {{packageName}}.service.StripeService;
|
|
4
|
-
import
|
|
9
|
+
import org.slf4j.Logger;
|
|
10
|
+
import org.slf4j.LoggerFactory;
|
|
11
|
+
import org.springframework.beans.factory.annotation.Value;
|
|
12
|
+
import org.springframework.http.HttpStatus;
|
|
5
13
|
import org.springframework.http.ResponseEntity;
|
|
6
14
|
import org.springframework.web.bind.annotation.*;
|
|
7
15
|
|
|
8
16
|
import java.util.Map;
|
|
17
|
+
import java.util.Optional;
|
|
9
18
|
|
|
10
19
|
@RestController
|
|
11
20
|
@RequestMapping("/api/payments")
|
|
12
21
|
public class PaymentsController {
|
|
13
22
|
|
|
23
|
+
private static final Logger logger = LoggerFactory.getLogger(PaymentsController.class);
|
|
24
|
+
|
|
25
|
+
@Value("${stripe.webhook-secret}")
|
|
26
|
+
private String webhookSecret;
|
|
27
|
+
|
|
14
28
|
private final StripeService stripeService;
|
|
29
|
+
private final UserRepository userRepository;
|
|
15
30
|
|
|
16
|
-
public PaymentsController(StripeService stripeService) {
|
|
31
|
+
public PaymentsController(StripeService stripeService, UserRepository userRepository) {
|
|
17
32
|
this.stripeService = stripeService;
|
|
33
|
+
this.userRepository = userRepository;
|
|
18
34
|
}
|
|
19
35
|
|
|
20
36
|
@PostMapping("/checkout")
|
|
21
|
-
public ResponseEntity
|
|
22
|
-
<?> createCheckout(@RequestBody Map<String, String> request) {
|
|
37
|
+
public ResponseEntity<?> createCheckout(@RequestBody Map<String, String> request) {
|
|
23
38
|
try {
|
|
24
|
-
|
|
39
|
+
String customerId = request.get("customerId"); // Optional
|
|
40
|
+
com.stripe.model.checkout.Session session = stripeService.createCheckoutSession(customerId);
|
|
25
41
|
return ResponseEntity.ok(Map.of("url", session.getUrl()));
|
|
26
|
-
} catch (
|
|
27
|
-
|
|
42
|
+
} catch (Exception e) {
|
|
43
|
+
logger.error("Error creating checkout session", e);
|
|
44
|
+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
45
|
+
.body(Map.of("error", e.getMessage()));
|
|
28
46
|
}
|
|
29
47
|
}
|
|
30
48
|
|
|
31
|
-
@PostMapping("/
|
|
32
|
-
public ResponseEntity
|
|
49
|
+
@PostMapping("/webhook")
|
|
50
|
+
public ResponseEntity<String> handleWebhook(
|
|
51
|
+
@RequestBody String payload,
|
|
52
|
+
@RequestHeader("Stripe-Signature") String sigHeader) {
|
|
53
|
+
|
|
54
|
+
Event event;
|
|
55
|
+
|
|
33
56
|
try {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return ResponseEntity.status(
|
|
57
|
+
event = Webhook.constructEvent(payload, sigHeader, webhookSecret);
|
|
58
|
+
} catch (SignatureVerificationException e) {
|
|
59
|
+
logger.warn("Invalid signature", e);
|
|
60
|
+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
|
61
|
+
} catch (Exception e) {
|
|
62
|
+
logger.error("Error parsing webhook", e);
|
|
63
|
+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Error parsing webhook");
|
|
38
64
|
}
|
|
39
|
-
}
|
|
40
65
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
66
|
+
switch (event.getType()) {
|
|
67
|
+
case "checkout.session.completed":
|
|
68
|
+
com.stripe.model.checkout.Session session = (com.stripe.model.checkout.Session) event.getDataObjectDeserializer().getObject().orElse(null);
|
|
69
|
+
if (session != null) {
|
|
70
|
+
logger.info("Checkout completed: {}", session.getId());
|
|
71
|
+
// Match to database user here using session properties
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case "customer.subscription.updated":
|
|
75
|
+
com.stripe.model.Subscription subscription = (com.stripe.model.Subscription) event.getDataObjectDeserializer().getObject().orElse(null);
|
|
76
|
+
if (subscription != null) {
|
|
77
|
+
logger.info("Subscription updated: {}", subscription.getId());
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case "customer.subscription.deleted":
|
|
81
|
+
com.stripe.model.Subscription deletedSub = (com.stripe.model.Subscription) event.getDataObjectDeserializer().getObject().orElse(null);
|
|
82
|
+
if (deletedSub != null) {
|
|
83
|
+
logger.info("Subscription deleted: {}", deletedSub.getId());
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
logger.info("Unhandled event type: {}", event.getType());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return ResponseEntity.ok("Received");
|
|
48
91
|
}
|
|
49
|
-
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
package {{packageName}}.model;
|
|
2
|
+
|
|
3
|
+
import jakarta.persistence.*;
|
|
4
|
+
import lombok.*;
|
|
5
|
+
import org.springframework.data.annotation.CreatedDate;
|
|
6
|
+
import org.springframework.data.annotation.LastModifiedDate;
|
|
7
|
+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|
8
|
+
|
|
9
|
+
import java.time.LocalDateTime;
|
|
10
|
+
|
|
11
|
+
@Entity
|
|
12
|
+
@Table(name = "items")
|
|
13
|
+
@Data
|
|
14
|
+
@NoArgsConstructor
|
|
15
|
+
@AllArgsConstructor
|
|
16
|
+
@Builder
|
|
17
|
+
@EntityListeners(AuditingEntityListener.class)
|
|
18
|
+
public class Item {
|
|
19
|
+
|
|
20
|
+
@Id
|
|
21
|
+
@GeneratedValue(strategy = GenerationType.UUID)
|
|
22
|
+
private String id;
|
|
23
|
+
|
|
24
|
+
@Column(nullable = false)
|
|
25
|
+
private String name;
|
|
26
|
+
|
|
27
|
+
private String description;
|
|
28
|
+
|
|
29
|
+
private Double price;
|
|
30
|
+
|
|
31
|
+
@CreatedDate
|
|
32
|
+
@Column(name = "created_at", nullable = false, updatable = false)
|
|
33
|
+
private LocalDateTime createdAt;
|
|
34
|
+
|
|
35
|
+
@LastModifiedDate
|
|
36
|
+
@Column(name = "updated_at", nullable = false)
|
|
37
|
+
private LocalDateTime updatedAt;
|
|
38
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
package {{packageName}}.model;
|
|
2
|
+
|
|
3
|
+
import jakarta.persistence.*;
|
|
4
|
+
import lombok.*;
|
|
5
|
+
import org.springframework.data.annotation.CreatedDate;
|
|
6
|
+
import org.springframework.data.annotation.LastModifiedDate;
|
|
7
|
+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|
8
|
+
|
|
9
|
+
import java.time.LocalDateTime;
|
|
10
|
+
|
|
11
|
+
@Entity
|
|
12
|
+
@Table(name = "users")
|
|
13
|
+
@Data
|
|
14
|
+
@NoArgsConstructor
|
|
15
|
+
@AllArgsConstructor
|
|
16
|
+
@Builder
|
|
17
|
+
@EntityListeners(AuditingEntityListener.class)
|
|
18
|
+
public class User {
|
|
19
|
+
|
|
20
|
+
@Id
|
|
21
|
+
@GeneratedValue(strategy = GenerationType.UUID)
|
|
22
|
+
private String id;
|
|
23
|
+
|
|
24
|
+
@Column(unique = true, nullable = false)
|
|
25
|
+
private String email;
|
|
26
|
+
|
|
27
|
+
private String name;
|
|
28
|
+
|
|
29
|
+
private String password;
|
|
30
|
+
|
|
31
|
+
@Column(name = "stripe_customer_id")
|
|
32
|
+
private String stripeCustomerId;
|
|
33
|
+
|
|
34
|
+
@CreatedDate
|
|
35
|
+
@Column(name = "created_at", nullable = false, updatable = false)
|
|
36
|
+
private LocalDateTime createdAt;
|
|
37
|
+
|
|
38
|
+
@LastModifiedDate
|
|
39
|
+
@Column(name = "updated_at", nullable = false)
|
|
40
|
+
private LocalDateTime updatedAt;
|
|
41
|
+
}
|
package/templates/java-spring/mvc/src/main/java/{{packagePath}}/repository/ItemRepository.java.hbs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
package {{packageName}}.repository;
|
|
2
|
+
|
|
3
|
+
import {{packageName}}.model.Item;
|
|
4
|
+
import org.springframework.data.jpa.repository.JpaRepository;
|
|
5
|
+
import org.springframework.stereotype.Repository;
|
|
6
|
+
|
|
7
|
+
@Repository
|
|
8
|
+
public interface ItemRepository extends JpaRepository<Item, String> {
|
|
9
|
+
}
|
package/templates/java-spring/mvc/src/main/java/{{packagePath}}/repository/UserRepository.java.hbs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
package {{packageName}}.repository;
|
|
2
|
+
|
|
3
|
+
import {{packageName}}.model.User;
|
|
4
|
+
import org.springframework.data.jpa.repository.JpaRepository;
|
|
5
|
+
import org.springframework.stereotype.Repository;
|
|
6
|
+
|
|
7
|
+
import java.util.Optional;
|
|
8
|
+
|
|
9
|
+
@Repository
|
|
10
|
+
public interface UserRepository extends JpaRepository<User, String> {
|
|
11
|
+
Optional<User> findByEmail(String email);
|
|
12
|
+
Optional<User> findByStripeCustomerId(String stripeCustomerId);
|
|
13
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
package {{packageName}}.service;
|
|
2
|
+
|
|
3
|
+
import {{packageName}}.dto.AuthRequest;
|
|
4
|
+
import {{packageName}}.dto.AuthResponse;
|
|
5
|
+
import {{packageName}}.model.User;
|
|
6
|
+
import {{packageName}}.repository.UserRepository;
|
|
7
|
+
import {{packageName}}.security.JwtTokenProvider;
|
|
8
|
+
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
9
|
+
import org.springframework.stereotype.Service;
|
|
10
|
+
|
|
11
|
+
import java.util.Optional;
|
|
12
|
+
|
|
13
|
+
@Service
|
|
14
|
+
public class AuthService {
|
|
15
|
+
|
|
16
|
+
private final UserRepository userRepository;
|
|
17
|
+
private final PasswordEncoder passwordEncoder;
|
|
18
|
+
private final JwtTokenProvider tokenProvider;
|
|
19
|
+
|
|
20
|
+
public AuthService(UserRepository userRepository, PasswordEncoder passwordEncoder, JwtTokenProvider tokenProvider) {
|
|
21
|
+
this.userRepository = userRepository;
|
|
22
|
+
this.passwordEncoder = passwordEncoder;
|
|
23
|
+
this.tokenProvider = tokenProvider;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public AuthResponse register(AuthRequest request) {
|
|
27
|
+
if (userRepository.findByEmail(request.email()).isPresent()) {
|
|
28
|
+
throw new IllegalArgumentException("User already exists");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
String hashedPassword = passwordEncoder.encode(request.password());
|
|
32
|
+
|
|
33
|
+
User user = User.builder()
|
|
34
|
+
.email(request.email())
|
|
35
|
+
.password(hashedPassword)
|
|
36
|
+
.build();
|
|
37
|
+
|
|
38
|
+
user = userRepository.save(user);
|
|
39
|
+
|
|
40
|
+
String token = tokenProvider.generateToken(user.getId(), user.getEmail());
|
|
41
|
+
|
|
42
|
+
return new AuthResponse(token, user.getId(), user.getEmail());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public AuthResponse login(AuthRequest request) {
|
|
46
|
+
Optional<User> userOptional = userRepository.findByEmail(request.email());
|
|
47
|
+
|
|
48
|
+
if (userOptional.isEmpty() || !passwordEncoder.matches(request.password(), userOptional.get().getPassword())) {
|
|
49
|
+
throw new IllegalArgumentException("Invalid credentials");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
User user = userOptional.get();
|
|
53
|
+
String token = tokenProvider.generateToken(user.getId(), user.getEmail());
|
|
54
|
+
|
|
55
|
+
return new AuthResponse(token, user.getId(), user.getEmail());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public User getMe(String userId) {
|
|
59
|
+
return userRepository.findById(userId)
|
|
60
|
+
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
|
61
|
+
}
|
|
62
|
+
}
|
package/templates/java-spring/mvc/src/main/java/{{packagePath}}/service/StripeService.java.hbs
CHANGED
|
@@ -29,18 +29,18 @@ public class StripeService {
|
|
|
29
29
|
|
|
30
30
|
public Session createCheckoutSession(String customerId) throws StripeException {
|
|
31
31
|
SessionCreateParams.Builder paramsBuilder = SessionCreateParams.builder()
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
.setMode(SessionCreateParams.Mode.SUBSCRIPTION)
|
|
33
|
+
.setSuccessUrl(frontendUrl + "/success?session_id={CHECKOUT_SESSION_ID}")
|
|
34
|
+
.setCancelUrl(frontendUrl + "/cancel")
|
|
35
|
+
.addLineItem(
|
|
36
|
+
SessionCreateParams.LineItem.builder()
|
|
37
|
+
.setPrice(priceId)
|
|
38
|
+
.setQuantity(1L)
|
|
39
|
+
.build()
|
|
40
|
+
);
|
|
41
41
|
|
|
42
42
|
if (customerId != null && !customerId.isEmpty()) {
|
|
43
|
-
|
|
43
|
+
paramsBuilder.setCustomer(customerId);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
return Session.create(paramsBuilder.build());
|
|
@@ -48,20 +48,20 @@ public class StripeService {
|
|
|
48
48
|
|
|
49
49
|
public com.stripe.model.billingportal.Session createPortalSession(String customerId) throws StripeException {
|
|
50
50
|
com.stripe.param.billingportal.SessionCreateParams params =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
com.stripe.param.billingportal.SessionCreateParams.builder()
|
|
52
|
+
.setCustomer(customerId)
|
|
53
|
+
.setReturnUrl(frontendUrl + "/dashboard")
|
|
54
|
+
.build();
|
|
55
55
|
|
|
56
56
|
return com.stripe.model.billingportal.Session.create(params);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
public Customer createCustomer(String email, String name) throws StripeException {
|
|
60
60
|
CustomerCreateParams params = CustomerCreateParams.builder()
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
.setEmail(email)
|
|
62
|
+
.setName(name)
|
|
63
|
+
.build();
|
|
64
64
|
|
|
65
65
|
return Customer.create(params);
|
|
66
66
|
}
|
|
67
|
-
}
|
|
67
|
+
}
|
|
@@ -2,8 +2,10 @@ package {{packageName}};
|
|
|
2
2
|
|
|
3
3
|
import org.springframework.boot.SpringApplication;
|
|
4
4
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
5
|
+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
5
6
|
|
|
6
7
|
@SpringBootApplication
|
|
8
|
+
@EnableJpaAuditing
|
|
7
9
|
public class {{projectNamePascalCase}}Application {
|
|
8
10
|
|
|
9
11
|
public static void main(String[] args) {
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
"build": "nest build",
|
|
8
8
|
"start": "node dist/main.js",
|
|
9
9
|
"lint": "eslint \"{src,test}/**/*.ts\"",
|
|
10
|
-
"test": "jest"
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"db:generate": "prisma generate",
|
|
12
|
+
"db:push": "prisma db push"
|
|
11
13
|
},
|
|
12
14
|
"dependencies": {
|
|
13
15
|
"@nestjs/common": "^10.3.0",
|
|
@@ -16,6 +18,7 @@
|
|
|
16
18
|
"@nestjs/jwt": "^10.2.0",
|
|
17
19
|
"@nestjs/passport": "^10.0.3",
|
|
18
20
|
"@nestjs/platform-express": "^10.3.0",
|
|
21
|
+
"@prisma/client": "^5.10.2",
|
|
19
22
|
"bcryptjs": "^2.4.3",
|
|
20
23
|
"class-transformer": "^0.5.1",
|
|
21
24
|
"class-validator": "^0.14.1",
|
|
@@ -32,6 +35,7 @@
|
|
|
32
35
|
"@types/node": "^20.11.0",
|
|
33
36
|
"@types/passport-jwt": "^4.0.0",
|
|
34
37
|
"@types/uuid": "^9.0.7",
|
|
38
|
+
"prisma": "^5.10.2",
|
|
35
39
|
"typescript": "^5.3.3"
|
|
36
40
|
}
|
|
37
|
-
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
generator client {
|
|
2
|
+
provider = "prisma-client-js"
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
datasource db {
|
|
6
|
+
provider = "postgresql"
|
|
7
|
+
url = env("DATABASE_URL")
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
model User {
|
|
11
|
+
id String @id @default(uuid())
|
|
12
|
+
email String @unique
|
|
13
|
+
name String?
|
|
14
|
+
password String
|
|
15
|
+
stripeCustomerId String? @map("stripe_customer_id")
|
|
16
|
+
createdAt DateTime @default(now()) @map("created_at")
|
|
17
|
+
updatedAt DateTime @updatedAt @map("updated_at")
|
|
18
|
+
|
|
19
|
+
@@map("users")
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
model Item {
|
|
23
|
+
id String @id @default(uuid())
|
|
24
|
+
name String
|
|
25
|
+
description String?
|
|
26
|
+
price Float?
|
|
27
|
+
createdAt DateTime @default(now()) @map("created_at")
|
|
28
|
+
updatedAt DateTime @updatedAt @map("updated_at")
|
|
29
|
+
|
|
30
|
+
@@map("items")
|
|
31
|
+
}
|
|
@@ -4,14 +4,16 @@ import { ItemsModule } from './modules/items.module';
|
|
|
4
4
|
import { AuthModule } from './auth/auth.module';
|
|
5
5
|
import { PaymentsModule } from './payments/payments.module';
|
|
6
6
|
import { HealthController } from './controllers/health.controller';
|
|
7
|
+
import { PrismaModule } from './prisma/prisma.module';
|
|
7
8
|
|
|
8
9
|
@Module({
|
|
9
10
|
imports: [
|
|
10
11
|
ConfigModule.forRoot({ isGlobal: true }),
|
|
12
|
+
PrismaModule,
|
|
11
13
|
AuthModule,
|
|
12
14
|
PaymentsModule,
|
|
13
15
|
ItemsModule,
|
|
14
16
|
],
|
|
15
17
|
controllers: [HealthController],
|
|
16
18
|
})
|
|
17
|
-
export class AppModule {}
|
|
19
|
+
export class AppModule {}
|