【小程序】小程序认证服务接入流程分享
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))
});
},
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh