title: 自定义身份认证
本篇文档介绍在 Wilddog Auth 中如何使用自定义身份认证。
原理

前期准备
- 在控制面板中创建应用。请参考 控制面板—创建应用。
- 在 控制面板—身份认证—登录方式—超级秘钥 中获取超级密钥。
实现自定义身份认证
1.安装 Wilddog Auth SDK:
将以下 pod 包含在你的 Podfile 中:
pod 'Wilddog/Auth'
安装 SDK:
$ pod install
2.创建 Wilddog Auth 实例:
Objective-C
Swift
objectivec
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<your-wilddog-appid>.wilddogio.com"];
[WDGApp configureWithOptions:option];
swift
let options = WDGOptions.init(syncURL: "https://<your-wilddog-appid>.wilddogio.com")
WDGApp.configure(with: options)
3.当用户成功登录你的用户系统时,服务器通过 Server SDK 生成 Custom Token,并返回给客户端。
4.客户端收到 Custom Token 后,使用 signInWithCustomToken: 方法进行认证:
Objective-C
Swift
objectivec
WDGAuth *auth = [WDGAuth auth];
[auth signInWithCustomToken:customToken
completion:^(WDGUser *_Nullable user, NSError *_Nullable error) {
// ...
}];
swift
let auth = WDGAuth.auth()
auth?.signIn(withCustomToken: customToken) { (user, error) in
// ...
}
退出登录
signOut: 方法用于用户退出登录:
Objective-C
Swift
objectivec
NSError *error;
[[WDGAuth auth] signOut:&error];
if (!error) {
// 退出登录成功
}
swift
try! WDGAuth.auth()!.signOut()
更多使用
- 通过
[WDGAuth auth].currentUser 获取当前用户并管理用户。详情请参考 用户管理。