【小程序】小程序认证服务接入流程分享

编程入门 行业动态 更新时间:2024-10-23 11:29:55

【小<a href=https://www.elefans.com/category/jswz/34/1771429.html style=程序】小程序认证服务接入流程分享"/>

【小程序】小程序认证服务接入流程分享

1、 agc网站创建web应用,并启用相关服务:

  • 创建web应用

    

  • 启用认证服务

  • 获取应用配置信息

    2、认证接口调用

    通过邮箱/手机号获取验证码:

    相关代码:

      //获取邮箱验证码getemailverifycode() {agconnect.auth().requestEmailVerifyCode(this.account,agconnect.auth.Action.ACTION_REGISTER_LOGIN,'zh_CN', //发送验证码的语言120) //发送间隔时间.then(ret => {//验证码申请成功console.log("email success")}).catch(error => {//验证码申请失败console.log("email fail",error)});},//获取手机验证码getPhoneVerifyCode() {agconnect.auth().requestPhoneVerifyCode('86',this.account,agconnect.auth.Action.ACTION_REGISTER_LOGIN,'zh_CN', //发送验证码的语言90).then(ret => {//验证码申请成功console.log("success")}).catch(error => {//验证码申请失败    console.log("fail")});},

    使用手机号码/邮箱注册账号并登录:

    相关代码:

      //创建邮箱用户createmailuser() {let emailUser = new agconnect.auth.EmailUser(this.account, this.password, this.code);agconnect.auth().createEmailUser(emailUser).then(user => {//创建用户成功console.log(" create success11111")this.getuserinfo()this.code = ''}).catch(error => {//创建用户失败console.log("create fail11111", error)});},//创建手机用户createphoneuser() {let phoneUser = new agconnect.auth.PhoneUser('86', this.account, this.password, this.code);agconnect.auth().createPhoneUser(phoneUser).then(user => {//创建用户成功console.log(" create success")this.getuserinfo()this.code = ''}).catch(error => {//创建用户失败console.log("create fail", error)});},

    使用密码/验证码登录进行认证:

    相关代码:

      //登录邮箱账号loginwithemail() {console.log(this.code)let credential;if (this.code) {credential = agconnect.auth.EmailAuthProvider.credentialWithVerifyCode(this.account, this.password, this.code);console.log(1111)} else {credential = agconnect.auth.EmailAuthProvider.credentialWithPassword(this.account, this.password);console.log(2222)}agconnect.auth().signIn(credential).then(user => {//登录成功console.log("login success")this.getuserinfo()}).catch(error => {//登录失败console.log("login fail", error)});},//登录手机账号loginwithphone() {console.log(this.code)let credential;if (this.code) {credential = agconnect.auth.PhoneAuthProvider.credentialWithVerifyCode('86', this.account, this.password, this.code);console.log(1111)} else {credential = agconnect.auth.PhoneAuthProvider.credentialWithPassword('86', this.account, this.password);console.log(2222)}agconnect.auth().signIn(credential).then(user => {//登录成功console.log("login success")this.getuserinfo()}).catch(error => {//登录失败console.log("login fail", error)});},

    匿名登录并进行关联账号:

    相关代码:

      //匿名登录loginAnonymously(){agconnect.auth().signInAnonymously().then(user => {//登录成功this.getuserinfo()}).catch(error => {//登录失败});},//关联账号linkaccount(){if (this.checkIsPhoneNumber(this.account)) {agconnect.auth().getCurrentUser().then(user => {if (!user) {console.error('no user login');return;}let credential = agconnect.auth.PhoneAuthProvider.credentialWithVerifyCode("86", this.account, this.password, this.code);user.link(credential).then(res => {this.getuserinfo();})}).catch((e) => {console.error(JSON.stringify(e))});} else {agconnect.auth().getCurrentUser().then(user => {if (!user) {console.error('no user login');return;}let credential = agconnect.auth.EmailAuthProvider.credentialWithVerifyCode(this.account, this.password, this.code);user.link(credential).then(res => {this.getuserinfo();})}).catch((e) => {console.error(JSON.stringify(e))});}},

    更改绑定的手机号/邮箱:

    相关代码:

    //修改账号updateuser(){agconnect.auth().getCurrentUser().then(user=>{if(user){//业务逻辑if (this.checkIsPhoneNumber(this.account)) {console.log("updatephone")user.updatePhone('86', this.account, this.code, 'zh_CN');} else {user.updateEmail( this.account, this.code, 'zh_CN');console.log("updatemail")}this. getuserinfo()}});},

    修改/重置账号密码:

    相关代码:

  //获取修改/重置密码验证码getnewVerifycode() {console.log(console.log("getcode"))if (this.checkIsPhoneNumber(this.account)) {console.log("this is phone num")agconnect.auth().requestPhoneVerifyCode('86',this.account,agconnect.auth.Action.ACTION_RESET_PASSWORD,'zh_CN', //发送验证码的语言90).then(ret => {//验证码申请成功console.log("success")}).catch(error => {//验证码申请失败    console.log("fail")});} else {agconnect.auth().requestEmailVerifyCode(this.account,agconnect.auth.Action.ACTION_RESET_PASSWORD,'zh_CN', //发送验证码的语言120) //发送间隔时间.then(ret => {//验证码申请成功console.log("email success")}).catch(error => {//验证码申请失败console.log("email fail",error)});console.log("this is email")}},//修改密码updatepwd(){agconnect.auth().getCurrentUser().then(user=>{if(user){//业务逻辑if (this.checkIsPhoneNumber(this.account)) {console.log("updatephonepwd")user.updatePassword(this.password, this.code, 11);} else {user.updatePassword(this.password, this.code, 12);console.log("updatemailpwd")}}});console.log(this.password, this.code)},//重置密码reset(){if (this.checkIsPhoneNumber(this.account)) {console.log("resetPasswordByPhone")agconnect.auth().resetPasswordByPhone('86', this.account, this.password, this.code);} else {agconnect.auth().resetPasswordByEmail(this.account, this.password, this.code);console.log("resetPasswordByEmail")}},

