-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add login via email and google account
- Loading branch information
1 parent
85193eb
commit aadaa17
Showing
16 changed files
with
500 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// ErrorMessageEnum.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 07/01/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum ErrorMessageEnum: Swift.Error { | ||
case errorMessage(message:String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
StudyFora/Features/Login/Data/RepositoryImpl/LoginRepositoryImpl.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// LoginRepositoryImpl.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 06/01/24. | ||
// | ||
|
||
import Foundation | ||
import FirebaseAuth | ||
import GoogleSignIn | ||
|
||
struct LoginRepositoryImpl: LoginRepository { | ||
func login(withEmail data: RegisterRequestModel, completion: @escaping (Result<AuthDataResult, Error>) -> Void) { | ||
Auth.auth().signIn(withEmail: data.email, password: data.password) { authResult, error in | ||
if let error = error { | ||
completion(.failure(error)) | ||
} else if let authResult = authResult { | ||
completion(.success(authResult)) | ||
} else { | ||
fatalError("Unexpected nil value in authResult") | ||
} | ||
} | ||
} | ||
|
||
func login(withCredential credential: AuthCredential, completion: @escaping (Result<AuthDataResult, Error>) -> Void) { | ||
Auth.auth().signIn(with: credential) { authResult, error in | ||
if let error = error { | ||
completion(.failure(error)) | ||
} else if let authResult = authResult { | ||
completion(.success(authResult)) | ||
} else { | ||
fatalError("Unexpected nil value in authResult") | ||
} | ||
} | ||
} | ||
|
||
func signIn(config: GIDConfiguration, presentedUI: UIViewController, completion: @escaping (Result<AuthCredential, Error>) -> Void) { | ||
|
||
GIDSignIn.sharedInstance.signIn(withPresenting: presentedUI) { result, error in | ||
guard error == nil else { | ||
fatalError(error!.localizedDescription) | ||
} | ||
|
||
guard let user = result?.user, | ||
let idToken = user.idToken?.tokenString | ||
else { | ||
fatalError("Error loading user token") | ||
} | ||
|
||
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: user.accessToken.tokenString) | ||
|
||
completion(.success(credential)) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
StudyFora/Features/Login/Domain/Repository/LoginRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// LoginRepository.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 06/01/24. | ||
// | ||
|
||
import FirebaseAuth | ||
import GoogleSignIn | ||
|
||
protocol LoginRepository{ | ||
func login(withEmail data: RegisterRequestModel, completion: @escaping (Result<AuthDataResult, Error>) -> Void) | ||
func login(withCredential credential: AuthCredential, completion: @escaping (Result<AuthDataResult, Error>) -> Void) | ||
func signIn(config: GIDConfiguration, presentedUI: UIViewController, completion: @escaping (Result<AuthCredential, Error>) -> Void) | ||
} |
29 changes: 29 additions & 0 deletions
29
StudyFora/Features/Login/Domain/UseCase/CredentialLoginUseCase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// CredentialLoginUseCase.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 07/01/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import FirebaseAuth | ||
|
||
struct CredentialLoginUseCase { | ||
var repository: LoginRepository | ||
|
||
func execute(credential: AuthCredential) async throws -> AuthDataResult? { | ||
return try await withCheckedThrowingContinuation { continuation in | ||
repository.login(withCredential: credential) { result in | ||
do { | ||
let authResult = try result.get() | ||
print("User logined with credential successfully!") | ||
continuation.resume(returning: authResult) | ||
} catch { | ||
print("Error login user with credential: \(error.localizedDescription)") | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
StudyFora/Features/Login/Domain/UseCase/EmailLoginUseCase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// EmailLoginUseCase.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 06/01/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import FirebaseAuth | ||
|
||
struct EmailLoginUseCase { | ||
var repository: LoginRepository | ||
|
||
func execute(data: RegisterRequestModel) async throws -> AuthDataResult? { | ||
return try await withCheckedThrowingContinuation { continuation in | ||
repository.login(withEmail: data) { result in | ||
do { | ||
let authResult = try result.get() | ||
print("User logined with email successfully!") | ||
continuation.resume(returning: authResult) | ||
} catch { | ||
print("Error login user with email: \(error.localizedDescription)") | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
StudyFora/Features/Login/Domain/UseCase/GoogleLoginUseCase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// GoogleLoginUseCase.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 07/01/24. | ||
// | ||
|
||
import Foundation | ||
import FirebaseAuth | ||
import GoogleSignIn | ||
import FirebaseCore | ||
|
||
struct GoogleLoginUseCase { | ||
var repository: LoginRepository | ||
|
||
func execute() async throws -> AuthCredential? { | ||
guard | ||
let clientID = FirebaseApp.app()?.options.clientID, | ||
let windowScene = await UIApplication.shared.connectedScenes.first as? UIWindowScene, | ||
let rootViewController = await windowScene.windows.first?.rootViewController | ||
// let rootViewController = await UIApplication.shared.windows.first?.rootViewController | ||
else { | ||
throw ErrorMessageEnum.errorMessage(message: "No view controller available") | ||
} | ||
|
||
// Create Google Sign In configuration object. | ||
let config = GIDConfiguration(clientID: clientID) | ||
GIDSignIn.sharedInstance.configuration = config | ||
|
||
return try await withCheckedThrowingContinuation { continuation in | ||
repository.signIn(config: config, presentedUI: rootViewController) { credential in | ||
do { | ||
let credential = try credential.get() | ||
print("User logined with Google successfully!") | ||
continuation.resume(returning: credential) | ||
} catch { | ||
print("Error login user with Google: \(error.localizedDescription)") | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
StudyFora/Features/Login/Presentation/View/LoginView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// LoginView.swift | ||
// StudyFora | ||
// | ||
// Created by Carissa Farry Hilmi Az Zahra on 06/01/24. | ||
// | ||
|
||
import SwiftUI | ||
import GoogleSignInSwift | ||
|
||
struct LoginView: View { | ||
@EnvironmentObject var router: Router | ||
|
||
@StateObject var loginViewModel = LoginViewModel(loginRepository: LoginRepositoryImpl()) | ||
|
||
@State var email = "" | ||
@State var password = "" | ||
|
||
var body: some View { | ||
VStack { | ||
Text("Login") | ||
TextField("Email", text: $email) | ||
SecureField("Password", text: $password) | ||
Button(action: { | ||
Task { | ||
await loginViewModel.emailLogin(data: RegisterRequestModel(email: email, password: password)) | ||
print(email) | ||
} | ||
}) { | ||
Text("Log in") | ||
} | ||
|
||
GoogleSignInButton(viewModel: GoogleSignInButtonViewModel(scheme: .light, style: .wide, state: .normal), action: { | ||
Task { | ||
await loginViewModel.googleLogin() | ||
print(email) | ||
} | ||
}) | ||
|
||
Button("Back") { | ||
router.navigateBack() | ||
} | ||
} | ||
.padding() | ||
} | ||
} | ||
|
||
#Preview { | ||
LoginView() | ||
} |
Oops, something went wrong.