Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove manual user route functions
Browse files Browse the repository at this point in the history
0xemc committed Nov 27, 2023
1 parent 02f5e33 commit 16aaace
Showing 2 changed files with 33 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { getUsersWalletorhandle } from ".generated/carbonmark-api-sdk/clients";
import { getUsersWalletorhandle, postLogin, postLoginVerify, postUsers, putUsersWallet } from ".generated/carbonmark-api-sdk/clients";
import { useWeb3 } from "@klimadao/lib/utils";
import { t, Trans } from "@lingui/macro";
import { Trans, t } from "@lingui/macro";
import { ButtonPrimary } from "components/Buttons/ButtonPrimary";
import { Text } from "components/Text";
import { InputField } from "components/shared/Form/InputField";
import { TextareaField } from "components/shared/Form/TextareaField";
import { Spinner } from "components/shared/Spinner";
import { Text } from "components/Text";
import { isAddress } from "ethers-v6";
import { loginUser, postUser, putUser, verifyUser } from "lib/api";
import { VALID_HANDLE_REGEX } from "lib/constants";
import { User } from "lib/types/carbonmark.types";
import { isNil } from "lodash";
@@ -74,28 +73,32 @@ export const EditProfile: FC<Props> = (props) => {
setIsLoading(true);

if (!address) return;
const loginRes = await loginUser(address);
const loginRes = await postLogin(address);

if (!signer) return;
const signature = await signer.signMessage(
editSignMessage(loginRes.nonce)
);

const verifyResponse = await verifyUser({
const verifyResponse = await postLoginVerify({
address,
signature,
});

let response;
let response: User;
if (isExistingUser) {
response = await putUser({
user: values,
token: verifyResponse.token,
response = await putUsersWallet(values.wallet, values, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${verifyResponse.token}`,
}
});
} else {
response = await postUser({
user: values,
token: verifyResponse.token,
response = await postUsers(values, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${verifyResponse.token}`,
}
});
}

@@ -147,23 +150,23 @@ export const EditProfile: FC<Props> = (props) => {
"handle",
!isExistingUser // validate only if handle can be changed
? {
required: {
value: true,
message: t`Handle is required`,
},
pattern: {
value: VALID_HANDLE_REGEX, // no special characters!
message: t`Handle should not contain any special characters`,
},
validate: {
isAddress: (v) =>
!isAddress(v) || // do not allow polygon addresses
t`Handle should not be an address`,
isNewHandle: async (v) =>
(await fetchIsNewHandle(v)) || // ensure unique handles
t`Sorry, this handle already exists`,
},
}
required: {
value: true,
message: t`Handle is required`,
},
pattern: {
value: VALID_HANDLE_REGEX, // no special characters!
message: t`Handle should not contain any special characters`,
},
validate: {
isAddress: (v) =>
!isAddress(v) || // do not allow polygon addresses
t`Handle should not be an address`,
isNewHandle: async (v) =>
(await fetchIsNewHandle(v)) || // ensure unique handles
t`Sorry, this handle already exists`,
},
}
: undefined
),
}}
85 changes: 0 additions & 85 deletions carbonmark/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,9 @@
import { getUsersWalletorhandle } from ".generated/carbonmark-api-sdk/clients";
import { KlimaRetire } from "@klimadao/lib/types/subgraph";
import { urls } from "lib/constants";
import { pollUntil } from "lib/pollUntil";
import { User } from "lib/types/carbonmark.types";
import { createDownloadLink } from "./createDownloadLink";

export const loginUser = async (wallet: string): Promise<{ nonce: string }> => {
const res = await fetch(`${urls.api.users}/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
wallet,
}),
});

const data = await res.json();

if (!res.ok || !data.nonce) {
throw new Error(data.message);
}
return data;
};

export const verifyUser = async (params: {
address: string;
signature: string;
}): Promise<{ token: string }> => {
const res = await fetch(`${urls.api.users}/login/verify`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
wallet: params.address,
signature: params.signature,
}),
});

const data = await res.json();

if (!res.ok || !data.token) {
throw new Error(data.message);
}
return data;
};

export const putUser = async (params: {
user: User;
token: string;
}): Promise<User> => {
const res = await fetch(`${urls.api.users}/${params.user.wallet}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${params.token}`,
},
body: JSON.stringify(params.user),
});

const data = await res.json();

if (!res.ok || data.error) {
throw new Error(data.message);
}
return data;
};

export const postUser = async (params: {
user: User;
token: string;
}): Promise<User> => {
const res = await fetch(urls.api.users, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${params.token}`,
},
body: JSON.stringify(params.user),
});

const data = await res.json();

if (!res.ok || data.error) {
throw new Error(data.message);
}
return data;
};

export const getRetirements = async (params: {
beneficiaryAddress: string;
limit?: number;

0 comments on commit 16aaace

Please sign in to comment.