Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Observable<String> createAccount(final String username, final String password, final String emailAddress) {
- mongoDatabase.getCollection(UserCodec.COLLETION_NAME, User)
- .find(eq(UserCodec.USERNAME_PROPERTY, username))
- .toObservable()
- .isEmpty()
- .map { final Boolean userStreamIsEmpty ->
- if (!userStreamIsEmpty) {
- throw new Exception("A user with the same username '${username}' was found in the DB")
- }
- userStreamIsEmpty
- }.flatMap({ final Boolean userStreamIsEmpty ->
- // MONGO DB REFERENCE IS RX MONGO, PASSWORD GENERATION IS BLOCKING. NOT SURE WHAT HAPPENS IN TERMS OF THREADING HERE...
- mongoDatabase.getCollection(UserCodec.COLLETION_NAME, User)
- .insertOne(new User(username: username, password: PasswordFactory.create().hash(password), emailAddress: emailAddress))
- .asObservable()
- } as Func1).map { final Success success ->
- mongoDatabase.getCollection(UserCodec.COLLETION_NAME, User)
- .find(eq(UserCodec.USERNAME_PROPERTY, username))
- }.flatMap({ final User retrievedUser ->
- Jwts
- .builder()
- .setPayload(Jackson.json(retrievedUser) as String)
- .setIssuedAt(new Date())
- .setExpiration(Date.from(LocalDateTime.now().plusHours(48).toInstant(ZoneOffset.UTC)))
- } as Func1)
- .bindExec()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement