我正在使用Parse Server來託管我的應用程序,並且目前使用用戶名和密碼以及Facebook進行身份驗證。我也想使用Google。似乎沒有指導如何做到這一點,所以任何幫助將不勝感激。如何在iOS上的Parse Server上實施Google登錄?
我熟悉使用適用於iOS的Google SignIn SDK,但不會將其與Parse集成。
謝謝。
我正在使用Parse Server來託管我的應用程序,並且目前使用用戶名和密碼以及Facebook進行身份驗證。我也想使用Google。似乎沒有指導如何做到這一點,所以任何幫助將不勝感激。如何在iOS上的Parse Server上實施Google登錄?
我熟悉使用適用於iOS的Google SignIn SDK,但不會將其與Parse集成。
謝謝。
在服務器上啓用OAuth的(將在futher更新被棄用,改爲權威性)
oauth: {
google: true
},
在客戶端創建一個AuthDelegate
class AuthDelegate : NSObject, PFUserAuthenticationDelegate {
func restoreAuthentication(withAuthData authData: [String : String]?) -> Bool {
return true
}
}
使用谷歌SDK來簽到,並得到UserID和AccessToken
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
print(user.userID)
print(user.authentication.accessToken)
print(user.authentication.accessTokenExpirationDate)
}
設置驗證數據通過驗證
PFUser.logInWithAuthType(inBackground: "google", authData: authd)
let authd : [String: String] = ["id":"\(user.userID!)","expiration_date":"\(user.authentication.accessTokenExpirationDate!)","access_token":"\(gToken!)"]
登錄在解析請注意,您必須考慮續約的accessToken自己(客戶端或服務器端)
這裏是我的版本實現谷歌拍在與中分析服務器。
import UIKit
import Parse
import KVNProgress
import GoogleSignIn
class GoogleAuthWorker: NSObject {
/// http://nsdateformatter.com
lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
return formatter
}()
var googleSignIn = GIDSignIn.sharedInstance()
var completion: (Error?) -> Void = { }
weak var host: UIViewController?
/// Logs in with Google. Presents Safari view controller from the host view controller.
func logInWithGoogle(host: UIViewController?, completion: @escaping (Error?) -> Void) {
self.completion = completion
self.host = host
PFUser.register(self, forAuthType: "google")
googleSignIn?.delegate = self
googleSignIn?.uiDelegate = self
googleSignIn?.signIn()
}
}
// MARK: - PFUserAuthenticationDelegate
extension GoogleAuthWorker: PFUserAuthenticationDelegate {
func restoreAuthentication(withAuthData authData: [String : String]?) -> Bool {
// Check whether expiration date is in future or not.
guard let expirationDateString = authData?["expiration_date"],
let expirationDate = dateFormatter.date(from: expirationDateString) else { return false }
return expirationDate > Date()
}
}
// MARK: - GIDSignInDelegate
extension GoogleAuthWorker: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
guard let user = user else {
let appError = AppError.googleError(underlyingError: error)
self.completion(.failure(error: appError))
return
}
let authData: [String: String] = ["id": user.userID,
"id_token": user.authentication.idToken,
"expiration_date": "\(user.authentication.accessTokenExpirationDate!)",
"access_token": user.authentication.accessToken]
// Ask PFUser to log in with given auth data.
PFUser.logInWithAuthType(inBackground: "google", authData: authData).continue({ (task) -> Any? in
if let userObject = task.result {
// Fill userObject (which is PFUser) by profile data, like:
//userObject.email = user.profile.email
//userObject.password = UUID().uuidString
//userObject["firstName"] = user.profile.givenName
//userObject["lastName"] = user.profile.familyName
self.completion(nil)
} else {
// Failed to log in.
self.completion(task.error)
}
return nil
})
}
}
// MARK: - GIDSignInUIDelegate
extension GoogleAuthWorker: GIDSignInUIDelegate {
func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
}
func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
host?.present(viewController, animated: true, completion: nil)
}
func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
host?.dismiss(animated: true, completion: nil)
}
}
嘿迪倫,你整理了一下嗎? –