Skip to content
This repository was archived by the owner on Mar 1, 2024. It is now read-only.

Commit 2c2fcfd

Browse files
committed
login endpoint
1 parent 9ede89b commit 2c2fcfd

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Diff for: src/authentication/login.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { gql } from '@apollo/client/core';
2+
import { apolloClient } from '../apollo-client';
3+
import { getAddressFromSigner, signText } from '../ethers.service';
4+
5+
const GET_CHALLENGE = `
6+
query($request: ChallengeRequest!) {
7+
challenge(request: $request) { text }
8+
}
9+
`;
10+
11+
export const generateChallenge = (address: string) => {
12+
return apolloClient.query({
13+
query: gql(GET_CHALLENGE),
14+
variables: {
15+
request: {
16+
address,
17+
},
18+
},
19+
});
20+
};
21+
22+
const AUTHENTICATION = `
23+
mutation($request: SignedAuthChallenge!) {
24+
authenticate(request: $request) {
25+
accessToken
26+
refreshToken
27+
}
28+
}
29+
`;
30+
31+
export const authenticate = (address: string, signature: string) => {
32+
return apolloClient.mutate({
33+
mutation: gql(AUTHENTICATION),
34+
variables: {
35+
request: {
36+
address,
37+
signature,
38+
},
39+
},
40+
});
41+
};
42+
43+
export const login = async () => {
44+
const address = getAddressFromSigner();
45+
console.log('address', address);
46+
47+
// we request a challenge from the server
48+
const challengeResponse = await generateChallenge(address);
49+
50+
// sign the text with the wallet
51+
const signature = await signText(challengeResponse.data.challenge.text);
52+
53+
const accessTokens = await authenticate(address, signature);
54+
console.log(accessTokens);
55+
56+
return accessTokens;
57+
};
58+
59+
(async () => {
60+
await login();
61+
})();

0 commit comments

Comments
 (0)