generated from semicolondsm/Semicolon_Repository_Generator
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add save RefreshToken logic (#62)
* feat: add save RefreshToken logic * chore: add redis * style: ktlint
- Loading branch information
1 parent
e717838
commit 1f5c79f
Showing
10 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
8 changes: 8 additions & 0 deletions
8
user-domain/src/main/kotlin/com/xquare/v1userservice/user/refreshtoken/RefreshToken.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.xquare.v1userservice.user.refreshtoken | ||
|
||
import java.util.UUID | ||
|
||
data class RefreshToken( | ||
val tokenValue: String, | ||
val userId: UUID | ||
) |
7 changes: 7 additions & 0 deletions
7
...-domain/src/main/kotlin/com/xquare/v1userservice/user/refreshtoken/spi/RefreshTokenSpi.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.xquare.v1userservice.user.refreshtoken.spi | ||
|
||
import com.xquare.v1userservice.user.refreshtoken.RefreshToken | ||
|
||
interface RefreshTokenSpi { | ||
suspend fun saveRefreshToken(refreshToken: RefreshToken): RefreshToken | ||
} |
This file contains 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
28 changes: 28 additions & 0 deletions
28
...ucture/src/main/kotlin/com/xquare/v1userservice/configuration/redis/RedisConfiguration.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.xquare.v1userservice.configuration.redis | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Primary | ||
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory | ||
import org.springframework.data.redis.core.ReactiveRedisOperations | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer | ||
import org.springframework.data.redis.serializer.RedisSerializationContext | ||
import org.springframework.data.redis.serializer.StringRedisSerializer | ||
|
||
@Configuration | ||
class RedisConfiguration { | ||
@Primary | ||
@Bean | ||
fun redisOperations(factory: ReactiveRedisConnectionFactory): ReactiveRedisOperations<String, Any> { | ||
val jsonSerializer = GenericJackson2JsonRedisSerializer() | ||
val stringSerializer = StringRedisSerializer() | ||
|
||
val serializationContext = RedisSerializationContext.newSerializationContext<String, Any>() | ||
.key(stringSerializer) | ||
.value(jsonSerializer) | ||
.build() | ||
|
||
return ReactiveRedisTemplate(factory, serializationContext) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...tructure/src/main/kotlin/com/xquare/v1userservice/user/refreshtoken/RefreshTokenEntity.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.xquare.v1userservice.user.refreshtoken | ||
|
||
import java.util.UUID | ||
|
||
data class RefreshTokenEntity( | ||
val tokenValue: String, | ||
val userId: UUID | ||
) |
7 changes: 7 additions & 0 deletions
7
.../com/xquare/v1userservice/user/refreshtoken/exceptions/RefreshTokenSaveFailedException.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.xquare.v1userservice.user.refreshtoken.exceptions | ||
|
||
import com.xquare.v1userservice.exceptions.BaseException | ||
|
||
class RefreshTokenSaveFailedException( | ||
errorMessage: String | ||
) : BaseException(errorMessage, 500) |
11 changes: 11 additions & 0 deletions
11
...main/kotlin/com/xquare/v1userservice/user/refreshtoken/mapper/RefreshTokenDomainMapper.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.xquare.v1userservice.user.refreshtoken.mapper | ||
|
||
import com.xquare.v1userservice.user.refreshtoken.RefreshToken | ||
import com.xquare.v1userservice.user.refreshtoken.RefreshTokenEntity | ||
import org.mapstruct.Mapper | ||
|
||
@Mapper | ||
interface RefreshTokenDomainMapper { | ||
fun refreshTokenDomainToEntity(refreshToken: RefreshToken): RefreshTokenEntity | ||
fun refreshTokenEntityToDomain(refreshTokenEntity: RefreshTokenEntity): RefreshToken | ||
} |
27 changes: 27 additions & 0 deletions
27
...ure/src/main/kotlin/com/xquare/v1userservice/user/refreshtoken/spi/RefreshTokenSpiImpl.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.xquare.v1userservice.user.refreshtoken.spi | ||
|
||
import com.xquare.v1userservice.user.refreshtoken.RefreshToken | ||
import com.xquare.v1userservice.user.refreshtoken.exceptions.RefreshTokenSaveFailedException | ||
import com.xquare.v1userservice.user.refreshtoken.mapper.RefreshTokenDomainMapper | ||
import java.time.Duration | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import org.springframework.data.redis.core.ReactiveRedisOperations | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class RefreshTokenSpiImpl( | ||
private val reactiveRedisOperations: ReactiveRedisOperations<String, Any>, | ||
private val refreshTokenDomainMapper: RefreshTokenDomainMapper | ||
) : RefreshTokenSpi { | ||
override suspend fun saveRefreshToken(refreshToken: RefreshToken): RefreshToken { | ||
val refreshTokenEntity = refreshTokenDomainMapper.refreshTokenDomainToEntity(refreshToken) | ||
val isSaveSuccess = reactiveRedisOperations.opsForValue() | ||
.set(refreshToken.tokenValue, refreshTokenEntity, Duration.ofDays(14)).awaitSingle() | ||
|
||
if (!isSaveSuccess) { | ||
throw RefreshTokenSaveFailedException("Refresh token save failed") | ||
} | ||
|
||
return refreshToken | ||
} | ||
} |