Flutter
...
Third party Authentication
在 Parse 中为 Flutter 应用集成 Apple 登录的技术指南
9 分
在parse上使用flutter进行apple登录 介绍 parse server支持第三方认证。 在本指南中,您将学习如何在parse上为您的flutter应用程序支持apple登录。 先决条件 要完成本教程,您需要: https //flutter dev/docs/get started/install https //developer android com/studio 或 https //code visualstudio com/ (带有 https //docs flutter dev/get started/editor dart 和 flutter) 一个创建并连接到 back4app 的 flutter 应用。 注意: 请遵循 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建 parse 应用。 付费会员资格,加入 https //developer apple com/programs/ 在项目中设置插件 sign in with apple sign in with apple 一台运行 ios 的设备(不是模拟器)。 目标 在 parse 服务器上的 flutter 应用中使用 apple 登录 1 将“使用 apple 登录”功能添加到您的 ios 基础项目 打开 ios/runner xcworkspace ios/runner xcworkspace 在 xcode 中 检查插件 sign in with apple sign in with apple 的设置说明 使用 apple 登录 使用 apple 登录 在您的 ios 项目中 选择 团队 团队 用于项目。 保存并关闭 xcode 2 在开发者门户中配置应用程序 id 登录到您的 https //developer apple com/ 并转到 标识符 标识符 部分。 检查您创建的 bundle identifier bundle identifier 是否存在 点击 bundle identifier bundle identifier 并向下滚动。检查 使用 apple 登录 使用 apple 登录 是否被选中 点击 编辑 编辑 并确保 启用为主应用程序 id 启用为主应用程序 id 被选中 如果一切正常,请保存并退出。 3 为 apple 设置 parse auth 访问 back4app 网站,登录后找到您的应用程序。之后,点击 服务器设置 服务器设置 并搜索 apple 登录 apple 登录 块并选择 设置 设置 apple 登录 apple 登录 部分看起来是这样的 现在,您只需将您的 bundle id bundle id 粘贴到下面的字段中,然后点击按钮保存 如果在集成 apple 登录时遇到任何问题,请通过聊天与我们的团队联系! 4 添加使用 apple 登录 现在您已经设置好项目,我们可以获取用户数据并登录到 parse 根据文档,我们必须发送一个包含用户认证数据的 map 1 final credential = await signinwithapple getappleidcredential( 2 scopes \[ 3 appleidauthorizationscopes email, 4 appleidauthorizationscopes fullname, 5 ], 6 ); 7 8 //https //docs parseplatform org/parse server/guide/#apple authdata 9 //according to the documentation, we must send a map with user authentication data 10 //make sign in with apple 11 final parseresponse = await parseuser loginwith('apple', 12 apple(credential identitytoken!, credential useridentifier!)); 5 从 flutter 使用 apple 登录 现在让我们用一个示例来展示如何在flutter应用中使用apple登录,界面非常简单。 打开你的flutter项目,进入main dart文件,清理所有代码,并用以下内容替换它: 1 import 'package\ flutter/material dart'; 2 import 'package\ parse server sdk flutter/parse server sdk dart'; 3 import 'package\ sign in with apple/sign in with apple dart'; 4 5 void main() async { 6 widgetsflutterbinding ensureinitialized(); 7 8 final keyapplicationid = 'your app id here'; 9 final keyclientkey = 'your client key here'; 10 final keyparseserverurl = 'https //parseapi back4app com'; 11 12 await parse() initialize(keyapplicationid, keyparseserverurl, 13 clientkey keyclientkey, debug true); 14 15 runapp(myapp()); 16 } 17 18 class myapp extends statelesswidget { 19 future\<bool> hasuserlogged() async { 20 parseuser? currentuser = await parseuser currentuser() as parseuser?; 21 if (currentuser == null) { 22 return false; 23 } 24 //checks whether the user's session token is valid 25 final parseresponse? parseresponse = 26 await parseuser getcurrentuserfromserver(currentuser sessiontoken!); 27 28 if (parseresponse? success == null || !parseresponse! success) { 29 //invalid session logout 30 await currentuser logout(); 31 return false; 32 } else { 33 return true; 34 } 35 } 36 37 @override 38 widget build(buildcontext context) { 39 return materialapp( 40 title 'flutter sign in with apple', 41 theme themedata( 42 primaryswatch colors blue, 43 visualdensity visualdensity adaptiveplatformdensity, 44 ), 45 home futurebuilder\<bool>( 46 future hasuserlogged(), 47 builder (context, snapshot) { 48 switch (snapshot connectionstate) { 49 case connectionstate none 50 case connectionstate waiting 51 return scaffold( 52 body center( 53 child container( 54 width 100, 55 height 100, 56 child circularprogressindicator()), 57 ), 58 ); 59 default 60 if (snapshot hasdata && snapshot data!) { 61 return userpage(); 62 } else { 63 return homepage(); 64 } 65 } 66 }), 67 ); 68 } 69 } 70 71 class homepage extends statefulwidget { 72 @override 73 homepagestate createstate() => homepagestate(); 74 } 75 76 class homepagestate extends state\<homepage> { 77 @override 78 widget build(buildcontext context) { 79 return scaffold( 80 appbar appbar( 81 title const text('flutter sign in with apple'), 82 ), 83 body center( 84 child singlechildscrollview( 85 padding const edgeinsets all(8), 86 child column( 87 crossaxisalignment crossaxisalignment stretch, 88 children \[ 89 container( 90 height 200, 91 child image network( 92 'http //blog back4app com/wp content/uploads/2017/11/logo b4a 1 768x175 1 png'), 93 ), 94 center( 95 child const text('flutter on back4app', 96 style 97 textstyle(fontsize 18, fontweight fontweight bold)), 98 ), 99 sizedbox( 100 height 100, 101 ), 102 container( 103 height 50, 104 child elevatedbutton( 105 child const text('sign in with apple'), 106 onpressed () => dosigninapple(), 107 ), 108 ), 109 sizedbox( 110 height 16, 111 ), 112 ], 113 ), 114 ), 115 )); 116 } 117 118 void dosigninapple() async { 119 late parseresponse parseresponse; 120 try { 121 //set scope 122 final credential = await signinwithapple getappleidcredential( 123 scopes \[ 124 appleidauthorizationscopes email, 125 appleidauthorizationscopes fullname, 126 ], 127 ); 128 129 //https //docs parseplatform org/parse server/guide/#apple authdata 130 //according to the documentation, we must send a map with user authentication data 131 //make sign in with apple 132 parseresponse = await parseuser loginwith('apple', 133 apple(credential identitytoken!, credential useridentifier!)); 134 135 if (parseresponse success) { 136 final parseuser parseuser = await parseuser currentuser() as parseuser; 137 138 //additional information in user 139 if (credential email != null) { 140 parseuser emailaddress = credential email; 141 } 142 if (credential givenname != null && credential familyname != null) { 143 parseuser set\<string>( 144 'name', '${credential givenname} ${credential familyname}'); 145 } 146 parseresponse = await parseuser save(); 147 if (parseresponse success) { 148 message showsuccess( 149 context context, 150 message 'user was successfully with sign in apple!', 151 onpressed () async { 152 navigator pushandremoveuntil( 153 context, 154 materialpageroute(builder (context) => userpage()), 155 (route\<dynamic> route) => false, 156 ); 157 }); 158 } else { 159 message showerror( 160 context context, message parseresponse error! message); 161 } 162 } else { 163 message showerror( 164 context context, message parseresponse error! message); 165 } 166 } on exception catch (e) { 167 print(e tostring()); 168 message showerror(context context, message e tostring()); 169 } 170 } 171 } 172 173 class userpage extends statelesswidget { 174 future\<parseuser?> getuser() async { 175 return await parseuser currentuser() as parseuser?; 176 } 177 178 @override 179 widget build(buildcontext context) { 180 void douserlogout() async { 181 final currentuser = await parseuser currentuser() as parseuser; 182 var response = await currentuser logout(); 183 if (response success) { 184 message showsuccess( 185 context context, 186 message 'user was successfully logout!', 187 onpressed () { 188 navigator pushandremoveuntil( 189 context, 190 materialpageroute(builder (context) => homepage()), 191 (route\<dynamic> route) => false, 192 ); 193 }); 194 } else { 195 message showerror(context context, message response error! message); 196 } 197 } 198 199 return scaffold( 200 appbar appbar( 201 title text('flutter sign in with apple'), 202 ), 203 body futurebuilder\<parseuser?>( 204 future getuser(), 205 builder (context, snapshot) { 206 switch (snapshot connectionstate) { 207 case connectionstate none 208 case connectionstate waiting 209 return center( 210 child container( 211 width 100, 212 height 100, 213 child circularprogressindicator()), 214 ); 215 default 216 return padding( 217 padding const edgeinsets all(8 0), 218 child column( 219 crossaxisalignment crossaxisalignment stretch, 220 mainaxisalignment mainaxisalignment center, 221 children \[ 222 center( 223 child text( 224 'hello, ${snapshot data! get\<string>('name')}')), 225 sizedbox( 226 height 16, 227 ), 228 container( 229 height 50, 230 child elevatedbutton( 231 child const text('logout'), 232 onpressed () => douserlogout(), 233 ), 234 ), 235 ], 236 ), 237 ); 238 } 239 })); 240 } 241 } 242 243 class message { 244 static void showsuccess( 245 {required buildcontext context, 246 required string message, 247 voidcallback? onpressed}) { 248 showdialog( 249 context context, 250 builder (buildcontext context) { 251 return alertdialog( 252 title const text("success!"), 253 content text(message), 254 actions \<widget>\[ 255 new elevatedbutton( 256 child const text("ok"), 257 onpressed () { 258 navigator of(context) pop(); 259 if (onpressed != null) { 260 onpressed(); 261 } 262 }, 263 ), 264 ], 265 ); 266 }, 267 ); 268 } 269 270 static void showerror( 271 {required buildcontext context, 272 required string message, 273 voidcallback? onpressed}) { 274 showdialog( 275 context context, 276 builder (buildcontext context) { 277 return alertdialog( 278 title const text("error!"), 279 content text(message), 280 actions \<widget>\[ 281 new elevatedbutton( 282 child const text("ok"), 283 onpressed () { 284 navigator of(context) pop(); 285 if (onpressed != null) { 286 onpressed(); 287 } 288 }, 289 ), 290 ], 291 ); 292 }, 293 ); 294 } 295 } 在您的应用程序仪表板中找到您的应用程序 id 和客户端密钥凭据,网址为 https //www back4app com/ 在 main dart main dart 中更新您的代码,使用您在 back4app 中项目的 applicationid 和 clientkey 的值。 keyapplicationid = 应用程序 id 应用程序 id keyclientkey = 客户端密钥 客户端密钥 运行项目,应用程序将如图所示加载。 结论 在这个阶段,您可以在 back4app 上使用 apple 登录 flutter。