Added refresh token endpoint

This commit is contained in:
Filip Izydorczyk
2020-12-23 11:51:17 +01:00
parent 7f630ccdac
commit 61c5a43192
7 changed files with 45 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package com.plannaplan.controllers;
import java.util.UUID;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.responses.models.TokenResponse;
@ -61,4 +63,16 @@ public class TokenController {
}
}
@GetMapping("/token/refresh")
@ApiOperation(value = "Endpoint to access new token based on refresh token. It's needed when request with provided token fail with code 403")
public ResponseEntity<TokenResponse> getRefreshToken(
@RequestParam("refreshToken") @ApiParam(value = "Refresh token obtained in /token request") final String refreshToken) {
User user = this.userService.getUserByRefreshToken(refreshToken)
.orElseThrow(() -> new NullPointerException("User not found"));
user.setToken(UUID.randomUUID().toString());
user = this.userService.save(user);
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);
}
}