账号退出与注销:

相关代码:

  //退出账号quit() {agconnect.auth().signOut().then(() => {//登出成功this.account = ''this.password = ''this.code = ''this.userInfo = ''this.setData({account : '',password : '',code : '',userInfo :'',})console.log("quit success")}).catch(error => {//登出失败console.log("quit fail")});},//注销账号delete() {agconnect.auth().deleteUser();this.setData({account : '',password : '',code : '',})this.account = ''this.password = ''this.code = ''},

账号重认证:

相关代码:

//重认证reauth(){if (this.checkIsPhoneNumber(this.account)) {let credential = '';if(verifyCode){credential = agconnect.auth.PhoneAuthProvider.credentialWithVerifyCode('86', this.account, this.password, this.code);}else{credential = agconnect.auth.PhoneAuthProvider.credentialWithPassword('86', this.account, this.password);}agconnect.auth().getCurrentUser().then(async user => {if(user){agconnect.auth().userReauthenticate(credential);}});console.log("userReauthenticatephone")} else {let credential = '';if (verifyCode) {credential = agconnect.auth.EmailAuthProvider.credentialWithVerifyCode(this.account, this.password, this.code);} else {credential = agconnect.auth.EmailAuthProvider.credentialWithPassword(this.account, this.password);}agconnect.auth().getCurrentUser().then(async user => {if(user){agconnect.auth().userReauthenticate(credential);}});console.log("userReauthenticatemail")}},

自有账号认证

代码如下:

//自有账号  ziyouaccount() {agconnect.auth().getCurrentUser().then(user => {if (!user) {console.error('no user login');return;}let credential = agconnect.auth.SelfBuildAuthProvider.credentialWithToken(token, autoCreateUser);if (!credential) {return Promise.reject('credential is undefined');}else{agconnect.auth().signIn(credential).then((res) => {//业务逻辑console.log("sign in success")}).catch((err) => {return Promise.reject('sign in failed');});}}).catch((e) => {console.error(JSON.stringify(e))});},

欲了解更多更全技术文章,欢迎访问/?ha_source=zzh​​​​​​​

更多推荐

【小程序】小程序认证服务接入流程分享

本文发布于:2024-03-04 02:07:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707975.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   流程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!