Skip to content

Commit

Permalink
feat(core): support google one tap
Browse files Browse the repository at this point in the history
support google one tap verification
  • Loading branch information
simeng-li committed Aug 7, 2024
1 parent 6a71448 commit e2d7c8e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ export class SocialVerification implements IdentifierVerificationRecord<Verifica
new RequestError({ code: 'session.verification_failed', status: 400 })
);

// TODO: sync userInfo and link social identity

const user = await this.findUserBySocialIdentity();

if (!user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export default function enterpriseSsoVerificationRoutes<
params: z.object({
connectorId: z.string(),
}),
body: socialVerificationCallbackPayloadGuard,
body: socialVerificationCallbackPayloadGuard.merge(
z.object({
verificationId: z.string(),
})
),
response: z.object({
verificationId: z.string(),
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GoogleConnector } from '@logto/connector-kit';
import {
VerificationType,
socialAuthorizationUrlPayloadGuard,
Expand Down Expand Up @@ -101,10 +102,34 @@ export default function socialVerificationRoutes<T extends ExperienceInteraction
},
});

const socialVerificationRecord = ctx.experienceInteraction.getVerificationRecordByTypeAndId(
VerificationType.Social,
verificationId
);
const socialVerificationRecord = (() => {
if (verificationId) {
return ctx.experienceInteraction.getVerificationRecordByTypeAndId(
VerificationType.Social,
verificationId
);
}

// Check if is Google one tap verification
if (
connectorId === GoogleConnector.factoryId &&
connectorData[GoogleConnector.oneTapParams.credential]
) {
const socialVerificationRecord = SocialVerification.create(
libraries,
queries,
connectorId
);
ctx.experienceInteraction.setVerificationRecord(socialVerificationRecord);
return socialVerificationRecord;
}

// No verificationId provided and not Google one tap callback
throw new RequestError({
code: 'session.verification_session_not_found',
status: 404,
});
})();

assertThat(
socialVerificationRecord.connectorId === connectorId,
Expand Down
6 changes: 3 additions & 3 deletions packages/schemas/src/types/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ export const socialAuthorizationUrlPayloadGuard = z.object({
export type SocialVerificationCallbackPayload = {
/** The callback data from the social connector. */
connectorData: Record<string, unknown>;
/** The verification ID returned from the authorization URI. */
verificationId: string;
/** The verification ID returned from the authorization URI. Optional for Google one tap callback */
verificationId?: string;
};
export const socialVerificationCallbackPayloadGuard = z.object({
connectorData: jsonObjectGuard,
verificationId: z.string(),
verificationId: z.string().optional(),
}) satisfies ToZodObject<SocialVerificationCallbackPayload>;

/** Payload type for `POST /api/experience/verification/password`. */
Expand Down

0 comments on commit e2d7c8e

Please sign in to comment.