Skip to content

Commit

Permalink
Feature/Introduce PersonNameComponents for Google, Facebook, LinkedIn…
Browse files Browse the repository at this point in the history
… sign in (#33)

Co-authored-by: Yll Fejziu <[email protected]>
  • Loading branch information
EgzonArifi and yllfejziu authored Nov 15, 2024
1 parent f4e932d commit bb984d3
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 26 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
Tests:
runs-on: macos-14-xlarge
runs-on: macos-15-xlarge
steps:
- name: Cancel previous jobs
uses: styfle/[email protected]
Expand All @@ -21,8 +21,8 @@ jobs:
xcode-version: latest-stable

- name: Build project
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild build-for-testing -destination 'name=iPhone 14 Pro' -scheme 'PovioKitAuth-Package' | xcpretty
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild build-for-testing -destination 'name=iPhone 16 Pro,OS=18.0' -scheme 'PovioKitAuth-Package' | xcpretty

- name: Run tests
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild test-without-building -destination 'name=iPhone 14 Pro' -scheme 'PovioKitAuth-Package' | xcpretty
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild test-without-building -destination 'name=iPhone 16 Pro,OS=18.0' -scheme 'PovioKitAuth-Package' | xcpretty

8 changes: 1 addition & 7 deletions Sources/Apple/AppleAuthenticator+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ public extension AppleAuthenticator {

/// User full name represented by `givenName` and `familyName`
public var name: String? {
guard let givenName = nameComponents?.givenName else {
return nameComponents?.familyName
}
guard let familyName = nameComponents?.familyName else {
return givenName
}
return "\(givenName) \(familyName)"
nameComponents?.name
}
}

Expand Down
42 changes: 42 additions & 0 deletions Sources/Core/PersonNameComponents+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// PersonNameComponents+Extension.swift
// PovioKitAuth
//
// Created by Egzon Arifi on 09/11/2024.
//

import AuthenticationServices
import Foundation

public extension PersonNameComponents {
var name: String? {
guard let givenName else {
return familyName
}
guard let familyName else {
return givenName
}
return "\(givenName) \(familyName)"
}
}

public extension PersonNameComponents {
init(
namePrefix: String? = .none,
middleName: String? = .none,
givenName: String? = .none,
familyName: String? = .none,
nameSuffix: String? = .none,
nickname: String? = .none,
phoneticRepresentation: PersonNameComponents? = .none
) {
self.init()
self.namePrefix = namePrefix
self.familyName = familyName
self.middleName = middleName
self.givenName = givenName
self.nameSuffix = nameSuffix
self.nickname = nickname
self.phoneticRepresentation = phoneticRepresentation
}
}
15 changes: 6 additions & 9 deletions Sources/Facebook/FacebookAuthenticator+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ public extension FacebookAuthenticator {
struct Response {
public let userId: String
public let token: String
public let name: String?
public let nameComponents: PersonNameComponents?
public let email: String?
public let expiresAt: Date

/// User full name represented by `givenName` and `familyName`
public var name: String? {
nameComponents?.name
}
}

struct GraphResponse: Decodable {
Expand All @@ -36,11 +41,3 @@ public extension FacebookAuthenticator {
}
}
}

public extension FacebookAuthenticator.GraphResponse {
var displayName: String {
[firstName, lastName]
.compactMap { $0 }
.joined(separator: " ")
}
}
5 changes: 4 additions & 1 deletion Sources/Facebook/FacebookAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ private extension FacebookAuthenticator {
let authResponse = Response(
userId: object.id,
token: token.tokenString,
name: object.displayName,
nameComponents: PersonNameComponents(
givenName: object.firstName,
familyName: object.lastName
),
email: object.email,
expiresAt: token.expirationDate
)
Expand Down
8 changes: 7 additions & 1 deletion Sources/Google/GoogleAuthenticator+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
//

import Foundation
import PovioKitAuthCore

public extension GoogleAuthenticator {
struct Response {
public let userId: String?
public let idToken: String?
public let accessToken: String
public let refreshToken: String
public let name: String?
public let nameComponents: PersonNameComponents?
public let email: String?
public let expiresAt: Date?

/// User full name represented by `givenName` and `familyName`
public var name: String? {
nameComponents?.name
}
}
}
5 changes: 4 additions & 1 deletion Sources/Google/GoogleAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ private extension GIDGoogleUser {
idToken: idToken?.tokenString,
accessToken: accessToken.tokenString,
refreshToken: refreshToken.tokenString,
name: profile?.name,
nameComponents: PersonNameComponents(
givenName: profile?.givenName,
familyName: profile?.familyName
),
email: profile?.email,
expiresAt: accessToken.expirationDate
)
Expand Down
7 changes: 6 additions & 1 deletion Sources/LinkedIn/LinkedInAuthenticator+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public extension LinkedInAuthenticator {
struct Response {
public let userId: String
public let token: String
public let name: String
public let nameComponents: PersonNameComponents
public let email: String
public let expiresAt: Date

/// User full name represented by `givenName` and `familyName`
public var name: String? {
nameComponents.name
}
}
}
6 changes: 4 additions & 2 deletions Sources/LinkedIn/LinkedInAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ extension LinkedInAuthenticator: Authenticator {

storage.set(true, forKey: storageIsAuthenticatedKey)

let name = "\(profileResponse.localizedFirstName) \(profileResponse.localizedLastName)"
return Response(
userId: profileResponse.id,
token: authResponse.accessToken,
name: name,
nameComponents: PersonNameComponents(
givenName: profileResponse.localizedFirstName,
familyName: profileResponse.localizedLastName
),
email: emailResponse.emailAddress,
expiresAt: authResponse.expiresIn
)
Expand Down

0 comments on commit bb984d3

Please sign in to comment.