-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-auth-challenge.ts
40 lines (31 loc) · 1.11 KB
/
create-auth-challenge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {Authsignal, VerificationMethod} from '@authsignal/node';
import {CreateAuthChallengeTriggerHandler} from 'aws-lambda';
const apiSecretKey = process.env.AUTHSIGNAL_SECRET!;
const apiUrl = process.env.AUTHSIGNAL_URL!;
const authsignal = new Authsignal({apiSecretKey, apiUrl});
export const handler: CreateAuthChallengeTriggerHandler = async event => {
const userId = event.request.userAttributes.sub;
const email = event.request.userAttributes.email;
if (!userId) {
throw new Error('User is undefined');
}
// Check if a challenge has already been initiated via passkey SDK
const {challengeId} = await authsignal.getChallenge({
userId,
action: 'cognitoAuth',
verificationMethod: VerificationMethod.PASSKEY,
});
// Should match your URL Scheme if using the React Native SDK to launch the pre-built UI
const redirectUrl = 'authsignal://auth';
const {url, token} = await authsignal.track({
userId,
action: 'cognitoAuth',
attributes: {
email,
challengeId,
redirectUrl,
},
});
event.response.publicChallengeParameters = {url, token};
return event;
};