This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
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.
Merge pull request #89 from mytiki/feat/Implemment-Google-OAuth-logic
Feat/implemment google o auth logic
- Loading branch information
Showing
18 changed files
with
734 additions
and
53 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
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 |
---|---|---|
|
@@ -29,4 +29,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: 96d93e7c36e6d9cd116be19ddc3938e717dc0264 | ||
|
||
COCOAPODS: 1.14.3 | ||
COCOAPODS: 1.15.2 |
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,38 @@ | ||
/* | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public struct Account: Hashable { | ||
|
||
public var username: String | ||
public var provider: AccountProvider | ||
public var status: AccountStatus = .unverified | ||
|
||
public static func toAccount(accounts: [AuthEmailTokenResponse]) -> [Account]{ | ||
var accoutsReturn: [Account] = [] | ||
for account in accounts { | ||
accoutsReturn.append(Account(username: account.email, provider: AccountProvider.toAccountProvider(provider: account.provider) ?? AccountProvider.email(.google))) | ||
} | ||
return accoutsReturn | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(username) | ||
hasher.combine(provider) | ||
} | ||
|
||
public static func == (lhs: Account, rhs: Account) -> Bool { | ||
lhs.username == rhs.username && | ||
lhs.provider == rhs.provider | ||
} | ||
|
||
public init(username: String, provider: AccountProvider) { | ||
self.username = username | ||
self.provider = provider | ||
self.status = EmailService.verifyStatus(email: username) | ||
} | ||
} |
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,39 @@ | ||
/* | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
|
||
import Foundation | ||
|
||
public enum AccountProvider: Hashable{ | ||
|
||
case email(EmailProviderEnum) | ||
|
||
func name() -> String{ | ||
var name = "" | ||
switch(self){ | ||
case .email(let emailEnum) : | ||
name = emailEnum.rawValue | ||
} | ||
return name.replacingOccurrences(of: "_", with: " ").capitalized | ||
} | ||
|
||
static func all() -> [AccountProvider] { | ||
var providers: [AccountProvider] = [] | ||
EmailProviderEnum.allCases.forEach{ provider in | ||
providers.append(.email(provider)) | ||
} | ||
return providers | ||
} | ||
|
||
public static func toAccountProvider(provider: String) -> AccountProvider? { | ||
if(provider == "GOOGLE"){ | ||
return email(.google) | ||
} | ||
if(provider == "OUTLOOK"){ | ||
return email(.outlook) | ||
}else{ | ||
return email(.google) | ||
} | ||
} | ||
} |
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,139 @@ | ||
/* | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// # AccountService | ||
/// | ||
/// The `AccountService` class manages user accounts, providing methods for retrieving account | ||
/// information, handling logins, and managing sessions. | ||
/// | ||
/// ## Overview | ||
/// | ||
/// The `AccountService` class is responsible for maintaining a collection of 3rd party accounts, | ||
/// allowing for account retrieval based on providers and performing login and logout operations. | ||
/// | ||
/// ## Example | ||
/// | ||
/// To use the `AccountService` class, follow the example below: | ||
/// | ||
/// ```swift | ||
/// let accountService = AccountService() | ||
/// | ||
/// // Retrieve all accounts | ||
/// let allAccounts = accountService.accounts() | ||
/// | ||
/// // Retrieve accounts for a specific provider | ||
/// let facebookAccounts = accountService.accounts(for: .facebook) | ||
/// | ||
/// // Retrieve available account providers | ||
/// let availableProviders = accountService.providers() | ||
public class AccountService{ | ||
// MARK: - Properties | ||
|
||
/// An array containing the user accounts managed by the service. | ||
var _accounts: [Account] = EmailService.accounts() | ||
|
||
// MARK: - Public Methods | ||
|
||
/// Retrieves all user accounts stored in the service. | ||
/// | ||
/// - Returns: An array of user accounts. | ||
func accounts() -> [Account] { | ||
_accounts = EmailService.accounts() | ||
return _accounts | ||
} | ||
|
||
/// Retrieves user accounts associated with a specific `AccountProvider`. | ||
/// | ||
/// - Parameter provider: The account provider for which accounts should be retrieved. | ||
/// - Returns: An array of user accounts for the specified provider. | ||
func accounts(for provider: AccountProvider) -> [Account] { | ||
accounts() | ||
var accounts: [Account] = [] | ||
_accounts.forEach { acc in | ||
if acc.provider == provider { accounts.append(acc) } | ||
} | ||
return accounts | ||
} | ||
|
||
/// Retrieves available `AccountProvider`, excluding those already associated with existing accounts. | ||
/// | ||
/// - Returns: An array of available account providers. | ||
func providers() -> [AccountProvider] { | ||
var providers = AccountProvider.all() | ||
_accounts.forEach { acc in | ||
let index = providers.firstIndex(where: { | ||
acc.provider.name() == $0.name() | ||
}) | ||
} | ||
return providers | ||
} | ||
|
||
/// Logs in a user by creating a new account or verifying an existing one. | ||
/// | ||
/// - Parameters: | ||
/// - username: The username for the account. | ||
/// - password: The password for the account. | ||
/// - provider: The account provider for the login. | ||
/// - Returns: The logged-in user account. | ||
/// - Throws: An error indicating issues with the login process, such as empty credentials or an already linked account. | ||
func login(username: String, password: String, provider: AccountProvider) throws -> Account { | ||
if username.isEmpty || password.isEmpty { | ||
throw TikiError.error("Username and password should not be empty.") | ||
} else { | ||
if _accounts.first(where: { | ||
$0.username == username && | ||
$0.provider == provider && | ||
$0.status == .verified | ||
}) != nil { | ||
throw TikiError.error("Account already linked.") | ||
} else { | ||
let account = Account(username: username, provider: provider) | ||
var accountType: AccountType? | ||
switch(provider){ | ||
case .email(let emailEnum): | ||
accountType = .email(emailEnum) | ||
break | ||
} | ||
_accounts.append(Account(username: username, provider: provider)) | ||
|
||
return account | ||
} | ||
} | ||
} | ||
|
||
/// Logs out a user by removing the associated account. | ||
/// | ||
/// - Parameters: | ||
/// - username: The username of the account to be logged out. | ||
/// - provider: The account provider of the account to be logged out. | ||
/// - Returns: The logged-out user account. | ||
/// - Throws: An error indicating issues with the logout process, such as an empty username or an account not found. | ||
func logout(username: String, provider: AccountProvider) -> Account { | ||
if (username.isEmpty) { | ||
print("Username should not be empty.") | ||
|
||
} | ||
let index = _accounts.firstIndex(where: { | ||
$0.username == username && | ||
$0.provider == provider | ||
}) | ||
if (index == nil) { | ||
print("Account not found.") | ||
} | ||
let account = _accounts[index!] | ||
var accountType: AccountType? | ||
_accounts.remove(at: index!) | ||
switch(provider){ | ||
case .email(let emailEnum): | ||
accountType = .email(emailEnum) | ||
EmailService.logout(email: username) | ||
break | ||
} | ||
return account | ||
} | ||
|
||
} |
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,11 @@ | ||
/* | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
|
||
import Foundation | ||
|
||
public enum AccountStatus{ | ||
case verified | ||
case unverified | ||
} |
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,11 @@ | ||
/* | ||
* AccountTypeEnum Enum | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
import Foundation | ||
|
||
/// An enumeration representing different types of user accounts. | ||
public enum AccountType { | ||
case email(EmailProviderEnum) | ||
} |
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,22 @@ | ||
/* | ||
* Copyright (c) TIKI Inc. | ||
* MIT license. See LICENSE file in the root directory. | ||
*/ | ||
|
||
import Foundation | ||
|
||
public struct AuthEmailTokenResponse: Codable { | ||
public let auth: String | ||
public let refresh: String | ||
public let provider: String | ||
public let email: String | ||
public let expiration: Date? | ||
|
||
public init(auth: String, refresh: String, provider: String, email: String, expiration: Date?) { | ||
self.auth = auth | ||
self.refresh = refresh | ||
self.provider = provider | ||
self.email = email | ||
self.expiration = expiration | ||
} | ||
} |
Oops, something went wrong.