本篇文档介绍在 Wilddog Auth 中如何使用手机号和密码对用户进行身份认证。
前期准备
- 在控制面板中创建应用。请参考 控制面板-创建应用。
- 在控制面板 身份认证—登录方式 中打开手机号登录方式。
创建用户
用手机号密码创建一个新用户,需完成以下步骤:
1.安装 Wilddog Auth SDK:
将以下 pod 包含在你的 Podfile 中:
安装 SDK:
2.创建 Wilddog Auth 实例:
Objective-C
Swift
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<your-wilddog-appid>.wilddogio.com"]; [WDGApp configureWithOptions:option];
|
let options = WDGOptions.init(syncURL: "https://<your-wilddog-appid>.wilddogio.com") WDGApp.configure(with: options)
|
3.使用 createUserWithPhone:password:completion: 方法创建新用户:
Objective-C
Swift
[[WDGAuth auth] createUserWithPhone:@"18888888888" password:@"password123" completion:^(WDGUser * _Nullable user, NSError * _Nullable error) { }];
|
WDGAuth.auth()?.createUser(withPhone: "18888888888", password: "password123") { (user, error) in }
|
注意:
如果新用户创建成功,默认会处于登录状态,并且你可以在回调方法中获取登录用户。
登录用户
1.安装 Wilddog Auth SDK:
将以下 pod 包含在你的 Podfile 中:
安装 SDK:
2.创建 Wilddog Auth 实例:
Objective-C
Swift
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<your-wilddog-appid>.wilddogio.com"]; [WDGApp configureWithOptions:option];
|
let options = WDGOptions.init(syncURL: "https://<your-wilddog-appid>.wilddogio.com") WDGApp.configure(with: options)
|
3.将该用户的手机号和密码传递到 signInWithPhone:password:completion:,即可在你应用中登录此用户:
Objective-C
Swift
[[WDGAuth auth] signInWithPhone:@"18888888888" password:@"password123" completion:^(WDGUser * _Nullable user, NSError * _Nullable error) { }];
|
WDGAuth.auth()?.signIn(withPhone: "18888888888", password: "password123") { (user, error) in }
|
注意:
如果用户成功登录,你可以在回调方法中获取登录用户。
验证用户手机号
1.发送验证用户的手机验证码:
Objective-C
Swift
[user sendPhoneVerificationWithCompletion:^(NSError * _Nullable error) { }];
|
user?.sendPhoneVerification { error in }
|
2.确认验证用户手机验证码:
Objective-C
Swift
[user verifyPhoneWithSmsCode:realSms completion:^(NSError * _Nullable error) { }];
|
user?.verifyPhone(withSmsCode: realSms){ error in }
|
退出登录
signOut: 方法用于用户退出登录:
Objective-C
Swift
NSError *error; [[WDGAuth auth] signOut:&error]; if (!error) { }
|
try! WDGAuth.auth()!.signOut()
|
更多使用
- 通过
[WDGAuth auth].currentUser 获取当前用户并管理用户。详情请参考 用户管理。