Skip to content

Commit

Permalink
(chore): Add GID and SignInViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
N3v1 committed Oct 26, 2024
1 parent eb02aa9 commit 00c7530
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ScribbleLab.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,22 @@
388DC81F2CCBE37200C80FF9 /* ScribbleLabUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ScribbleLabUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
387BC5162CCD1BFA00B3D2A4 /* Exceptions for "ScribbleLab" folder in "ScribbleLab" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
);
target = 388DC8032CCBE36F00C80FF9 /* ScribbleLab */;
};
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
388DC8062CCBE36F00C80FF9 /* ScribbleLab */ = {
isa = PBXFileSystemSynchronizedRootGroup;
exceptions = (
387BC5162CCD1BFA00B3D2A4 /* Exceptions for "ScribbleLab" folder in "ScribbleLab" target */,
);
path = ScribbleLab;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -483,6 +496,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ScribbleLab/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ScribbleLab;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.education";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down Expand Up @@ -636,6 +650,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ScribbleLab/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ScribbleLab;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.education";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down Expand Up @@ -843,6 +858,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ScribbleLab/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ScribbleLab;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.education";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down Expand Up @@ -887,6 +903,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ScribbleLab/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ScribbleLab;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.education";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down
47 changes: 47 additions & 0 deletions ScribbleLab/App/Application_utility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Application_utility.swift
// ScribbleLab
//
// Copyright (c) 2024 ScribbleLabApp LLC. - All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import UIKit
import SwiftUI

final class Application_Utility: UIViewController {
static var rootViewController: UIViewController {
guard let screen = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
return .init()
}

guard let root = screen.windows.first?.rootViewController else {
return .init()
}

return root
}
}
64 changes: 64 additions & 0 deletions ScribbleLab/Core/Authentication/Models/SignInViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,68 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Combine
import Foundation
import Firebase
import FirebaseAuth
import ScribbleFoundation

class SignInViewModel: ObservableObject {

/// Published property holding the entered email.
@Published var password: String = ""

/// Published property holding the entered password.
@Published var email: String = ""

/// Published property determining if the log-in button is enabled.
///
/// The button is enabled when the entered password contains at least 8 characters,
/// the email contains an '@', and the username is not empty.
///
@Published var loginButtonDisabled: Bool = true

var cancellables = Set<AnyCancellable>()

// I have no f*** clue of what is happening here right now - Never touch this pice of
// code or else you'll die
// Thank you very much for your understanding
// Whishing you a great day!
init() {
$email
.combineLatest($password)
.map { [weak self] email, password in
self?.validateInput(email: email, password: password) ?? false
}
.assign(to: \.loginButtonDisabled, on: self)
.store(in: &cancellables)
}

/// Validates the email and password for login.
/// - Parameters:
/// - email: The email to validate.
/// - password: The password to validate.
/// - Returns: A boolean indicating whether the input is valid for login.
private func validateInput(email: String, password: String) -> Bool {
return !email.isEmpty && validateEmail(withEmail: email) && password.count >= 8
}

func validateEmail(withEmail email: String) -> Bool {
return Validation.isValidEmail(email)
}

func signIn() async throws {
try await SCIDAuthService.shared.logIn(withEmail: email, password: password)
}

deinit {
cancellables.forEach { $0.cancel() }
cancellables.removeAll()

Task {
await ScribbleLabApp().logger.log(
"SignInViewModel succesfully deinitialized - SCRG-S0"
)
}
}
}
103 changes: 103 additions & 0 deletions ScribbleLab/Core/Authentication/Models/SignInWithGoogleViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// SignInWithGoogleViewModel.swift
// ScribbleLab · Authentication
//
// Copyright (c) 2024 ScribbleLabApp LLC. - All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Foundation
import Firebase
import FirebaseAuth
import GoogleSignIn
import ScribbleFoundation

class SignInWithGoogleViewModel: ObservableObject {
@Published var isLogInSucceded: Bool = false

// swiftlint:disable:next function_body_length
func proceedWithGoogle() {
guard let clientID = FirebaseApp.app()?.options.clientID else {
Task {
await ScribbleLabApp().logger.error(
"FIR/GID Client ID is not available. GID-E115"
)
}
return
}

let config = GIDConfiguration(clientID: clientID)
GIDSignIn.sharedInstance.configuration = config

GIDSignIn.sharedInstance.signIn(
withPresenting: Application_Utility.rootViewController
) { user, error in
if let error = error {
Task {
await ScribbleLabApp().logger.error(
"Failed to present GID OAuth sheet with error \(error.localizedDescription) - GID-E110"
)
}
return
}

guard let user = user?.user, let idToken = user.idToken else {
Task {
await ScribbleLabApp().logger.error(
"User or ID Token is not available. GID-E116"
)
}
return
}

let accessToken = user.accessToken
let credential = GoogleAuthProvider.credential(
withIDToken: idToken.tokenString,
accessToken: accessToken.tokenString
)

Auth.auth().signIn(with: credential) { res, error in
if let error = error {
Task {
await ScribbleLabApp().logger.error(
"Failed to sign in using GID with error \(error.localizedDescription) - GID-E118"
)
}
return
}

guard let user = res?.user else {
Task {
await ScribbleLabApp().logger.error(
"User is not available after sign-in. - GID-E119")
}
return
}
print("User signed in: \(user.uid) - SCID-S0")
}
}
}
}
59 changes: 59 additions & 0 deletions ScribbleLab/Core/Authentication/Models/SignUpViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// SignUpViewModel.swift
// ScribbleLab · Authentication
//
// Copyright (c) 2024 ScribbleLabApp LLC. - All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Combine
import Foundation
import Firebase
import FirebaseAuth
import ScribbleFoundation

class SignUpViewModel: ObservableObject {

@Published var email: String = ""
@Published var password: String = ""
@Published var username: String = ""

/// Published property determining if the sign-up button is enabled.
///
/// The button is enabled when the entered password contains at least 8 characters,
/// the email contains an '@', and the username is not empty.
///
@Published var isSignUpButtonEnabled = false

private var cancellables = Set<AnyCancellable>()



init() {}


deinit {}
}
17 changes: 17 additions & 0 deletions ScribbleLab/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FirebaseAutomaticScreenReportingEnabled</key>
<false/>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.googleusercontent.apps.357715374736-ptifl9j6ranh8fvgq9ue3d6oivq864v8</string>
</dict>
</array>
</dict>
</plist>

0 comments on commit 00c7530

Please sign in to comment.