How to authenticate a user in Parse iOS SDK using OAuth?

前端 未结 1 703
梦谈多话
梦谈多话 2020-12-16 05:53

Parse Server offers OAuth authentication. How can I use the Parse Server\'s predefined OAuth modules, e.g. Facebook, to sign up a new user or login an existing user of the \

1条回答
  •  囚心锁ツ
    2020-12-16 06:27

    1, create a class extends from both NSObject and PFUserAuthenticationDelegate.

    class AuthDelegate:NSObject, PFUserAuthenticationDelegate {
        func restoreAuthenticationWithAuthData(authData: [String : String]?) -> Bool {
            return true
        }
    }
    

    2, register this authentication delegate

    // parmeter 'forAuthType' is the name of file defined in 
    // https://github.com/parse-community/parse-server/blob/master/src/Adapters/Auth/index.js
    // such as: google, github, linkedin ......
    PFUser.registerAuthenticationDelegate(AuthDelegate(), forAuthType: "google")
    

    3, using PFUser.logInWithAuthTypeInBackground method to store user info to _User

    // for google oauth, authData format will be
    // ["id":"PUT_USER_ID_HERE","accesstoken":"PUT_TOKEN_HERE"]
    PFUser.logInWithAuthType(inBackground: "google", authData:[.....])
    

    4, you will see a record is created in _User, with only objectId and authData

    0 讨论(0)
提交回复
热议问题