在React Native中实现Apple登录与Parse集成
9 分
使用 apple 登录 react native 介绍 在上一个教程中,您使用 \<font color="#2166ae">parse user\</font> 类为您的应用程序构建了用户登录/注销功能。现在您将学习如何使用 apple 登录从 apple 检索用户数据并登录、注册或将现有用户与之关联。您还将安装和配置 \<font color="#2166ae">react native apple authentication\</font> 库来实现这一点。 \<font color="#2166ae">parse user linkwith\</font> 方法负责使用任何第三方身份验证方法注册和登录用户,只要您传递每个不同提供者所请求的正确参数。在将用户数据链接到新的或现有的 \<font color="#2166ae">parse user\</font> 后,parse 将在您的设备上存储有效的用户会话。未来对 \<font color="#2166ae">currentasync\</font> 等方法的调用将成功检索您的用户数据,就像常规登录一样。 您可以随时访问我们 github 仓库中构建的完整 android 项目 kotlin 示例仓库 https //github com/templates back4app/android parse sdk kotlin java 示例仓库 https //github com/templates back4app/android parse sdk java 前提条件 要完成本教程,您需要: 一个创建的 react native 应用,并且 连接到 back4app https //www back4app com/docs/react native/parse sdk/react native sdk 完成之前的指南,以便更好地理解 parse 用户类 https //www back4app com/docs/react native/parse sdk/working with users/react native login 目标 使用 apple 登录在 parse 上为 react native 应用构建用户登录功能。 1 安装依赖 在 react native 中启用 apple 登录的最流行方法是使用 \<font color="#2166ae">react native apple authentication\</font> 来处理它。由于该库的配置取决于您的开发环境、目标平台和偏好,请按照 官方文档 https //github com/invertase/react native apple authentication 进行设置。 如果您正在为 android 开发,您还需要安装 https //github com/auth0/jwt decode 库来解码 apple jwt 令牌。 注意: 请确保彻底遵循 xcode 环境的初始设置说明,在 apple 开发者门户上创建您的应用 id、密钥和服务 id。 2 使用 apple 登录与 parse 现在让我们在 \<font color="#2166ae">userlogin\</font> 组件中创建一个新方法,调用 apple 登录认证模态框。 \<font color="#2166ae">react native apple authentication\</font> 库有两个独立的模块来处理此调用,具体取决于您的用户平台,因此您需要在 ios 上使用 \<font color="#2166ae">appleauth performrequest\</font> ,在 android 上使用 \<font color="#2166ae">appleauthandroid signin\</font> 。如果用户使用 apple 登录,此调用将从 apple 检索用户数据,您需要存储 \<font color="#2166ae">id\</font> , \<font color="#2166ae">token\</font> 和 apple 邮箱以备后用。 javascript 1 const douserloginapple = async function () promise\<boolean> { 2 try { 3 let response = {}; 4 let appleid = ''; 5 let appletoken = ''; 6 let appleemail = ''; 7 if (platform os === 'ios') { 8 // performs login request requesting user email 9 response = await appleauth performrequest({ 10 requestedoperation appleauth operation login, 11 requestedscopes \[appleauth scope email], 12 }); 13 // on ios, user id and email are easily retrieved from request 14 appleid = response user; 15 appletoken = response identitytoken; 16 appleemail = response email; 17 } else if (platform os === 'android') { 18 // configure the request 19 appleauthandroid configure({ 20 // the service id you registered with apple 21 clientid 'your service id', 22 // return url added to your apple dev console 23 redirecturi 'your redirect url', 24 responsetype appleauthandroid responsetype all, 25 scope appleauthandroid scope all, 26 }); 27 response = await appleauthandroid signin(); 28 // decode user id and email from token returned from apple, 29 // this is a common workaround for apple sign in via web api 30 const decodedidtoken = jwt decode(response id token); 31 appleid = decodedidtoken sub; 32 appletoken = response id token; 33 appleemail = decodedidtoken email; 34 } 35 // format authdata to provide correctly for apple linkwith on parse 36 const authdata = { 37 id appleid, 38 token appletoken, 39 }; 40 } catch (error) { 41 // error can be caused by wrong parameters or lack of internet connection 42 alert alert('error!', error); 43 return false; 44 } 45 };1 const douserloginapple = async function () promise\<boolean> { 2 try { 3 let response object = {}; 4 let appleid string = ''; 5 let appletoken string = ''; 6 let appleemail string = ''; 7 if (platform os === 'ios') { 8 // performs login request requesting user email 9 response = await appleauth performrequest({ 10 requestedoperation appleauth operation login, 11 requestedscopes \[appleauth scope email], 12 }); 13 // on ios, user id and email are easily retrieved from request 14 appleid = response user; 15 appletoken = response identitytoken; 16 appleemail = response email; 17 } else if (platform os === 'android') { 18 // configure the request 19 appleauthandroid configure({ 20 // the service id you registered with apple 21 clientid 'your service id', 22 // return url added to your apple dev console 23 redirecturi 'your service url', 24 responsetype appleauthandroid responsetype all, 25 scope appleauthandroid scope all, 26 }); 27 response = await appleauthandroid signin(); 28 // decode user id and email from token returned from apple, 29 // this is a common workaround for apple sign in via web api 30 const decodedidtoken object = jwt decode(response id token); 31 appleid = decodedidtoken sub; 32 appletoken = response id token; 33 appleemail = decodedidtoken email; 34 } 35 // format authdata to provide correctly for apple linkwith on parse 36 const authdata object = { 37 id appleid, 38 token appletoken, 39 }; 40 } catch (error any) { 41 // error can be caused by wrong parameters or lack of internet connection 42 alert alert('error!', error); 43 return false; 44 } 45 }; 请注意,对于android,您需要解码来自apple的返回令牌,因为该库 \<font color="#2166ae">react native apple authentication\</font> 使用apple sign in网页api进行身份验证。使用此方法时,数据访问存在限制,因此获取用户id和电子邮件的常见解决方法是通过此解码过程,如官方parse指南中所述 这里 https //docs parseplatform org/parse server/guide/#apple authdata 。 之后,您可以使用 \<font color="#2166ae">parse user linkwith\</font> 在一个新的 \<font color="#2166ae">parse user\</font> 对象上注册新用户并登录。请注意,如果您的用户已经使用此 apple 认证注册, \<font color="#2166ae">linkwith\</font> 将使用现有账户登录他。 javascript 1 const douserloginapple = async function () promise\<boolean> { 2 try { 3 let response = {}; 4 let appleid = ''; 5 let appletoken = ''; 6 let appleemail = ''; 7 if (platform os === 'ios') { 8 // performs login request requesting user email 9 response = await appleauth performrequest({ 10 requestedoperation appleauth operation login, 11 requestedscopes \[appleauth scope email], 12 }); 13 // on ios, user id and email are easily retrieved from request 14 appleid = response user; 15 appletoken = response identitytoken; 16 appleemail = response email; 17 } else if (platform os === 'android') { 18 // configure the request 19 appleauthandroid configure({ 20 // the service id you registered with apple 21 clientid 'your service io', 22 // return url added to your apple dev console 23 redirecturi 'your service url', 24 responsetype appleauthandroid responsetype all, 25 scope appleauthandroid scope all, 26 }); 27 response = await appleauthandroid signin(); 28 // decode user id and email from token returned from apple, 29 // this is a common workaround for apple sign in via web api 30 const decodedidtoken = jwt decode(response id token); 31 appleid = decodedidtoken sub; 32 appletoken = response id token; 33 appleemail = decodedidtoken email; 34 } 35 // format authdata to provide correctly for apple linkwith on parse 36 const authdata = { 37 id appleid, 38 token appletoken, 39 }; 40 // log in or sign up on parse using this apple credentials 41 let usertologin = new parse user(); 42 // set username and email to match provider email 43 usertologin set('username', appleemail); 44 usertologin set('email', appleemail); 45 return await usertologin 46 linkwith('apple', { 47 authdata authdata, 48 }) 49 then(async (loggedinuser) => { 50 // login returns the corresponding parseuser object 51 alert alert( 52 'success!', 53 `user ${loggedinuser get('username')} has successfully signed in!`, 54 ); 55 // to verify that this is in fact the current user, currentasync can be used 56 const currentuser = await parse user currentasync(); 57 console log(loggedinuser === currentuser); 58 // navigation navigate takes the user to the screen named after the one 59 // passed as parameter 60 navigation navigate('home'); 61 return true; 62 }) 63 catch(async (error) => { 64 // error can be caused by wrong parameters or lack of internet connection 65 alert alert('error!', error message); 66 return false; 67 }); 68 } catch (error) { 69 // error can be caused by wrong parameters or lack of internet connection 70 alert alert('error!', error); 71 return false; 72 } 73 };1 const douserloginapple = async function () promise\<boolean> { 2 try { 3 let response object = {}; 4 let appleid string = ''; 5 let appletoken string = ''; 6 let appleemail string = ''; 7 if (platform os === 'ios') { 8 // performs login request requesting user email 9 response = await appleauth performrequest({ 10 requestedoperation appleauth operation login, 11 requestedscopes \[appleauth scope email], 12 }); 13 // on ios, user id and email are easily retrieved from request 14 appleid = response user; 15 appletoken = response identitytoken; 16 appleemail = response email; 17 } else if (platform os === 'android') { 18 // configure the request 19 appleauthandroid configure({ 20 // the service id you registered with apple 21 clientid 'com back4app userguide', 22 // return url added to your apple dev console 23 redirecturi 'https //tuhl software/back4appuserguide/', 24 responsetype appleauthandroid responsetype all, 25 scope appleauthandroid scope all, 26 }); 27 response = await appleauthandroid signin(); 28 // decode user id and email from token returned from apple, 29 // this is a common workaround for apple sign in via web api 30 const decodedidtoken object = jwt decode(response id token); 31 appleid = decodedidtoken sub; 32 appletoken = response id token; 33 appleemail = decodedidtoken email; 34 } 35 // format authdata to provide correctly for apple linkwith on parse 36 const authdata object = { 37 id appleid, 38 token appletoken, 39 }; 40 // log in or sign up on parse using this apple credentials 41 let usertologin parse user = new parse user(); 42 // set username and email to match provider email 43 usertologin set('username', appleemail); 44 usertologin set('email', appleemail); 45 return await usertologin 46 linkwith('apple', { 47 authdata authdata, 48 }) 49 then(async (loggedinuser parse user) => { 50 // login returns the corresponding parseuser object 51 alert alert( 52 'success!', 53 `user ${loggedinuser get('username')} has successfully signed in!`, 54 ); 55 // to verify that this is in fact the current user, currentasync can be used 56 const currentuser parse user = await parse user currentasync(); 57 console log(loggedinuser === currentuser); 58 // navigation navigate takes the user to the screen named after the one 59 // passed as parameter 60 navigation navigate('home'); 61 return true; 62 }) 63 catch(async (error object) => { 64 // error can be caused by wrong parameters or lack of internet connection 65 alert alert('error!', error message); 66 return false; 67 }); 68 } catch (error any) { 69 // error can be caused by wrong parameters or lack of internet connection 70 alert alert('error!', error); 71 return false; 72 } 73 }; 将此功能添加到您的 \<font color="#2166ae">usersignin\</font> 组件,并将其分配给您的 apple 按钮 \<font color="#2166ae">onpress\</font> 参数。继续测试您的新功能。请注意,用户在成功注册和/或登录后将被重定向到您的主屏幕。 3 验证用户登录和会话创建 为了确保 apple 登录成功,您可以查看您的 parse 仪表板,查看您的新 \<font color="#2166ae">用户\</font> (如果您的 apple 认证数据不属于其他用户),包含 apple \<font color="#2166ae">authdata\</font> 参数。 您还可以在仪表板中验证是否创建了有效的会话,其中包含指向该 \<font color="#2166ae">用户\</font> 对象的指针。 4 将现有用户链接到 apple 登录 另一个 \<font color="#2166ae">linkwith\</font> 可能的用法是将现有用户与另一个身份验证提供者链接,在这种情况下是 apple。添加这个调用 \<font color="#2166ae">linkwith\</font> 的功能,方法与您在 \<font color="#2166ae">userlogin\</font> 中所做的相同,添加到您的 \<font color="#2166ae">hellouser\</font> 组件或直接添加到您的主屏幕。这里唯一的区别是,您将从已登录的用户对象中使用它,而不是从空的 \<font color="#2166ae">parse user\</font> 调用该方法。 javascript 1 const douserlinkapple = async function (){ 2 try { 3 let response = {}; 4 let appleid = ''; 5 let appletoken = ''; 6 if (platform os === 'ios') { 7 // performs login request requesting user email 8 response = await appleauth performrequest({ 9 requestedoperation appleauth operation login, 10 requestedscopes \[appleauth scope email], 11 }); 12 // on ios, user id and email are easily retrieved from request 13 appleid = response user; 14 appletoken = response identitytoken; 15 } else if (platform os === 'android') { 16 // configure the request 17 appleauthandroid configure({ 18 // the service id you registered with apple 19 clientid 'your service id', 20 // return url added to your apple dev console 21 redirecturi 'your redirect url', 22 responsetype appleauthandroid responsetype all, 23 scope appleauthandroid scope all, 24 }); 25 response = await appleauthandroid signin(); 26 // decode user id and email from token returned from apple, 27 // this is a common workaround for apple sign in via web api 28 const decodedidtoken = jwt decode(response id token); 29 appleid = decodedidtoken sub; 30 appletoken = response id token; 31 } 32 // format authdata to provide correctly for apple linkwith on parse 33 const authdata = { 34 id appleid, 35 token appletoken, 36 }; 37 let currentuser = await parse user currentasync(); 38 // link user with his apple credentials 39 return await currentuser 40 linkwith('apple', { 41 authdata authdata, 42 }) 43 then(async (loggedinuser) => { 44 // login returns the corresponding parseuser object 45 alert alert( 46 'success!', 47 `user ${loggedinuser get( 48 'username', 49 )} has successfully linked his apple account!`, 50 ); 51 // to verify that this is in fact the current user, currentasync can be used 52 currentuser = await parse user currentasync(); 53 console log(loggedinuser === currentuser); 54 return true; 55 }) 56 catch(async (error) => { 57 // error can be caused by wrong parameters or lack of internet connection 58 alert alert('error!', error message); 59 return false; 60 }); 61 } catch (error) { 62 // error can be caused by wrong parameters or lack of internet connection 63 alert alert('error!', error); 64 return false; 65 } 66 };1 const douserlinkapple = async function () promise\<boolean> { 2 try { 3 let response object = {}; 4 let appleid string = ''; 5 let appletoken string = ''; 6 if (platform os === 'ios') { 7 // performs login request requesting user email 8 response = await appleauth performrequest({ 9 requestedoperation appleauth operation login, 10 requestedscopes \[appleauth scope email], 11 }); 12 // on ios, user id and email are easily retrieved from request 13 appleid = response user; 14 appletoken = response identitytoken; 15 } else if (platform os === 'android') { 16 // configure the request 17 appleauthandroid configure({ 18 // the service id you registered with apple 19 clientid 'your service id', 20 // return url added to your apple dev console 21 redirecturi 'your service url', 22 responsetype appleauthandroid responsetype all, 23 scope appleauthandroid scope all, 24 }); 25 response = await appleauthandroid signin(); 26 // decode user id and email from token returned from apple, 27 // this is a common workaround for apple sign in via web api 28 const decodedidtoken object = jwt decode(response id token); 29 appleid = decodedidtoken sub; 30 appletoken = response id token; 31 } 32 // format authdata to provide correctly for apple linkwith on parse 33 const authdata object = { 34 id appleid, 35 token appletoken, 36 }; 37 let currentuser parse user = await parse user currentasync(); 38 // link user with his apple credentials 39 return await currentuser 40 linkwith('apple', { 41 authdata authdata, 42 }) 43 then(async (loggedinuser parse user) => { 44 // login returns the corresponding parseuser object 45 alert alert( 46 'success!', 47 `user ${loggedinuser get( 48 'username', 49 )} has successfully linked his apple account!`, 50 ); 51 // to verify that this is in fact the current user, currentasync can be used 52 currentuser = await parse user currentasync(); 53 console log(loggedinuser === currentuser); 54 return true; 55 }) 56 catch(async (error object) => { 57 // error can be caused by wrong parameters or lack of internet connection 58 alert alert('error!', error message); 59 return false; 60 }); 61 } catch (error any) { 62 // error can be caused by wrong parameters or lack of internet connection 63 alert alert('error!', error); 64 return false; 65 } 66 }; 将此功能分配给您的主屏幕上的一个苹果按钮 \<font color="#2166ae">onpress\</font> 参数。测试您的新功能,注意到 \<font color="#2166ae">parse user\</font> 对象 \<font color="#2166ae">authdata\</font> 的值将会更新为新的认证提供者数据。验证用户是否确实在您的 parse 服务器仪表板中更新。 结论 在本指南的最后,您学习了如何使用 \<font color="#2166ae">react native apple authentication\</font> 进行登录、注册或链接现有的 parse 用户。在下一个指南中,我们将向您展示如何执行有用的用户查询。