Apple Login for JavaScript

Apple Login for JavaScript

官方文档:developer.apple.com/documentati...

参数说明

Response type

控制登录后返回的类型,可选是code,id_token等,如果是id_token,下面的response_mode则必须是form_post

Response Mode

控制登录后code/id_token返回方式;

  1. 如果response_typequery则会拼接在下面配置的redirect_uri的后面
  2. 如果response_typeid_token,则需要配置form_post,则会以post方法请求redirect_uri

Client Id

在Apple 开发者账户创建Identifier:

注册成功则Identifier则是Client Id;

Redirect uri

  1. 如果response_typequery则登录获取的code则会拼接在url后面
  2. 如果response_typeid_token,则需要配置form_post,则会以post方法请求返回该url

![编辑](/Users/moki/Library/Application Support/typora-user-images/image-20250331005459622.png)

Scope

向Apple请求的用户信息, 比如name email使用空格分割

JS使用Apple登录(前端部分)

使用url

可以使用拼接的url打开苹果登录页,文档: developer.apple.com/documentati...:

typescript 复制代码
const baseUrl = "https://appleid.apple.com/auth/authorize";
const url = new URL(baseUrl);

url.searchParams.append("response_type", "code"); 
url.searchParams.append("response_mode", "query");
url.searchParams.append("client_id", "xxx");
url.searchParams.append("nonce", "name email");
url.searchParams.append("redirect_uri", "https://xxx.com");

window.open(url.torString(), "_blank");

使用JS SDK

body中引入js文件:

html 复制代码
<body>
        <script 
                type="text/javascript"           src="https://appleid.cdnapple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
        </script>
    </body>

初始化:

js 复制代码
// 参数值同上
AppleID.auth.init({
    clientId : '[CLIENT_ID]',
    scope : '[SCOPES]',
    redirectURI : '[REDIRECT_URI]',
    usePopup : true
});

主动调用登录:

typescript 复制代码
interface SignResponseType {
  authorization: {
    code: string;
    id_token: string;
    state: string;
  },
  user: {
       email: string,
       name: {
         firstName: string,
         lastName: string
       }
     }
}

const onCode = () => {
  try {
    // AppleID是根据上面 body 中导入的 js 挂载
    window.AppleID.auth
      .signIn()
      .then((res: SignResponseType) => {
              console.log("code: ", res?.authorization?.code)
      })
      .catch(console.log);
  }catch(err) {
    // TODO...
  }
}

onCode();

校验Apple Code (后端部分)

该部分以 Node 为例子,处理前端获取到apple code传递给后端进行校验

首选需要登录apple开发者账户创建一个keys:

需要勾选上Sign in with Apple:

记住 keyid和保存证书为xx.p8格式,该证书只能下载一次!

![image-20250331011349189](/Users/moki/Library/Application Support/typora-user-images/image-20250331011349189.png)

使用 apple-signin-auth处理:

typescript 复制代码
import appleSignin from 'apple-signin-auth';


const clientSecret = appleSignin.getClientSecret({
    clientID: 'xxxx', // 与上面创建的 clientID 为同一个
    teamID: 'x x x x', // Appl 开发者账户的 唯一标识符
    privateKey: '', // 该值为上面导出的`xxx.p8`的证书的值,可用文本编辑器打开
    // privateKeyPath: "", // 不想用 privateKey 可以用 该属性代替,表示 xxx.p8 证书的地址
    keyIdentifier: '', // 上面创建 key 的 key Id
    expAfter: 15777000, 
});
const options = {
    clientID: '', // Apple Client ID
    redirectUri: '', // use the same value which you passed to authorisation URL.
    clientSecret: clientSecret
};


// 测试前端的token是否有效
try {
    const tokenResponse = await appleSignin.getAuthorizationToken("x x x", options);

} catch (err) {
    console.error(err);
}
相关推荐
夜郎king12 分钟前
HTML5 SVG 实现日出日落动画与实时天气可视化
前端·html5·svg 日出日落
夏幻灵1 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_1 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝1 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions2 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发2 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_2 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、2 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao2 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架