登录认证
app.auth()
接口描述
返回 Auth 对象
签名:auth({ persistence: string }): Auth
输入参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| persistence | string | 否 | 身份认证状态如何持久保留,有三个选项 local、session 和 none,默认为 session。 |
local:在显式退出登录之前的 30 天内保留身份验证状态session:在窗口关闭时清除身份验证状态none:在页面重新加载时清除身份验证状态
示例代码
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "xxxx-yyy",
});
const auth = app.auth({
persistence: "local", //用户显式退出或更改密码之前的30天一直有效
});
Auth
Auth.currentUser
接口描述
返回表示当前用户的 User 实例
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
// 执行某种登录之后...
const user = app.auth().currentUser;
Auth.getCurrenUser()
自 1.8.x 版本起接口更名为 Auth.getCurrentUser
接口描述
Auth.currentUser的异步操作,返回表示当前用户的 User 实例
签名:getCurrenUser(): Promise<User | null>
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
// 执行某种登录之后...
app
.auth()
.getCurrenUser()
.then((user) => {
// ...
});
Auth.hasLoginState()
接口描述
返回当前登录状态 LoginState,如果未登录,则返回 null
签名:hasLoginState(): LoginState | null
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
const loginState = app.auth().hasLoginState();
if (loginState) {
// 登录态有效
} else {
// 没有登录态,或者登录态已经失效
}
Auth.getLoginState()
接口描述
Auth.hasLoginState()的异步操作,返回当前登录状态 LoginState,如果未登录,则返回 null
签名:getLoginState(): Promise<LoginState | null>
此 API 是 hasLoginState 的异步模式,适用于本地存储为异步的平台,比如 React Native
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
const loginState = app
.auth()
.getLoginState()
.then((loginState) => {
if (loginState) {
// 登录态有效
} else {
// 没有登录态,或者登录态已经失效
}
});
Auth.weixinAuthProvider()
接口描述
获取用于微信登录的 WeixinAuthProvider
签名:weixinAuthProvider({ appid: string, scope: string }): WeixinAuthProvider
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| appid | string | 是 | 微信公众平台(或开放平台)应用的 appid。 |
| scope | string | 是 | 网页授权类型,可选值为 snsapi_base(公众平台,只获取用户的 openid)、snsapi_userinfo(公众平台,获取用户的基本信息)和 snsapi_login(开放平台网页授权)。 |
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
const provider = app.auth().weixinAuthProvider({
appid: "your-appid",
scope: "snsapi_base",
});
provider.getRedirectResult().then((loginState) => {
if (loginState) {
// 登录成功,本地已存在登录态
} else {
// 未登录,唤起微信登录
provider.signInWithRedirect();
}
});
Auth.customAuthProvider()
接口描述
获取用于自定义登录的 CustomAuthProvider
签名:customAuthProvider(): CustomAuthProvider
示例代码
// 将 your-envId 替换成您的环境 ID
const app = cloudbase.init({
env: "your-envId",
});
async function login() {
// 获取自定义登录 ticket
// 将 your-api 替换成获取 ticket 的 URL
const ticket = await fetch("your-api");
app
.auth({
persistence: "session",
})
.customAuthProvider()
.signIn(ticket)
.then(() => {
// 登录成功
})
.catch((err) => {
// 登录失败
});
}
login();
Auth.anonymousAuthProvider()
接口描述
获取用于匿名登录的 AnonymousAuthProvider
签名:anonymousAuthProvider(): AnonymousAuthProvider
示例代码
const app = cloudbase.init({
env: "xxxx-yyy",
});
app
.auth({
persistence: "session",
})
.anonymousAuthProvider()
.signIn()
.then(() => {
// 登录成功
})
.catch((err) => {
// 登录失败
});
Auth.signUpWithEmailAndPassword()
接口描述
使用邮箱和密码注册一个云开发账户,调用后,会自动向注册邮箱发送邮箱验证邮件。
签名:signUpWithEmailAndPassword(email: string, password: string): Promise<void>