Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 6, 2024
1 parent fce9b33 commit 60330f0
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignInReq
import com.bestswlkh0310.graduating.graduatingserver.api.auth.res.TokenRes
import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolRepository
import com.bestswlkh0310.graduating.graduatingserver.core.school.getBy
import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserEntity
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRepository
import com.bestswlkh0310.graduating.graduatingserver.core.user.getByEmail
import com.bestswlkh0310.graduating.graduatingserver.core.user.*
import com.bestswlkh0310.graduating.graduatingserver.global.exception.CustomException
import com.bestswlkh0310.graduating.graduatingserver.infra.oauth2.apple.AppleOAuth2Client
import com.bestswlkh0310.graduating.graduatingserver.infra.oauth2.apple.AppleOAuth2Helper
Expand All @@ -30,44 +27,42 @@ class AuthService(
private val appleOAuth2Helper: AppleOAuth2Helper,
private val googleOAuth2Helper: GoogleOAuth2Helper,
private val jwtClient: JwtClient,
private val sessionHolder: UserAuthenticationHolder
) {
fun signIn(req: SignInReq): TokenRes {
val email = this.getEmail(
code = req.code,
platformType = req.platformType
)
val email = when (req.platformType) {
PlatformType.GOOGLE -> googleSignIn(req.code)
PlatformType.APPLE -> appleSignIn(req.code)
}
val user = userRepository.getByEmail(email)

return jwtClient.generate(user)
return TokenRes.of(
token = jwtClient.generate(user),
state = user.state
)
}

fun signUp(req: SignUpReq): TokenRes {
val email = this.getEmail(
code = req.code,
platformType = req.platformType
)

val school = schoolRepository.getBy(req.schoolId)
val user = userRepository.findByEmail(email).firstOrNull()
?: userRepository.save(
UserEntity(
email = email,
nickname = req.nickname,
platformType = req.platformType,
graduatingYear = req.graduatingYear,
school = school
)
)
val user = sessionHolder.current()

if (user.isActive) {
throw CustomException(HttpStatus.BAD_REQUEST, "User already sign in")
}

user.active(
nickname = req.nickname,
graduatingYear = req.graduatingYear,
school = school,
)
userRepository.save(user)

return jwtClient.generate(user)
return TokenRes.of(
token = jwtClient.generate(user),
state = user.state
)
}

private fun getEmail(code: String, platformType: PlatformType) =
when (platformType) {
PlatformType.GOOGLE -> googleSignIn(code)
PlatformType.APPLE -> appleSignIn(code)
}

// return email
private fun googleSignIn(code: String): String {
val token = googleOAuth2Client.getToken(code = code)
Expand Down Expand Up @@ -95,6 +90,9 @@ class AuthService(
userRepository.getByEmail(email)
}

return jwtClient.generate(user)
return TokenRes.of(
token = jwtClient.generate(user),
state = user.state
)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.bestswlkh0310.graduating.graduatingserver.api.auth.req

import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType
import jakarta.validation.constraints.Size

data class SignUpReq(
val platformType: PlatformType,
val code: String,
@Size(min = 1, max = 24)
val nickname: String,
val graduatingYear: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package com.bestswlkh0310.graduating.graduatingserver.api.auth.res

import com.bestswlkh0310.graduating.graduatingserver.core.user.UserState
import com.bestswlkh0310.graduating.graduatingserver.infra.token.Token

data class TokenRes(
val accessToken: String,
val refreshToken: String
)
val refreshToken: String,
val state: UserState
) {
companion object {
fun of(token: Token, state: UserState) =
TokenRes(
accessToken = token.accessToken,
refreshToken = token.refreshToken,
state = state
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserService(

fun editUser(req: EditUserReq): VoidRes {
val user = userAuthenticationHolder.current()
user.updateNickname(req.nickname)
user.update(req.nickname)
userRepository.save(user)
return VoidRes()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bestswlkh0310.graduating.graduatingserver.api.user.dto

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size

data class EditUserReq(
@field:NotBlank
@Size(min = 1, max = 24)
val nickname: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ data class UserRes(
fun of(user: UserEntity) = UserRes(
id = user.id,
email = user.email,
nickname = user.nickname,
graduatingYear = user.graduatingYear,
school = user.school
nickname = user.nickname!!,
graduatingYear = user.graduatingYear!!,
school = user.school!!
)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.bestswlkh0310.graduating.graduatingserver.core.user

import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolEntity
import com.bestswlkh0310.graduating.graduatingserver.global.exception.CustomException
import jakarta.persistence.*
import org.springframework.http.HttpStatus

@Entity
@Table(name = "tbl_user")
class UserEntity(
id: Long = 0,
email: String,
nickname: String,
role: UserRole = UserRole.USER,
platformType: PlatformType,
graduatingYear: Int,
school: SchoolEntity
state: UserState = UserState.PENDING,
nickname: String?,
graduatingYear: Int?,
school: SchoolEntity?,
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -23,10 +26,6 @@ class UserEntity(
var email = email
private set

@Column(nullable = false)
var nickname = nickname
private set

@Column(nullable = false)
@Enumerated(EnumType.STRING)
var role = role
Expand All @@ -36,17 +35,34 @@ class UserEntity(
@Enumerated(EnumType.STRING)
var platformType = platformType
private set

@Column(nullable = false)
@Enumerated(EnumType.STRING)
var state = state
private set

var nickname = nickname
private set

var graduatingYear = graduatingYear
private set
@JoinColumn(name = "school_id", nullable = false)

@JoinColumn(name = "school_id")
@ManyToOne
var school = school
private set

fun updateNickname(nickname: String) {
val isActive: Boolean
get() = state == UserState.NONE

fun active(nickname: String, graduatingYear: Int, school: SchoolEntity) {
this.state = UserState.NONE
this.nickname = nickname
this.graduatingYear = graduatingYear
this.school = school
}

fun update(nickname: String) {
this.nickname = nickname
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bestswlkh0310.graduating.graduatingserver.core.user

enum class UserState {
PENDING,
NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class WebSecurityConfig(
.authorizeHttpRequests {
it.requestMatchers(
"/management/**",
"/auth/**",

"/auth/refresh",
"/auth/sign-in",

"/school/**",

"/meals/**"
).permitAll()
.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JwtClient(
throw CustomException(HttpStatus.UNAUTHORIZED, "invalid token")
}

fun generate(user: UserEntity) = TokenRes(
fun generate(user: UserEntity) = Token(
accessToken = createToken(
user = user,
tokenExpired = jwtProperties.expired.access,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bestswlkh0310.graduating.graduatingserver.infra.token

data class Token(
val accessToken: String,
val refreshToken: String,
)
2 changes: 1 addition & 1 deletion Graduating-iOS/Data/Network/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension AuthEndpoint: MyTarget {
case .signIn:
.none
case .signUp:
.none
.refresh
case .refresh:
.none
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"location" : "https://github.com/hhhello0507/my-ios-kit",
"state" : {
"branch" : "main",
"revision" : "c7ea8ddf17976e4d971a1bc0079d95080ec92a85"
"revision" : "0af54f35d92ba306c76e74ff4c140c07e4cb9ff2"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension OnboardingFirstView: View {
receiveSignInFlow($0, platformType: .google)
}
.onReceive(viewModel.$signInFlow) {
if case .success = $0 {
if case .success(let token) = $0 {

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SignKit
final class OnboardingViewModel: ObservableObject {
@Published var code: String?
@Published var platformType: PlatformType?
@Published var email: String?
@Published var school: School?
@Published var graduatingYear = Date.now[.year] ?? 1900
@Published var nickname: String = ""
Expand All @@ -32,8 +33,6 @@ extension OnboardingViewModel {
}
AuthService.shared.signUp(
.init(
platformType: platformType,
code: code,
nickname: nickname,
graduatingYear: graduatingYear,
schoolId: school.id
Expand All @@ -45,7 +44,8 @@ extension OnboardingViewModel {
}

func signIn() {
guard let platformType, let code else {
guard let platformType,
let code else {
return
}
AuthService.shared.signIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension OAuth2ViewModel {
controller.performRequests()
}

@MainActor
func googleSignIn() {
guard let rootViewController = UIApplicationUtil.window?.rootViewController else {
self.googleSignInFlow = .failure(GoogleSignInError.notFoundRootViewController)
Expand All @@ -55,7 +56,6 @@ extension OAuth2ViewModel: ASAuthorizationControllerDelegate {
self.appleSignInFlow = .failure(AppleSignInError.credentialNotFound)
return
}

guard let code = credential.authorizationCode,
let codeString = String(data: code, encoding: .utf8) else {
print("\(#file) - Can't decode code")
Expand Down
6 changes: 0 additions & 6 deletions Graduating-iOS/Model/Auth/AuthDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ public struct SignInReq: ReqProtocol {
}

public struct SignUpReq: ReqProtocol {
public let platformType: PlatformType
public let code: String
public let nickname: String
public let graduatingYear: Int
public let schoolId: Int

public init(
platformType: PlatformType,
code: String,
nickname: String,
graduatingYear: Int,
schoolId: Int
) {
self.platformType = platformType
self.code = code
self.nickname = nickname
self.graduatingYear = graduatingYear
self.schoolId = schoolId
Expand Down

0 comments on commit 60330f0

Please sign in to comment.