Flutter社交账号授权登录插件fiction_social_auth的使用
Flutter社交账号授权登录插件fiction_social_auth的使用
概述
Fiction Social Auth 是一个由 Fiction Developers 开发的社交账号认证助手。更多详细信息,请访问 fictiondevelopers.com/auth-system。
我们知道,在开发 Flutter 应用时,管理社交账号登录是一件繁琐的事情。你必须在开发者账户上设置很多凭证,之后几乎不可能将这些凭证转移到客户的账户或记住配置了哪个项目/账户。因此,我们推荐你使用这个包来简化你的工作,只需不到五分钟即可完成!
注意: 我们不会保存用户的任何数据,所有数据都会发送到你的应用并从我们的会话中立即清除,所以你不必担心隐私问题。
功能检查表
- [✅] Google (已实现)
- ❌ Facebook (开发中)
- ❌ Apple (计划中)
- [✅] GitHub (已实现) - 几乎实现了,但有时用户名会生成显示名和邮箱
- [✅] LinkedIn (已实现) - 请注意“id”,我返回它,但不确定是否正确
- ❌ Twitter (计划中)
- ❌ 其他 (计划中)
安装
- 在
pubspec.yaml
文件中添加此包的最新版本(然后运行dart pub get
):
dependencies:
fiction_social_auth: ^0.0.1
- 导入包:
import 'package:fiction_social_auth/fiction_social_auth.dart';
Android 配置
在 AndroidManifest.xml
中添加以下内容:
<application>
<!-- 其他配置 -->
<activity
android:name="com.linusu.flutter_web_auth_2.CallbackActivity"
android:exported="true">
<intent-filter android:label="flutter_web_auth_2">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="social-auth-scheme" />
</intent-filter>
</activity>
</application>
示例代码
以下是一个完整的示例代码,展示了如何使用 fiction_social_auth
插件进行社交账号授权登录。
import 'package:flutter/material.dart';
import 'package:fiction_social_auth/fiction_social_auth.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Fiction Social Auth")),
body: SizedBox(
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
// 创建 FictionSocialAuth 实例
FictionSocialAuth socialAuth = FictionSocialAuth();
// 使用 Google 进行授权登录
UserData? user = await socialAuth.socialAuth(AuthType.google);
// 打印用户数据
print("UserData: ${user?.toJson()}");
},
child: Text("Google"),
),
const SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
// 创建 FictionSocialAuth 实例
FictionSocialAuth socialAuth = FictionSocialAuth();
// 使用 GitHub 进行授权登录
UserData? user = await socialAuth.socialAuth(AuthType.github);
// 打印用户数据
print("UserData: ${user?.toJson()}");
},
child: Text("GitHub"),
),
const SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
// 创建 FictionSocialAuth 实例
FictionSocialAuth socialAuth = FictionSocialAuth();
// 使用 LinkedIn 进行授权登录
UserData? user = await socialAuth.socialAuth(AuthType.linkedin);
// 打印用户数据
print("UserData: ${user?.toJson()}");
},
child: Text("LinkedIn"),
),
]),
),
),
);
}
}
更多关于Flutter社交账号授权登录插件fiction_social_auth的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter社交账号授权登录插件fiction_social_auth的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
fiction_social_auth
是一个 Flutter 插件,用于简化社交账号授权登录的流程。它支持多种社交平台,如 Facebook、Google、Twitter 等。以下是如何使用 fiction_social_auth
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 fiction_social_auth
插件的依赖:
dependencies:
flutter:
sdk: flutter
fiction_social_auth: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 配置社交平台
在使用 fiction_social_auth
之前,你需要在各个社交平台上创建应用并获取相应的 API Key 或 Client ID。
2.1 Facebook 配置
- 在 Facebook 开发者平台 上创建应用。
- 获取
App ID
和App Secret
。 - 在
AndroidManifest.xml
和Info.plist
中进行相应的配置。
2.2 Google 配置
- 在 Google Cloud Console 上创建项目并启用 Google Sign-In。
- 获取
Client ID
。 - 在
AndroidManifest.xml
和Info.plist
中进行相应的配置。
2.3 Twitter 配置
- 在 Twitter Developer 上创建应用。
- 获取
API Key
和API Secret Key
。
3. 初始化插件
在你的 Flutter 应用中初始化 fiction_social_auth
插件:
import 'package:fiction_social_auth/fiction_social_auth.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化插件
await FictionSocialAuth.initialize(
facebookAppId: 'YOUR_FACEBOOK_APP_ID',
googleClientId: 'YOUR_GOOGLE_CLIENT_ID',
twitterApiKey: 'YOUR_TWITTER_API_KEY',
twitterApiSecretKey: 'YOUR_TWITTER_API_SECRET_KEY',
);
runApp(MyApp());
}
4. 实现登录功能
你可以通过调用 FictionSocialAuth
的各种方法来实现不同社交平台的登录:
import 'package:fiction_social_auth/fiction_social_auth.dart';
class LoginPage extends StatelessWidget {
Future<void> _loginWithFacebook() async {
try {
final user = await FictionSocialAuth.loginWithFacebook();
print('Facebook User: $user');
} catch (e) {
print('Facebook Login Error: $e');
}
}
Future<void> _loginWithGoogle() async {
try {
final user = await FictionSocialAuth.loginWithGoogle();
print('Google User: $user');
} catch (e) {
print('Google Login Error: $e');
}
}
Future<void> _loginWithTwitter() async {
try {
final user = await FictionSocialAuth.loginWithTwitter();
print('Twitter User: $user');
} catch (e) {
print('Twitter Login Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Social Login'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _loginWithFacebook,
child: Text('Login with Facebook'),
),
ElevatedButton(
onPressed: _loginWithGoogle,
child: Text('Login with Google'),
),
ElevatedButton(
onPressed: _loginWithTwitter,
child: Text('Login with Twitter'),
),
],
),
),
);
}
}
5. 处理用户信息
fiction_social_auth
插件返回的用户信息通常包含用户的 ID、姓名、电子邮件、头像等。你可以根据这些信息来处理用户的登录和注册逻辑。
6. 登出
如果你需要实现登出功能,可以调用 FictionSocialAuth.logout()
方法:
Future<void> _logout() async {
await FictionSocialAuth.logout();
print('User logged out');
}