How to get user info after validating JWT token?

I have followed this tutorial (https://auth0.com/docs/quickstart/backend/java-spring-security5/01-authorization) and successfully created working authentication flow for my Java API backend.

    @GetMapping("/posts")
    fun String(): getPosts: List<String> {
        val userId: String = "google-oauth2|108335805846181510876"
        val posts: List<String> = postDao.listPosts(userId);
        return posts;
    }

How should I get the userId from the token?

1 Like

Hehe got it.

@GetMapping("/posts")
    fun String(authentication: Authentication): getPosts: List<String> {
        val userId: String = authentication.name
        val posts: List<String> = postDao.listPosts(userId);
        return posts;
    }

Reference:

2 Likes

Glad you figured it out!