package io.abnd.rvep.security.service.impl;

import io.abnd.rvep.security.dao.intf.RvepUserRoleDAO;
import io.abnd.rvep.security.model.RvepRole;
import io.abnd.rvep.security.model.RvepUserRole;
import io.abnd.rvep.security.service.intf.JwtGenerator;
import io.abnd.rvep.user.dao.intf.RvepUserProfileDAO;
import io.abnd.rvep.user.model.RvepUser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RvepJwtGenerator implements JwtGenerator {

    private RvepUserRoleDAO rvepUserRoleDAO;
    private RvepUserProfileDAO rvepUserProfileDAO;

    public RvepJwtGenerator(RvepUserRoleDAO rvepUserRoleDAO,
                            RvepUserProfileDAO rvepUserProfileDAO) {
        this.rvepUserProfileDAO = rvepUserProfileDAO;
        this.rvepUserRoleDAO = rvepUserRoleDAO;
    }

    @Override
    public String generateIdToken(String email, String provider) {
        // get user
        RvepUser user = rvepUserProfileDAO.findByEmail(email).getRvepUser();
        // get user role
        RvepUserRole userRole = rvepUserRoleDAO.findByRvepUserId(user.getId());
        // get role
        RvepRole role = userRole.getRvepRole();

        // return idToken
        return Jwts.builder().setSubject(email)
                .claim("provider", provider)
                .claim("role", role.getName())
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, "secretkey")
                .compact();
    }
}

@@ -1398,24 +1398,72 @@
 .getName())%0A
+                .claim(%22user_id%22, user.getId())%0A
             

add user id to jwt claims