Skip to content

Commit

Permalink
[FIX] 자동로그인 로직 가독성을 위한 리팩터링(#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimscastle committed Sep 5, 2024
1 parent 24a4244 commit 3eafd4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
16 changes: 12 additions & 4 deletions Genti_iOS/Genti_iOS/Data/Repositories/AuthRepositoryImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

final class AuthRepositoryImpl: AuthRepository {

let userdefaultsRepository: UserDefaultsRepository = UserDefaultsRepositoryImpl()
let requestService: RequestService

init(requestService: RequestService) {
Expand Down Expand Up @@ -37,9 +37,17 @@ final class AuthRepositoryImpl: AuthRepository {
return entity
}

func reissueToken(token: GentiTokenEntity) async throws -> GentiTokenEntity {
let dto: ReissueTokenDTO = try await requestService.fetchResponseNonRetry(for: AuthRouter.reissueToken(token: token))
return GentiTokenEntity(accessToken: dto.accessToken, refreshToken: dto.refreshToken)
func reissueTokenSuccess() async -> Bool {
do {
let token = userdefaultsRepository.getToken()
guard let accessToken = token.accessToken, let refreshToken = token.refreshToken, let fcmToken = token.fcmToken else { return false }
let dto: ReissueTokenDTO = try await requestService.fetchResponseNonRetry(for: AuthRouter.reissueToken(token: .init(accessToken: accessToken, refreshToken: refreshToken, fcmToken: fcmToken)))
userdefaultsRepository.set(to: dto.accessToken, forKey: .accessToken)
userdefaultsRepository.set(to: dto.refreshToken, forKey: .refreshToken)
return true
} catch {
return false
}
}

func logout() async throws {
Expand Down
2 changes: 1 addition & 1 deletion Genti_iOS/Genti_iOS/Domain/Interfaces/AuthRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
protocol AuthRepository {
func kakaoLogin(token: String, fcmToken: String) async throws -> SocialLoginEntity
func appleLogin(authorizationCode: String, identityToken: String, fcmToken: String) async throws -> SocialLoginEntity
func reissueToken(token: GentiTokenEntity) async throws -> GentiTokenEntity
func reissueTokenSuccess() async -> Bool
func signIn(sex: String, birthYear: String) async throws -> SignInUserEntity
func logout() async throws
func resign() async throws
Expand Down
26 changes: 8 additions & 18 deletions Genti_iOS/Genti_iOS/Domain/UseCases/SplashUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,16 @@ final class SplashUseCaseImpl: SplashUseCase {
self.userdefaultRepository = userdefaultRepository
}

@MainActor
func canAutoLogin() async -> Bool {
guard let userRole = userdefaultRepository.getUserRole() else { return false }
switch userRole {
case .signInComplete:
let token = userdefaultRepository.getToken()
guard let accessToken = token.accessToken, let refreshToken = token.refreshToken else { return false }
do {
let reissuedToken = try await authRepository.reissueToken(token: .init(accessToken: accessToken, refreshToken: refreshToken))
guard let accessToken = reissuedToken.accessToken, let refreshToken = reissuedToken.refreshToken else {
return false
}
userdefaultRepository.setAccessToken(token: accessToken)
userdefaultRepository.setRefreshToken(token: refreshToken)
return true
} catch {
return false
}
case .signInNotComplete:
guard checkUserAlreadySignIn() else { return false }
if await authRepository.reissueTokenSuccess() {
return true
} else {
return false
}
}

private func checkUserAlreadySignIn() -> Bool {
return userdefaultRepository.getUserRole() == .signInComplete
}
}

0 comments on commit 3eafd4a

Please sign in to comment.