Skip to content

Commit

Permalink
chore: add apple sign in config, pr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eyatsenkoperpetio committed Nov 28, 2023
1 parent 2761c84 commit 49189c7
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public struct SignInView: View {
}
}
if viewModel.socialLoginEnabled {
SocialSignView(viewModel: .init(onSigned: viewModel.sign))
SocialSignView(
viewModel: .init(
config: viewModel.config,
completion: { viewModel.sign(with: $0) }
)
)
}
Spacer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import GoogleSignIn
import MSAL

public class SignInViewModel: ObservableObject {

@Published private(set) var isShowProgress = false
@Published private(set) var showError: Bool = false
@Published private(set) var showAlert: Bool = false
Expand All @@ -35,7 +35,7 @@ public class SignInViewModel: ObservableObject {
}

let router: AuthorizationRouter
private let config: ConfigProtocol
let config: ConfigProtocol
private let interactor: AuthInteractorProtocol
private let analytics: AuthorizationAnalytics
private let validator: Validator
Expand Down Expand Up @@ -92,13 +92,15 @@ public class SignInViewModel: ObservableObject {
}
}

@MainActor
func sign(with result: Result<SocialResult, Error>) {
result.success(social)
result.success { social(result: $0) }
result.failure { error in
errorMessage = error.localizedDescription
}
}

