Flutter云开发认证插件cloudbase_auth_nullsafety的使用
Flutter云开发认证插件cloudbase_auth_nullsafety的使用
Cloudbase Auth Nullsafety for Flutter
⚠️ 特别注意(warning)
目前所有空安全版本均为测试版本,请勿在生产使用。
腾讯云·云开发的 Flutter空安全(民间改制) 插件,更多的云开发 Flutter 插件请见云开发文档。
💔 iOS可能遇到的错误
Include of non-modular header inside framework module
- fix:
Targets
->Build Settings
-> SearchAllow No
-> SettingAllow Non-modular Includes In Framework Modules: Yes
安装
在 flutter 项目的 pubspec.yaml
文件的 dependencies
中添加
dependencies:
cloudbase_core_nullsafety: ^0.0.3
cloudbase_auth_nullsafety: ^0.0.4
然后执行以下命令安装依赖:
flutter pub get
简单示例
以下是使用 cloudbase_auth_nullsafety
插件进行用户认证的基本示例:
import 'package:cloudbase_core_nullsafety/cloudbase_core_nullsafety.dart';
import 'package:cloudbase_auth_nullsafety/cloudbase_auth_nullsafety.dart';
void main() async {
// 初始化 CloudBase
CloudBaseCore core = CloudBaseCore.init({
// 填写你的云开发环境ID
'env': 'your-env-id'
});
// 获取登录对象
CloudBaseAuth auth = CloudBaseAuth(core);
// 获取当前登录状态
CloudBaseAuthState authState = await auth.getAuthState();
// 如果未登录,唤起匿名登录
if (authState == null) {
try {
CloudBaseAuthSuccess success = await auth.signInAnonymously();
print('匿名登录成功: $success');
} catch (error) {
print('匿名登录失败: $error');
}
}
// 微信登录
try {
await auth.signInByWx(
wxAppId: 'Your wxAppId',
wxUniLink: 'Your wxUniLink',
);
print('微信登录成功');
} catch (error) {
print('微信登录失败: $error');
}
// 绑定微信登录
try {
await auth.linkWithWeChat(
wxAppId: 'Your wxAppId',
wxUniLink: 'Your wxUniLink',
withUnionId: false,
);
print('绑定微信登录成功');
} catch (error) {
print('绑定微信登录失败: $error');
}
// 使用自定义票据登录
try {
await auth.signInWithTicket(ticket: 'Your custom login ticket');
print('自定义登录成功');
} catch (error) {
print('自定义登录失败: $error');
}
// 绑定自定义登录
try {
await auth.linkWithTicket(ticket: 'Your custom login ticket');
print('绑定自定义登录成功');
} catch (error) {
print('绑定自定义登录失败: $error');
}
// 发送短信验证码
try {
await auth.sendPhoneCode(phone: 'Your phone number');
print('短信验证码发送成功');
} catch (error) {
print('短信验证码发送失败: $error');
}
// 短信登录
try {
CloudBaseAuthSuccess loginSuccess = await auth.signInWithPhone(
phone: 'Your phone number',
code: 'Your sms code',
);
print('短信登录成功: $loginSuccess');
} catch (error) {
print('短信登录失败: $error');
}
// 短信注册
try {
CloudBaseAuthSuccess signUpSuccess = await auth.signUpWithPhone(
phone: 'Your phone number',
code: 'Your sms code',
);
print('短信注册成功: $signUpSuccess');
} catch (error) {
print('短信注册失败: $error');
}
// 强制重置密码
try {
await auth.forceResetPwd(
phone: 'Your phone number',
code: 'Your sms code',
passwd: 'Your password',
);
print('密码重置成功');
} catch (error) {
print('密码重置失败: $error');
}
}
更多关于Flutter云开发认证插件cloudbase_auth_nullsafety的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter云开发认证插件cloudbase_auth_nullsafety的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
cloudbase_auth_nullsafety
是一个用于 Flutter 的云开发认证插件,支持空安全(null safety)。它允许开发者轻松集成腾讯云开发(CloudBase)的认证功能,包括手机号登录、邮箱登录、第三方登录等。
以下是如何在 Flutter 项目中使用 cloudbase_auth_nullsafety
插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 cloudbase_auth_nullsafety
插件的依赖:
dependencies:
flutter:
sdk: flutter
cloudbase_core_nullsafety: ^1.0.0
cloudbase_auth_nullsafety: ^1.0.0
然后运行 flutter pub get
来安装依赖。
2. 初始化 CloudBase
在使用 cloudbase_auth_nullsafety
之前,需要先初始化 CloudBase
核心模块。
import 'package:cloudbase_core_nullsafety/cloudbase_core_nullsafety.dart';
import 'package:cloudbase_auth_nullsafety/cloudbase_auth_nullsafety.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 CloudBase
CloudBaseCore core = CloudBaseCore.init({
'env': 'your-env-id' // 替换为你的云开发环境ID
});
// 初始化 CloudBaseAuth
CloudBaseAuth auth = CloudBaseAuth(core);
runApp(MyApp(auth: auth));
}
3. 使用 CloudBaseAuth 进行认证
CloudBaseAuth
提供了多种认证方式,以下是一些常见的用法示例:
3.1 匿名登录
void anonymousLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInAnonymously();
print('匿名登录成功: ${authState.userId}');
} catch (e) {
print('匿名登录失败: $e');
}
}
3.2 手机号登录
void phoneLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithPhone(
phone: 'your-phone-number', // 替换为你的手机号
code: 'your-verification-code' // 替换为你的验证码
);
print('手机号登录成功: ${authState.userId}');
} catch (e) {
print('手机号登录失败: $e');
}
}
3.3 邮箱登录
void emailLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithEmail(
email: 'your-email', // 替换为你的邮箱
password: 'your-password' // 替换为你的密码
);
print('邮箱登录成功: ${authState.userId}');
} catch (e) {
print('邮箱登录失败: $e');
}
}
3.4 第三方登录
void thirdPartyLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithProvider(
provider: 'weixin', // 替换为你的第三方登录提供商,如 'weixin', 'qq', 'github' 等
redirectUri: 'your-redirect-uri' // 替换为你的重定向URI
);
print('第三方登录成功: ${authState.userId}');
} catch (e) {
print('第三方登录失败: $e');
}
}
4. 获取当前用户信息
你可以通过 getAuthState
方法获取当前用户的认证状态:
void getCurrentUser(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.getAuthState();
if (authState != null) {
print('当前用户ID: ${authState.userId}');
} else {
print('用户未登录');
}
} catch (e) {
print('获取用户信息失败: $e');
}
}
5. 登出
你可以通过 signOut
方法登出当前用户:
void logout(CloudBaseAuth auth) async {
try {
await auth.signOut();
print('登出成功');
} catch (e) {
print('登出失败: $e');
}
}
6. 处理认证状态变化
你可以监听认证状态的变化,以便在用户登录或登出时执行相应的操作:
void listenAuthState(CloudBaseAuth auth) {
auth.onAuthStateChanged.listen((CloudBaseAuthState authState) {
if (authState != null) {
print('用户已登录: ${authState.userId}');
} else {
print('用户已登出');
}
});
}
7. 完整示例
以下是一个完整的示例,展示了如何在 Flutter 应用中使用 cloudbase_auth_nullsafety
插件:
import 'package:flutter/material.dart';
import 'package:cloudbase_core_nullsafety/cloudbase_core_nullsafety.dart';
import 'package:cloudbase_auth_nullsafety/cloudbase_auth_nullsafety.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 CloudBase
CloudBaseCore core = CloudBaseCore.init({
'env': 'your-env-id' // 替换为你的云开发环境ID
});
// 初始化 CloudBaseAuth
CloudBaseAuth auth = CloudBaseAuth(core);
runApp(MyApp(auth: auth));
}
class MyApp extends StatelessWidget {
final CloudBaseAuth auth;
MyApp({required this.auth});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('CloudBase Auth Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => anonymousLogin(auth),
child: Text('匿名登录'),
),
ElevatedButton(
onPressed: () => phoneLogin(auth),
child: Text('手机号登录'),
),
ElevatedButton(
onPressed: () => emailLogin(auth),
child: Text('邮箱登录'),
),
ElevatedButton(
onPressed: () => thirdPartyLogin(auth),
child: Text('第三方登录'),
),
ElevatedButton(
onPressed: () => getCurrentUser(auth),
child: Text('获取当前用户'),
),
ElevatedButton(
onPressed: () => logout(auth),
child: Text('登出'),
),
],
),
),
),
);
}
}
void anonymousLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInAnonymously();
print('匿名登录成功: ${authState.userId}');
} catch (e) {
print('匿名登录失败: $e');
}
}
void phoneLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithPhone(
phone: 'your-phone-number', // 替换为你的手机号
code: 'your-verification-code' // 替换为你的验证码
);
print('手机号登录成功: ${authState.userId}');
} catch (e) {
print('手机号登录失败: $e');
}
}
void emailLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithEmail(
email: 'your-email', // 替换为你的邮箱
password: 'your-password' // 替换为你的密码
);
print('邮箱登录成功: ${authState.userId}');
} catch (e) {
print('邮箱登录失败: $e');
}
}
void thirdPartyLogin(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.signInWithProvider(
provider: 'weixin', // 替换为你的第三方登录提供商,如 'weixin', 'qq', 'github' 等
redirectUri: 'your-redirect-uri' // 替换为你的重定向URI
);
print('第三方登录成功: ${authState.userId}');
} catch (e) {
print('第三方登录失败: $e');
}
}
void getCurrentUser(CloudBaseAuth auth) async {
try {
CloudBaseAuthState authState = await auth.getAuthState();
if (authState != null) {
print('当前用户ID: ${authState.userId}');
} else {
print('用户未登录');
}
} catch (e) {
print('获取用户信息失败: $e');
}
}
void logout(CloudBaseAuth auth) async {
try {
await auth.signOut();
print('登出成功');
} catch (e) {
print('登出失败: $e');
}
}