I have tried everything, but apparently it is moralis. since I changed the authentication code to:
authService.ts:
const Login = async() => {
setStateCircularProgress(true);
const provider = "metamask";
await enableWeb3({ provider: provider });
try {
// Connect to Metamask
const { signer, chain, account } = await connectToMetamask();
if (!state.authenticated) {
if (chain != chainID.testNet) {
await switchNetworkPolygon();
}
}
if (!account) {
throw new Error(
"Connecting to chain failed, as no connected account was found"
);
}
if (!chain) {
throw new Error(
"Connecting to chain failed, as no connected chain was found"
);
}
const { message } = await Moralis.Cloud.run("requestMessage", {
address: account,
chain: parseInt(chainID.testNet),
});
const signature = await signer.signMessage(message);
const { user } = await Moralis.Cloud.run("requestVerifyMessage", {
message,
signature,
});
console.log("message", message);
console.log("account", account);
console.log("user", user);
setValuesAlertFunc(true, "Login success", "success");
setStateCircularProgress(false);
} catch(e) {
const errorMessage = JSON.stringify(e);
const errorObject = JSON.parse(errorMessage);
setValuesAlertFunc(true, errorObject.message, "error");
console.log("error", e);
}
setStateCircularProgress(false);
};
y me sigue dando error de mensaje:
error Error: [C0006] Request failed, Bad Request(400): Unauthorized
at handleError (RESTController.js:438:17)
at async Login (UserState.tsx?t=1685654058847:219:11)
y los datos se estan pasando. no se que mas hacer o probar. please ayuda :(.
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import Moralis from 'moralis';
import { authRequests } from '../store';
import { ParseServerRequest } from '../utils/ParseServerRequest';
import config from '../config';
const STATEMENT = 'Welcome to Koolinart';
const EXPIRATION_TIME = 900000;
const TIMEOUT = 15;
export async function requestMessage({ address, chain, network }: { address: string; chain: string; network: 'evm' }) {
const url = new URL(config.SERVER_URL);
const now = new Date();
const expirationTime = new Date(now.getTime() + EXPIRATION_TIME);
const result = await Moralis.Auth.requestMessage({
address,
chain,
domain: url.hostname,
statement: STATEMENT,
uri: url.toString(),
expirationTime: expirationTime.toISOString(),
timeout: TIMEOUT,
});
console.log('url.hostname', url.hostname)
console.log('url', url.toString())
const { message, id, profileId } = result.toJSON();
authRequests.set(message, { id, profileId });
console.log('url', url.toString())
return message;
}
export async function verifyMessage({ network, signature, message }: any) {
const storedData = authRequests.get(message);
if (!storedData) {
throw new Error('Invalid message');
}
const { id: storedId, profileId: storedProfileId } = storedData;
const authData = {
id: storedProfileId,
authId: storedId,
message,
signature,
network,
};
// Authenticate
const user = await ParseServerRequest.post({
endpoint: `/users`,
params: {
authData: {
moralis: authData,
},
},
useMasterKey: true,
});
// Update user moralisProfile column
await ParseServerRequest.put({
endpoint: `/users/${user.objectId}`,
params: {
moralisProfileId: storedProfileId,
},
useMasterKey: true,
});
// Get authenticated user
const updatedUser = await ParseServerRequest.get({
endpoint: `/users/${user.objectId}`,
useMasterKey: true,
});
return updatedUser;
}
I will leave 2 more imports that have authService.ts, which is authRequests,
ParseServerRequest.
ParseServerRequest.ts:
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { CoreProvider, RequestController } from '@moralisweb3/common-core';
import config from '../config';
export class ParseServerRequest {
static get(arg0: { endpoint: string; useMasterKey: boolean; }) {
throw new Error('Method not implemented.');
}
static put(arg0: { endpoint: string; params: { moralisProfileId: string; }; useMasterKey: boolean; }) {
throw new Error('Method not implemented.');
}
protected readonly requestController: RequestController;
static post: any;
constructor() {
const core = CoreProvider.getDefault();
this.requestController = RequestController.create(core);
}
getHeaders = (useMasterKey: boolean) => ({
...(useMasterKey && { 'X-Parse-Master-Key': config.MASTER_KEY }),
'X-Parse-Application-Id': config.APPLICATION_ID,
});
post = <Result>({
endpoint,
params,
useMasterKey,
}: {
endpoint: string;
params: Record<string, unknown>;
useMasterKey: boolean;
}) =>
this.requestController.post(`${config.SERVER_URL}/${endpoint}`, undefined, params, {
headers: this.getHeaders(useMasterKey),
}) as Promise<Result>;
put = <Result>({
endpoint,
params,
useMasterKey,
}: {
endpoint: string;
params: Record<string, unknown>;
useMasterKey: boolean;
}) =>
this.requestController.put(`${config.SERVER_URL}/${endpoint}`, undefined, params, {
headers: this.getHeaders(useMasterKey),
}) as Promise<Result>;
get = <Result>({ endpoint, useMasterKey }: { endpoint: string; useMasterKey: boolean }) =>
this.requestController.get(`${config.SERVER_URL}/${endpoint}`, undefined, {
headers: this.getHeaders(useMasterKey),
}) as Promise<Result>;
}
store.ts- authRequests:
// TODO: replace for proper data store, and remove expired authRequests
export const authRequests = new Map<string, { id: string; profileId: string }>();
I donât understand if I modified something and itâs wrong. please help.