Skip to content

Commit

Permalink
Merge pull request #163 from leancloud/auth-data
Browse files Browse the repository at this point in the history
✨ Add onAuthDataTransform Hook
  • Loading branch information
jysperm authored Aug 3, 2021
2 parents 3529d8b + f46b00c commit 33af60c
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 772 deletions.
9 changes: 9 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ LeanEngine 中间件会为这些 Hook 函数检查「Hook 签名」,确保调
* `currentUser: AV.User`:被操作的用户。
* `user: AV.User`:同 `currentUser`.
* `object: AV.User`:同 `currentUser`,因为登录认证 hook 被操作的对象正好是发起操作的用户。
* `authData: {[provider: string]: any}`,客户端发送的第三方登录信息(仅 `onAuthData`)。
#### `AV.Cloud.onAuthData`
这个函数的签名:`function(func: function)`,其中 `func` 是接受一个 Request 对象作为参数,返回新的 authData 的异步函数,如果抛出异常会中断第三方登录。

`Request` 上的属性包括:

* `authData: {[provider: string]: any}`,客户端发送的第三方登录信息。
### 实时通信 Hook 函数
Expand Down
6 changes: 6 additions & 0 deletions leanengine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ export namespace Cloud {
currentUser: User
}

interface AuthDataTransformRequest {
authData: {[provider: string]: any}
}

type CloudFunction = (request: CloudFunctionRequest) => Promise<any>;
type ClassHookFunction = (request: ClassHookRequest) => Promise<any>;
type UserHookFunction = (request: UserHookRequest) => Promise<any>;
type AuthDataTransformFunction = (request: AuthDataTransformRequest) => Promise<{[provider: string]: any}>;

export class Error {
constructor(message: string, options?: {status?: number, code?: number})
Expand All @@ -150,6 +155,7 @@ export namespace Cloud {

export function onVerified(handler: UserHookFunction): void;
export function onLogin(handler: UserHookFunction): void;
export function onAuthData(handler: AuthDataTransformFunction): void;

export function onIMMessageReceived(handler: CloudFunction): void;
export function onIMReceiversOffline(handler: CloudFunction): void;
Expand Down
4 changes: 4 additions & 0 deletions lib/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Cloud.onLogin = function(func) {
Cloud.define('__on_login__User', func);
};

Cloud.onAuthData = function(func) {
Cloud.define('__on_authdata__User', func);
};

_.each(utils.realtimeHookMapping, (metadataName, hookName) => {
Cloud[hookName] = Cloud.define.bind(null, metadataName);
});
Expand Down
16 changes: 12 additions & 4 deletions lib/leanengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function createCloudFunctionRouter(options) {
return callCloudFunction(req, functionOrClass, options);
} else if (functionOrClass === 'onVerified') {
return callUserHook(req, 'onVerified', hookName);
} else if (functionOrClass === '_User' && hookName === 'onLogin') {
return callUserHook(req, 'onLogin', '_User');
} else if (functionOrClass === '_User' && _.contains(['onLogin', 'onAuthData'], hookName)) {
return callUserHook(req, hookName, '_User');
} else if ((functionOrClass === 'BigQuery' || functionOrClass === 'Insight') && hookName === 'onComplete') {
return onCompleteBigQueryJob(req);
} else {
Expand Down Expand Up @@ -278,6 +278,10 @@ function callUserHook(req, hookName, verifyType) {
object: user
});

if (req.body.authData) {
request.authData = req.body.authData;
}

return new Promise( (resolve, reject) => {
if (hookName === 'onLogin' && userHookFunction.length === 2) {
userHookFunction(request, utils.prepareResponseObject(req.res, err => {
Expand All @@ -288,8 +292,12 @@ function callUserHook(req, hookName, verifyType) {
}
}));
} else {
promiseTry(userHookFunction.bind(null, request)).then( () => {
resolve({result: 'ok'});
promiseTry(userHookFunction.bind(null, request)).then( result => {
if (hookName === 'onAuthData') {
resolve({result});
} else {
resolve({result: 'ok'});
}
}).catch(reject);
}
});
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ exports.hookNameMapping = {
beforeDelete: '__before_delete_for_',
afterDelete: '__after_delete_for_',
onVerified: '__on_verified_',
onLogin: '__on_login_'
onLogin: '__on_login_',
onAuthData: '__on_authdata_'
};

exports.realtimeHookMapping = {
Expand Down
Loading

0 comments on commit 33af60c

Please sign in to comment.