Created
May 12, 2020 12:20
-
-
Save dmi3coder/80c4bb604011493a3f546393493078cf to your computer and use it in GitHub Desktop.
UserResource with login and register endpoints for Quarkus JWT auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.donau.quarkify.user; | |
import tech.donau.quarkify.data.User; | |
import tech.donau.quarkify.security.TokenService; | |
import javax.inject.Inject; | |
import javax.transaction.Transactional; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
@Path("/users") | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Produces(MediaType.APPLICATION_JSON) | |
public class UserResource { | |
@Inject | |
TokenService service; | |
@POST | |
@Path("/register") | |
@Transactional | |
public User register(User user) { | |
user.persist(); //super simplified registration, no checks of uniqueness | |
return user; | |
} | |
@GET | |
@Path("/login") | |
public String login(@QueryParam("login")String login, @QueryParam("password") String password) { | |
User existingUser = User.find("login", login).firstResult(); | |
if(existingUser == null || !existingUser.password.equals(password)) { | |
throw new WebApplicationException(Response.status(404).entity("No user found or password is incorrect").build()); | |
} | |
return service.generateUserToken(existingUser.email, password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment