title: 微信登录

本篇文档介绍在 Wilddog Auth 中如何使用微信对用户进行身份认证。

前期准备

  1. 在控制面板中创建应用。请参考 控制面板-创建应用
  2. 微信开放平台管理中心,获取应用的 AppIDAppSecret
  3. 在 微信开放平台—网站应用—网站信息 中填写回调域名 auth.wilddog.com
  4. 在 控制面板 身份认证—登录方式 中打开微信登录方式,配置微信帐号 AppIDAppSecret

实现微信登录

1.安装 Wilddog Auth SDK:

  1. <script type="text/javascript" src="https://cdn.wilddog.com/sdk/js/2.5.6/wilddog-auth.js"></script>

2.创建 Wilddog Auth 实例:

  1. var config = {
  2. authDomain: "<appId>.wilddog.com"
  3. };
  4. wilddog.initializeApp(config);

3.Wilddog Auth 提供多种方式进行微信认证,你可以任选其一:

  • popup
  1. var provider = new wilddog.auth.WeixinAuthProvider();
  2. wilddog.auth().signInWithPopup(provider).then(function (user) {
  3. console.log(user);
  4. }).catch(function (error) {
  5. // 错误处理
  6. console.log(error);
  7. // ...
  8. });
  • redirect
  1. var provider = new wilddog.auth.WeixinAuthProvider();
  2. var auth = wilddog.auth();
  3. // 直接使用 signInWithRedirect 会造成重复登录。
  4. auth.onAuthStateChanged(function (user) {
  5. if (user == null) {
  6. auth.signInWithRedirect(provider).then(function (user) {
  7. console.log(user);
  8. }).catch(function (error) {
  9. // 错误处理
  10. console.log(error);
  11. // ...
  12. });
  13. } else {
  14. console.log(user);
  15. }
  16. })

更多认证绑定方式,请参考 API 文档

退出登录

signOut 方法用于用户退出登录:

  1. wilddog.auth().signOut().then(function() {
  2. // 退出成功
  3. console.log("sign-out")
  4. }).catch(function(error) {
  5. // 发生错误
  6. console.log("sign-out-error")
  7. });

更多使用

  • 通过 Wilddog.auth().currentUser() 获取当前用户并管理用户。详情请参考 用户管理
  • Wilddog Auth 可以将你的应用与 Wilddog Sync 无缝集成:使用微信登录后,Wilddog Auth 将给用户生成 Wilddog ID。Wilddog ID 结合 规则表达式,可以控制 Wilddog Sync 的用户访问权限。