feat: トークン、リフレッシュトークンの発行とリフレッシュトークンからトークンの再生成ができるように

This commit is contained in:
usbharu 2023-05-02 08:48:23 +09:00
parent 6b30fc1f4d
commit 8640fc44ee
4 changed files with 24 additions and 11 deletions

View File

@ -101,7 +101,8 @@ fun Application.parent() {
inject<IUserAuthService>().value, inject<IUserAuthService>().value,
inject<IMetaRepository>().value, inject<IMetaRepository>().value,
inject<IJwtRefreshTokenRepository>().value, inject<IJwtRefreshTokenRepository>().value,
inject<IUserRepository>().value inject<IUserRepository>().value,
inject<IdGenerateService>().value
) )
configureRouting( configureRouting(
inject<HttpSignatureVerifyService>().value, inject<HttpSignatureVerifyService>().value,

View File

@ -107,14 +107,14 @@ fun Application.configureSecurity(
post("/refresh-token") { post("/refresh-token") {
val refreshToken = call.receive<RefreshToken>() val refreshToken = call.receive<RefreshToken>()
val findByToken = refreshTokenRepository.findByToken(refreshToken.refreshToken) val findByToken = refreshTokenRepository.findByToken(refreshToken.refreshToken)
?: return@post call.respond(HttpStatusCode.Forbidden) ?: return@post call.respondText("token not found",status = HttpStatusCode.Forbidden)
if (findByToken.createdAt.isAfter(Instant.now())) { if (findByToken.createdAt.isAfter(Instant.now())) {
return@post call.respond(HttpStatusCode.Forbidden) return@post call.respondText("created_at", status = HttpStatusCode.Forbidden)
} }
if (findByToken.expiresAt.isAfter(Instant.now())) { if (findByToken.expiresAt.isBefore(Instant.now())) {
return@post call.respond(HttpStatusCode.Forbidden) return@post call.respondText( "expires_at", status = HttpStatusCode.Forbidden)
} }
val user = userRepository.findById(findByToken.userId) val user = userRepository.findById(findByToken.userId)

View File

@ -18,13 +18,24 @@ export const App: Component = () => {
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}).then(res => res.text()) }).then(res => res.json())
.then(res => fetch("/auth-check", { // .then(res => fetch("/auth-check", {
method: "GET", // method: "GET",
// headers: {
// 'Authorization': 'Bearer ' + res.token
// }
// }))
// .then(res => res.json())
.then(res => {
console.log(res.token);
fetch("/refresh-token", {
method: "POST",
headers: { headers: {
'Authorization': 'Bearer ' + res 'Content-Type': 'application/json',
} },
})).then(res => console.log(res)) body: JSON.stringify({refreshToken: res.refreshToken}),
}).then(res=> res.json()).then(res => console.log(res.token))
})
} }
}> }>

View File

@ -10,6 +10,7 @@ export default defineConfig({
'/api': 'http://localhost:8080', '/api': 'http://localhost:8080',
'/login': 'http://localhost:8080', '/login': 'http://localhost:8080',
'/auth-check': 'http://localhost:8080', '/auth-check': 'http://localhost:8080',
'/refresh-token': 'http://localhost:8080',
} }
}, },
root: './src/main/web', root: './src/main/web',