Skip to content

Commit

Permalink
Add support for user identities
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeo-workos committed Apr 12, 2024
1 parent 9bae08b commit a81ce53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions java-usermanagement-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ plugins {
id("com.workos.java.examples.java-application-conventions")
}

repositories {
flatDir {
dirs("libs")
dirs("build/libs")
}
}


application {
mainClass.set("com.workos.java.examples.UserManagementApp")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.workos.java.examples;

import com.workos.WorkOS;
import com.workos.usermanagement.models.Identity;
import com.workos.usermanagement.models.User;
import com.workos.usermanagement.types.UserManagementProviderEnumType;
import io.github.cdimascio.dotenv.Dotenv;
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.staticfiles.Location;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;


public class UserManagementApp {
Expand Down Expand Up @@ -49,15 +52,16 @@ public void callback(Context ctx) {
assert code != null;

User user = workos.userManagement.authenticateWithCode(clientId, code, null).getUser();
Identity[] identities = workos.userManagement.getUserIdentities(user.getId());
ctx.sessionAttribute("user", user);
ctx.sessionAttribute("identities", Arrays.stream(identities).map(Identity::toString).toList().toString());

ctx.redirect("/");
}

public void isLoggedIn(Context ctx) {

if (ctx.sessionAttribute("user") != null){
ctx.render("user.jte", Collections.singletonMap("user", ctx.sessionAttribute("user")));
ctx.render("user.jte", Map.of("user", ctx.sessionAttribute("user"), "identities", ctx.sessionAttribute("identities")));
} else {
ctx.render("home.jte");
}
Expand Down
5 changes: 4 additions & 1 deletion java-usermanagement-example/src/main/jte/user.jte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@param com.workos.usermanagement.models.User user
@param String identities

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
Expand Down Expand Up @@ -41,11 +42,13 @@
<tr>
<td>Email Verified: <code>${user.getEmailVerified()}</code></td>
</tr>
<tr>
<td>Identities: <code>${identities}</code></td>
</tr>
</table>
<div class="flex width-40vw">
<a href="/logout"><button class='button'>Logout</button></a>
</div>
</div>

</div>
</div>

0 comments on commit a81ce53

Please sign in to comment.