package com.example.task.service; import com.example.task.entity.StudentEntity; import com.example.task.repository.StudentRepository; import lombok.AllArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; @Service @AllArgsConstructor public class UserAuthenticationService { private final StudentRepository studentRepository; public StudentEntity getLoggedInUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.isAuthenticated()) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { String username = ((UserDetails) principal).getUsername(); StudentEntity user = studentRepository.findByUsername(username).get(); return user; } } return null; } }