package com.myapp.auth;

import com.myapp.service.UserService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public final class TokenHandler {

    private final String secret;
    private final UserService userService;

    @Autowired
    public TokenHandler(@Value("${app.jwt.secret}") String secret, UserService userService) {
        this.secret = secret;
        this.userService = userService;
    }

    Optional<UserDetails> parseUserFromToken(String token) {
        String username = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
        return Optional.ofNullable(userService.loadUserByUsername(username));
    }

    public String createTokenForUser(UserDetails user) {
        return Jwts.builder()
                .setSubject(user.getUsername())
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
}


@@ -366,16 +366,71 @@
 onent;%0A%0A
+import java.time.ZonedDateTime;%0Aimport java.util.Date;%0A
 import j
@@ -1133,24 +1133,101 @@
 ils user) %7B%0A
+        final ZonedDateTime afterOneWeek = ZonedDateTime.now().plusWeeks(1);%0A
         retu
@@ -1344,32 +1344,100 @@
 .HS512, secret)%0A
+                .setExpiration(Date.from(afterOneWeek.toInstant()))%0A
                 

set expiration to jwt