0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00
logto/packages/connectors/connector-mock-social/src/index.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-04-01 03:37:49 -05:00
import { randomUUID } from 'node:crypto';
2023-04-01 09:16:56 -05:00
import { z } from 'zod';
import type {
GetAuthorizationUri,
GetUserInfo,
CreateConnector,
SocialConnector,
} from '@logto/connector-kit';
import {
ConnectorError,
ConnectorErrorCodes,
ConnectorType,
jsonGuard,
} from '@logto/connector-kit';
import { defaultMetadata } from './constant.js';
import { mockSocialConfigGuard } from './types.js';
const getAuthorizationUri: GetAuthorizationUri = async ({ state, redirectUri }) => {
return `http://mock.social.com/?state=${state}&redirect_uri=${redirectUri}`;
};
const getUserInfo: GetUserInfo = async (data) => {
const dataGuard = z.object({
code: z.string(),
userId: z.optional(z.string()),
email: z.string().optional(),
phone: z.string().optional(),
});
const result = dataGuard.safeParse(data);
if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, JSON.stringify(data));
}
const { code, userId, ...rest } = result.data;
// For mock use only. Use to track the created user entity
return {
id: userId ?? `mock-social-sub-${randomUUID()}`,
...rest,
rawData: jsonGuard.parse(data),
};
};
const createMockSocialConnector: CreateConnector<SocialConnector> = async ({ getConfig }) => {
return {
metadata: defaultMetadata,
type: ConnectorType.Social,
configGuard: mockSocialConfigGuard,
getAuthorizationUri,
getUserInfo,
};
};
export default createMockSocialConnector;