Javascript:使用 Salesforce 实施无密码登录

javascript:使用 salesforce 实施无密码登录

salesforce 提供无头无密码登录流程,允许注册用户无缝访问应用程序。无密码登录非常用户友好,它只需要一个有效的电子邮件地址。在这篇文章中,我将分享一些用于使用 salesforce 实现无密码登录流程的代码片段。

要求

开始之前,请确保满足以下条件:

a) 您有权访问 salesforce 环境。

b) 您已注册用户并启用无密码登录选项。

第一步:发送用户名

export async function passwordlesslogin(username, captcha) {  const payload = {    username,    recaptcha: captcha,    verificationmethod: "email",  };  const config = {    headers: {      "content-type": "application/json",    },    method: "post",    body: json.stringify(payload),  };  const url = `${replace_with_your_salesforce_cloud_url}/services/auth/headless/init/passwordless/login`;  const response = await fetch(url, config);  const result = await response.json();  return result;}

登录后复制

这是对 salesforce 的第一次调用。请注意以下事项:

a) 您需要通过验证码。如需帮助,请查看 recaptcha v3。

b) 验证方法设置为电子邮件,这会告诉 salesforce 通过电子邮件发送一次性密码 (otp)。

立即学习“Java免费学习笔记(深入)”;

c) 除了验证码和验证方法之外,唯一需要的其他参数是用户名,它对应于用户注册的电子邮件。

如果请求成功,salesforce 将向提供的用户名发送一封电子邮件并返回如下响应:

{  "identifier": "mff0rwswmdawmdixdvrk",  "status": "success"}

登录后复制

第二步:捕获 otp

export async function passwordlessauthorize(identifier, code) {  const authorization = btoa(identifier + ":" + code);  const config = {    headers: {      "auth-request-type": "passwordless-login",      "auth-verification-type": "email",      authorization: "basic " + authorization,      "content-type": "application/x-www-form-urlencoded",    },    method: "post",    body: new urlsearchparams({      response_type: "code_credentials",      client_id: "replace_with_your_client_id",      redirect_uri: "replace_with_your_redirect_uri",    }),  };  const response = await fetch(    `${replace_with_your_salesforce_cloud_url}/services/oauth2/authorize`,    config  );  const result = await response.json();  return result;}

登录后复制

这是第二次致电 salesforce。这里有几个要点:

a) 标识符是第一步返回的值。

b) 该代码是 salesforce 通过电子邮件发送的 otp。

c) 注意 auth 和 authorization 标头的定义方式。

d) content-type 是 application/x-www-form-urlencoded。注意正文如何使用 urlsearchparams 进行格式化。

如果一切顺利,salesforce 将返回如下响应:

{  "code": "aprxoppu1bwu2d3sbssbklubzop4sxhra2tb.p3lapgviexvmwyigvaf6vtebi7ottvto18uuq==",  "sfdc_community_url": "https://site.com/application",  "sfdc_community_id": "xxxxxxxx"}

登录后复制

第三步:用代码交换访问令牌

最后一步是将上一步中的代码交换为访问令牌。访问令牌至关重要,因为它允许您代表用户发出请求。访问令牌的存在可启用用户会话。

export async function getaccesstoken(code) {  const config = {    headers: {      "content-type": "application/x-www-form-urlencoded",    },    method: "post",    body: new urlsearchparams({      code,      grant_type: "authorization_code",      client_id: "replace_with_your_client_id",      redirect_uri: "replace_with_your_redirect_uri",    }),  };  const response = await fetch(    `${replace_with_your_salesforce_cloud_url}/services/oauth2/token`,    config  );  const result = await response.json();  return result;}

登录后复制

响应应如下所示:

{  "access_token": "00dej000006dhsr!aqeaqgpj5xvnbl1qq8pi4xjyghmxajig7ca4ci0mixzcg7ho_yyzanyxpx9uelaez2905vfne6vzhmavmndoboks.wzhlzhc",  "refresh_token": "5aep861i1ns2kincggjsdz4ootyjzqw_gzds5f1pwqh0nfu0akgldaw5ptc.qadf.bvz1aplukjyise2lxx5kq0",  "sfdc_community_url": "https://site.com/application",  "sfdc_community_id": "xxxxxxxx",  "signature": "jwnfzy2g3phxcl3fjrfju5x2ayxw7ozsfg2bz6bbb74=",  "scope": "refresh_token openid user_registration_api api",  "id_token": "...",  "instance_url": "https://site.com/",  "id": "https://test.salesforce.com/id/00000/11111",  "token_type": "bearer",  "issued_at": "1733700157803"}

登录后复制

确保安全地存储 access_token,从这里,您可以为您的用户创建会话。出于安全考虑,最好在服务器上执行这些方法。

id_token 是 jwt 令牌。如果你解码它,它会看起来像这样:

{  "at_hash": "HTa4VEmQhCYi59WLhiL6DQ",  "sub": "https://test.salesforce.com/id/00000/11111",  "aud": "3MXG9j6uMOMC1DNjcltNj9xPoUi7xNbiSwPqOjmDSLfCW54f_Qf6EG3EKqUAGT6xyGPc7jqAMi4ZRw8WTIf9B",  "iss": "https://site.com/",  "exp": 1733702662,  "iat": 1733702542}

登录后复制

您还可以自定义 jwt 以包含其他数据。但是,建议保持结构最小化,并根据需要使用访问令牌来获取其他信息。

结论

无密码登录对每个人来说都很方便,大多数云服务(例如 salesforce)现在都提供无密码登录流程。利用此功能可以简化登录过程并改善用户体验。

以上就是Javascript:使用 Salesforce 实施无密码登录的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2646037.html

(0)
上一篇 2025年3月7日 07:41:01
下一篇 2025年3月3日 16:44:08

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论