Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions DevLog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DFABA3B02E23526500FEFBDB /* FirebaseFirestore in Frameworks */ = {isa = PBXBuildFile; productRef = DFABA3AF2E23526500FEFBDB /* FirebaseFirestore */; };
DFABA3B22E23526500FEFBDB /* FirebaseFunctions in Frameworks */ = {isa = PBXBuildFile; productRef = DFABA3B12E23526500FEFBDB /* FirebaseFunctions */; };
DFABA3B42E23526500FEFBDB /* FirebaseMessaging in Frameworks */ = {isa = PBXBuildFile; productRef = DFABA3B32E23526500FEFBDB /* FirebaseMessaging */; };
DFD3B68D2F8F1FB8001DA7CD /* Nexa in Frameworks */ = {isa = PBXBuildFile; productRef = DFD3B68C2F8F1FB8001DA7CD /* Nexa */; };
DFD645402EC827A10073E133 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = DFD6453F2EC827A10073E133 /* .gitignore */; };
DFD74E2F2E423EA700613803 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = DFD74E2E2E423EA700613803 /* README.md */; };
DFF2DACE2EDC02AD00778738 /* OrderedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = DFF2DACD2EDC02AD00778738 /* OrderedCollections */; };
Expand Down Expand Up @@ -76,6 +77,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DFD3B68D2F8F1FB8001DA7CD /* Nexa in Frameworks */,
DFABA3B42E23526500FEFBDB /* FirebaseMessaging in Frameworks */,
DFABA3A92E2351EE00FEFBDB /* GoogleSignInSwift in Frameworks */,
DFABA3B22E23526500FEFBDB /* FirebaseFunctions in Frameworks */,
Expand Down Expand Up @@ -172,6 +174,7 @@
DFABA3B12E23526500FEFBDB /* FirebaseFunctions */,
DFABA3B32E23526500FEFBDB /* FirebaseMessaging */,
DFF2DACD2EDC02AD00778738 /* OrderedCollections */,
DFD3B68C2F8F1FB8001DA7CD /* Nexa */,
);
productName = SwiftUI_DevLog;
productReference = DFD48B002DC4D6E2005905C5 /* DevLog.app */;
Expand Down Expand Up @@ -211,6 +214,7 @@
DFABA3AA2E23526500FEFBDB /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */,
DF66A07B2EA52E970098E643 /* XCRemoteSwiftPackageReference "SwiftLint" */,
DFF2DACC2EDC02AD00778738 /* XCRemoteSwiftPackageReference "swift-collections" */,
DFD3B68B2F8F1FB8001DA7CD /* XCRemoteSwiftPackageReference "Nexa" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = DFD48B012DC4D6E2005905C5 /* Products */;
Expand Down Expand Up @@ -610,6 +614,14 @@
minimumVersion = 11.15.0;
};
};
DFD3B68B2F8F1FB8001DA7CD /* XCRemoteSwiftPackageReference "Nexa" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/opficdev/Nexa";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.1.0;
};
};
DFF2DACC2EDC02AD00778738 /* XCRemoteSwiftPackageReference "swift-collections" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-collections.git";
Expand Down Expand Up @@ -666,6 +678,11 @@
package = DFABA3AA2E23526500FEFBDB /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseMessaging;
};
DFD3B68C2F8F1FB8001DA7CD /* Nexa */ = {
isa = XCSwiftPackageProductDependency;
package = DFD3B68B2F8F1FB8001DA7CD /* XCRemoteSwiftPackageReference "Nexa" */;
productName = Nexa;
};
DFF2DACD2EDC02AD00778738 /* OrderedCollections */ = {
isa = XCSwiftPackageProductDependency;
package = DFF2DACC2EDC02AD00778738 /* XCRemoteSwiftPackageReference "swift-collections" */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 22 additions & 36 deletions DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ import FirebaseAuth
import FirebaseFirestore
import FirebaseFunctions
import FirebaseMessaging
import Nexa

final class GithubAuthenticationService: NSObject, AuthenticationService {
private enum FunctionName: String {
case requestGithubTokens
case revokeGithubAccessToken
}

private enum GitHubAPI {
static let baseURL = URL(string: "https://api.github.com")!
static let acceptHeader = "application/vnd.github.v3+json"
}
Comment thread
opficdev marked this conversation as resolved.

private let store = Firestore.firestore()
private let functions = Functions.functions(region: "asia-northeast3")
private let messaging = Messaging.messaging()
private var user: User? { Auth.auth().currentUser }
private let providerID = AuthProviderID.gitHub
private let provider = TopViewControllerProvider()
private let logger = Logger(category: "GithubAuthService")
private let gitHubApiClient = NXAPIClient(
configuration: NXClientConfiguration(
baseURL: GitHubAPI.baseURL,
headers: ["Accept": GitHubAPI.acceptHeader]
)
)

func signIn() async throws -> AuthDataResponse {
logger.info("Starting GitHub sign in")
Expand Down Expand Up @@ -243,24 +255,11 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {

// GitHub API로 사용자 프로필 정보 가져오기
private func requestUserProfile(accessToken: String) async throws -> GitHubUser {
guard let url = URL(string: "https://api.github.com/user") else {
throw URLError(.badURL)
}

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.addValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")

let (data, response) = try await URLSession.shared.data(for: request)

guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}

let decoder = JSONDecoder()
let gitHubUser = try decoder.decode(GitHubUser.self, from: data)
let gitHubUser = try await gitHubApiClient
.get("/user", as: GitHubUser.self)
.header("Authorization", "Bearer \(accessToken)")
.validate(.statusCodes([200]))
.send()

if gitHubUser.email != nil {
return gitHubUser
Expand All @@ -276,24 +275,11 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
}

private func requestPrimaryVerifiedEmail(accessToken: String) async throws -> String? {
guard let url = URL(string: "https://api.github.com/user/emails") else {
throw URLError(.badURL)
}

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.addValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")

let (data, response) = try await URLSession.shared.data(for: request)

guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}

let decoder = JSONDecoder()
let gitHubEmails = try decoder.decode([GitHubEmail].self, from: data)
let gitHubEmails = try await gitHubApiClient
.get("/user/emails", as: [GitHubEmail].self)
.header("Authorization", "Bearer \(accessToken)")
.validate(.statusCodes([200]))
.send()

if let primaryVerifiedEmail = gitHubEmails.first(where: { $0.primary && $0.verified }) {
return primaryVerifiedEmail.email
Expand Down