@MainActor
private func social(result: SocialResult) {
switch result {
case .apple(let appleCredentials):
Expand All @@ -112,6 +114,7 @@ public class SignInViewModel: ObservableObject {
}
}

@MainActor
private func appleLogin(_ credentials: AppleCredentials, backend: String) {
socialLogin(
externalToken: credentials.token,
Expand All @@ -120,6 +123,7 @@ public class SignInViewModel: ObservableObject {
)
}

@MainActor
private func facebookLogin(backend: String) {
guard let currentAccessToken = AccessToken.current?.tokenString else {
return
Expand All @@ -131,6 +135,7 @@ public class SignInViewModel: ObservableObject {
)
}

@MainActor
private func googleLogin(_ result: GIDSignInResult, backend: String) {
socialLogin(
externalToken: result.user.accessToken.tokenString,
Expand All @@ -139,6 +144,7 @@ public class SignInViewModel: ObservableObject {
)
}

@MainActor
private func microsoftLogin(_ token: String, backend: String) {
socialLogin(
externalToken: token,
Expand All @@ -147,8 +153,9 @@ public class SignInViewModel: ObservableObject {
)
}

@MainActor
private func socialLogin(externalToken: String, backend: String, loginMethod: LoginMethod) {
Task { @MainActor in
Task {
isShowProgress = true
do {
let user = try await interactor.login(externalToken: externalToken, backend: backend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public struct SignUpView: View {
if viewModel.socialLoginEnabled {
SocialSignView(
signType: .register,
viewModel: .init(onSigned: viewModel.register)
viewModel: .init(
config: viewModel.config,
completion: { viewModel.register(with: $0) }
)
)
.padding(.bottom, 30)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ public class SignUpViewModel: ObservableObject {
}
}

@MainActor
func register(with result: Result<SocialResult, Error>) {
result.success(social)
result.failure { error in
errorMessage = error.localizedDescription
}
}

@MainActor
private func social(result: SocialResult) {
switch result {
case .apple(let appleCredentials):
Expand All @@ -146,13 +148,15 @@ public class SignUpViewModel: ObservableObject {
}
}

@MainActor
private func appleLogin(_ credentials: AppleCredentials, backend: String) {
registerSocial(
externalToken: credentials.token,
backend: backend
)
}

@MainActor
private func facebookLogin(backend: String) {
guard let currentAccessToken = AccessToken.current?.tokenString else {
return
Expand All @@ -163,22 +167,25 @@ public class SignUpViewModel: ObservableObject {
)
}

@MainActor
private func googleLogin(_ result: GIDSignInResult, backend: String) {
registerSocial(
externalToken: result.user.accessToken.tokenString,
backend: backend
)
}

@MainActor
private func microsoftLogin(_ token: String, backend: String) {
registerSocial(
externalToken: token,
backend: backend
)
}

@MainActor
private func registerSocial(externalToken: String, backend: String) {
Task { @MainActor in
Task {
await registerUser(
externalToken: externalToken,
backend: backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Core

struct SocialSignView: View {

// MARK: - Properties -
// MARK: - Properties

@StateObject var viewModel: SocialSignViewModel

Expand All @@ -37,7 +37,7 @@ struct SocialSignView: View {
}
}

// MARK: - Views -
// MARK: - Views

var body: some View {
VStack(spacing: 10) {
Expand Down Expand Up @@ -89,7 +89,7 @@ struct SocialSignView: View {
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(title) \(AuthLocalization.microsoft)")
}
if viewModel.config.socialLogin.appleSigninEnabled {
if viewModel.config.appleSignIn.enable {
LabelButton(
image: CoreAssets.iconApple.swiftUIImage,
title: "\(title) \(AuthLocalization.apple)",
Expand All @@ -106,7 +106,7 @@ struct SocialSignView: View {
#if DEBUG
struct SocialSignView_Previews: PreviewProvider {
static var previews: some View {
let vm = SocialSignViewModel(onSigned: { _ in })
let vm = SocialSignViewModel(config: ConfigMock(), completion: { _ in })
SocialSignView(viewModel: vm).padding()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ enum SocialResult {

final public class SocialSignViewModel: ObservableObject {

// MARK: - Properties -
// MARK: - Properties

private var onSigned: ((Result<SocialResult, Error>) -> Void)
private var completion: ((Result<SocialResult, Error>) -> Void)

init(
config: ConfigProtocol = Container.shared.resolve(ConfigProtocol.self) ?? ConfigMock(),
onSigned: @escaping (Result<SocialResult, Error>) -> Void
config: ConfigProtocol,
completion: @escaping (Result<SocialResult, Error>) -> Void
) {
self.config = config
self.onSigned = onSigned
self.completion = completion
}

let config: ConfigProtocol
let config: ConfigProtocol

private let appleSingInProvider: AppleSingInProvider = .init()
private let googleSingInProvider: GoogleSingInProvider = .init()
Expand All @@ -58,7 +58,7 @@ final public class SocialSignViewModel: ObservableObject {
UIApplication.topViewController()
}

// MARK: - Public Intens -
// MARK: - Public Intens

func signInWithApple() {
appleSingInProvider.request { [weak self] result in
Expand Down Expand Up @@ -118,11 +118,11 @@ final public class SocialSignViewModel: ObservableObject {
}

private func success(with social: SocialResult) {
onSigned(.success(social))
completion(.success(social))
}

private func failure(_ error: Error) {
onSigned(.failure(error))
completion(.failure(error))
}

}
8 changes: 4 additions & 4 deletions Core/Core.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
BAFB99842B0E282E007D09F9 /* MicrosoftConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB99832B0E282E007D09F9 /* MicrosoftConfig.swift */; };
BAFB998E2B0F70F1007D09F9 /* CustomError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB998D2B0F70F1007D09F9 /* CustomError.swift */; };
BAFB99902B14B377007D09F9 /* GoogleConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB998F2B14B377007D09F9 /* GoogleConfig.swift */; };
BAFB99922B14E23D007D09F9 /* SocialLoginConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB99912B14E23D007D09F9 /* SocialLoginConfig.swift */; };
BAFB99922B14E23D007D09F9 /* AppleSignInConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB99912B14E23D007D09F9 /* AppleSignInConfig.swift */; };
C8C446EF233F81B9FABB77D2 /* Pods_App_Core.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 349B90CD6579F7B8D257E515 /* Pods_App_Core.framework */; };
CFC84952299F8B890055E497 /* Debounce.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFC84951299F8B890055E497 /* Debounce.swift */; };
DB4EBE9E2B1075E100CB4DC4 /* ConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB4EBE9D2B1075E100CB4DC4 /* ConfigTests.swift */; };
Expand Down Expand Up @@ -287,7 +287,7 @@
BAFB99832B0E282E007D09F9 /* MicrosoftConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MicrosoftConfig.swift; sourceTree = "<group>"; };
BAFB998D2B0F70F1007D09F9 /* CustomError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomError.swift; sourceTree = "<group>"; };
BAFB998F2B14B377007D09F9 /* GoogleConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleConfig.swift; sourceTree = "<group>"; };
BAFB99912B14E23D007D09F9 /* SocialLoginConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialLoginConfig.swift; sourceTree = "<group>"; };
BAFB99912B14E23D007D09F9 /* AppleSignInConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleSignInConfig.swift; sourceTree = "<group>"; };
C7E5BCE79CE297B20777B27A /* Pods-App-Core.debugprod.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Core.debugprod.xcconfig"; path = "Target Support Files/Pods-App-Core/Pods-App-Core.debugprod.xcconfig"; sourceTree = "<group>"; };
CFC84951299F8B890055E497 /* Debounce.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Debounce.swift; sourceTree = "<group>"; };
DB4EBE9D2B1075E100CB4DC4 /* ConfigTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -701,7 +701,7 @@
BAFB99812B0E2354007D09F9 /* FacebookConfig.swift */,
BAFB99832B0E282E007D09F9 /* MicrosoftConfig.swift */,
BAFB998F2B14B377007D09F9 /* GoogleConfig.swift */,
BAFB99912B14E23D007D09F9 /* SocialLoginConfig.swift */,
BAFB99912B14E23D007D09F9 /* AppleSignInConfig.swift */,
);
path = Config;
sourceTree = "<group>";
Expand Down Expand Up @@ -936,7 +936,7 @@
0260E58028FD792800BBBE18 /* WebUnitViewModel.swift in Sources */,
02A4833A29B8A9AB00D33F33 /* DownloadManager.swift in Sources */,
027BD3AE2909475000392132 /* KeyboardScrollerOptions.swift in Sources */,
BAFB99922B14E23D007D09F9 /* SocialLoginConfig.swift in Sources */,
BAFB99922B14E23D007D09F9 /* AppleSignInConfig.swift in Sources */,
027BD3BE2909478B00392132 /* UIResponder+CurrentResponder.swift in Sources */,
070019AE28F701B200D5FC78 /* Certificate.swift in Sources */,
076F297F2A1F80C800967E7D /* Pagination.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class MicrosoftSingInProvider {
let clientApplication = try createClientApplication()

guard let account = try clientApplication.allAccounts().first else {
throw CustomError.error(text: "Error")
throw CustomError.error(text: "Account not found")
}

return account
Expand Down
28 changes: 28 additions & 0 deletions Core/Core/Configuration/Config/AppleSignInConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// SocialLoginConfig.swift
// Core
//
// Created by Eugene Yatsenko on 27.11.2023.
//

import Foundation

private enum AppleSignInKeys: String {
case enable = "ENABLED"
}

public class AppleSignInConfig: NSObject {
public var enable: Bool

init(dictionary: [String: Any]) {
enable = dictionary[AppleSignInKeys.enable.rawValue] as? Bool ?? false
super.init()
}
}

private let appleSignInKey = "APPLE_SIGNIN"
extension Config {
public var appleSignIn: AppleSignInConfig {
AppleSignInConfig(dictionary: self[appleSignInKey] as? [String: AnyObject] ?? [:])
}
}
20 changes: 9 additions & 11 deletions Core/Core/Configuration/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public protocol ConfigProtocol {
var facebook: FacebookConfig { get }
var microsoft: MicrosoftConfig { get }
var google: GoogleConfig { get }
var socialLogin: SocialLoginConfig { get }
var appleSignIn: AppleSignInConfig { get }
var features: FeaturesConfig { get }
}

Expand Down Expand Up @@ -134,11 +134,10 @@ extension Config: ConfigProtocol {
}

public var socialLoginEnabled: Bool {
socialLogin.enable &&
(socialLogin.appleSigninEnabled ||
appleSignIn.enable ||
facebook.enabled ||
microsoft.enabled ||
google.enabled)
google.enabled
}
}

Expand All @@ -157,20 +156,19 @@ public class ConfigMock: Config {
],
"GOOGLE": [
"ENABLED": true,
"CLIENT_ID": "CLIENT_ID"
"CLIENT_ID": "clientId"
],
"FACEBOOK": [
"ENABLED": true,
"FACEBOOK_APP_ID": "FACEBOOK_APP_ID",
"CLIENT_TOKEN": "CLIENT_TOKEN",
"FACEBOOK_APP_ID": "facebookAppId",
"CLIENT_TOKEN": "client_token"
],
"MICROSOFT": [
"ENABLED": true,
"APP_ID": "APP_ID"
"APP_ID": "appId"
],
"SOCIAL_LOGINS": [
"ENABLED": true,
"APPLE_SIGNIN_ENABLED": true
"APPLE_SIGNIN": [
"ENABLED": true
]
]

Expand Down
Loading

0 comments on commit 49189c7

Please sign in to comment.