Skip to content

Commit

Permalink
✨ created CustomAuthenticationSuccessHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jafacode committed Jan 8, 2025
1 parent 568354d commit 0371bea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.toyProject7.karrot.socialLogin.handler

import com.fasterxml.jackson.databind.ObjectMapper
import com.toyProject7.karrot.socialLogin.OAuth2AuthenticationException
import com.toyProject7.karrot.user.UserAccessTokenUtil
import com.toyProject7.karrot.user.service.UserService
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken
import org.springframework.security.oauth2.core.user.OAuth2User
import org.springframework.security.web.authentication.AuthenticationSuccessHandler
import org.springframework.stereotype.Component

@Component
class CustomAuthenticationSuccessHandler(
private val userService: UserService
) : AuthenticationSuccessHandler {

override fun onAuthenticationSuccess(
request: HttpServletRequest, response: HttpServletResponse, authentication: Authentication
) {
val oauth2User = authentication.principal as OAuth2User
val oauth2Token = authentication as OAuth2AuthenticationToken
val provider = oauth2Token.authorizedClientRegistrationId

// Extract attributes
val attributes = oauth2User.attributes
val providerId = extractProviderId(attributes, provider)
val email = extractEmail(attributes, provider)
val name = extractName(attributes, provider)

// Create or retrieve the user
val user = userService.createOrRetrieveSocialUser(email, providerId, provider, name)

// Generate JWT
val accessToken = UserAccessTokenUtil.generateAccessToken(user.id)

// Send the JWT in the response body as JSON
val responseBody = mapOf("accessToken" to accessToken, "user" to user)
response.contentType = "application/json"
response.characterEncoding = "UTF-8"
response.writer.write(ObjectMapper().writeValueAsString(responseBody))
}

private fun extractProviderId(attributes: Map<String, Any>, provider: String): String {
return when (provider) {
"google" -> attributes["sub"] as String
"naver" -> (attributes["response"] as Map<String, Any>)["id"] as String
"kakao" -> attributes["id"].toString() // Kakao's id may be Long, convert to String
else -> throw OAuth2AuthenticationException()
}
}

private fun extractEmail(attributes: Map<String, Any>, provider: String): String {
return when (provider) {
"google" -> attributes["email"] as String
"naver" -> (attributes["response"] as Map<String, Any>)["email"] as String
"kakao" -> {
val kakaoAccount = attributes["kakao_account"] as Map<String, Any>
kakaoAccount["email"] as String
}
else -> throw OAuth2AuthenticationException()
}
}

private fun extractName(attributes: Map<String, Any>, provider: String): String {
return when (provider) {
"google" -> attributes["name"] as String
"naver" -> (attributes["response"] as Map<String, Any>)["name"] as String
"kakao" -> {
val kakaoAccount = attributes["kakao_account"] as Map<String, Any>
val profile = kakaoAccount["profile"] as Map<String, Any>
profile["nickname"] as String
}
else -> "Unknown"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class SocialLoginUserService(private val userService: UserService) : OAuth2UserS

override fun loadUser(userRequest: OAuth2UserRequest): OAuth2User {
// Load the user details from the OAuth2 provider
val oauth2User = oAuth2UserService.loadUser(userRequest)

// Extract attributes
val provider = userRequest.clientRegistration.registrationId
val email = oauth2User.getAttribute<String>("email") ?: throw OAuth2AuthenticationException()
val providerId = oauth2User.getAttribute<String>("sub") ?: throw OAuth2AuthenticationException()
val name = oauth2User.getAttribute<String>("name") ?: throw OAuth2AuthenticationException()

// Create or retrieve the user
userService.createOrRetrieveSocialUser(email, providerId, provider, name)
return oauth2User
return oAuth2UserService.loadUser(userRequest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class NormalUser(
temperature: Double,
email: String,

@Column(name = "user_id", nullable = false)
@Column(name = "user_id")
var userId: String,
@Column(name = "hashed_password", nullable = false)
@Column(name = "hashed_password")
var hashedPassword: String,
) : UserEntity(nickname = nickname, location = location, temperature = temperature, email = email) {
// Additional attributes and methods for normal users can go here
Expand Down

0 comments on commit 0371bea

Please sign in to comment.