You will find it in parse-server-migration/src/auth/authService.ts
file
import Moralis from 'moralis';
import config from '../config';
export interface RequestMessage {
address: string;
chain: string;
networkType: string;
}
const STATEMENT = 'Please sign this message to confirm your identity.';
const EXPIRATION_TIME = 900000;
const TIMEOUT = 15;
export async function requestMessage({
address,
chain,
networkType,
}: {
address: string;
chain: string;
networkType: '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,
networkType,
domain: url.hostname,
uri: url.toString(),
statement: STATEMENT,
notBefore: now.toISOString(),
expirationTime: expirationTime.toISOString(),
timeout: TIMEOUT,
});
const { message } = result.toJSON();
return message;
}
Can you try replacing the 15 with 120. Since you are using walletconnect it sometimes can take more than 15 seconds for authentication.
The rest of the code seems ok.
After making the changes rebuild the server code and start the server again.
Still the same error, can it be related to this, that it is not picking up the time properly or the date, because the error says tried to encode invalid date
const now = new Date();
const expirationTime = new Date(now.getTime() + EXPIRATION_TIME);
Your code is correct. Here is the code from my project. It seems to be matching with your code
import Moralis from 'moralis';
import config from '../config';
export interface RequestMessage {
address: string;
chain: string;
networkType: string;
}
const STATEMENT = 'Please sign this message to confirm your identity.';
const EXPIRATION_TIME = 900000;
const TIMEOUT = 120;
export async function requestMessage({
address,
chain,
networkType,
}: {
address: string;
chain: string;
networkType: '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,
networkType,
domain: url.hostname,
uri: url.toString(),
statement: STATEMENT,
notBefore: now.toISOString(),
expirationTime: expirationTime.toISOString(),
timeout: TIMEOUT,
});
const { message } = result.toJSON();
return message;
}
Maybe the error is not coming from authentication code logic๐ค.
Can you take a look the code file that is referenced in the error trace if you can find more details in the file.
It is coming from this file
parse-server-migration/node_modules/parse-server/lib/Controllers/DatabaseController.js
This is the function that is throwing error
find(className, query, {
skip,
limit,
acl,
sort = {},
count,
keys,
op,
distinct,
pipeline,
readPreference,
hint,
caseInsensitive = false,
explain
} = {}, auth = {}, validSchemaController) {
const isMaster = acl === undefined;
const aclGroup = acl || [];
op = op || (typeof query.objectId == 'string' && Object.keys(query).length === 1 ? 'get' : 'find'); // Count operation if counting
op = count === true ? 'count' : op;
let classExists = true;
return this.loadSchemaIfNeeded(validSchemaController).then(schemaController => {
//Allow volatile classes if querying with Master (for _PushStatus)
//TODO: Move volatile classes concept into mongo adapter, postgres adapter shouldn't care
//that api.parse.com breaks when _PushStatus exists in mongo.
return schemaController.getOneSchema(className, isMaster).catch(error => {
// Behavior for non-existent classes is kinda weird on Parse.com. Probably doesn't matter too much.
// For now, pretend the class exists but has no objects,
if (error === undefined) {
classExists = false;
return {
fields: {}
};
}
throw error;
}).then(schema => {
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
// so duplicate that behavior here. If both are specified, the correct behavior to match Parse.com is to
// use the one that appears first in the sort list.
if (sort._created_at) {
sort.createdAt = sort._created_at;
delete sort._created_at;
}
if (sort._updated_at) {
sort.updatedAt = sort._updated_at;
delete sort._updated_at;
}
const queryOptions = {
skip,
limit,
sort,
keys,
readPreference,
hint,
caseInsensitive,
explain
};
Object.keys(sort).forEach(fieldName => {
if (fieldName.match(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
throw new _node.Parse.Error(_node.Parse.Error.INVALID_KEY_NAME, `Cannot sort by ${fieldName}`);
}
const rootFieldName = getRootFieldName(fieldName);
if (!SchemaController.fieldNameIsValid(rootFieldName, className)) {
throw new _node.Parse.Error(_node.Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
}
if (!schema.fields[fieldName.split('.')[0]] && fieldName !== 'score') {
delete sort[fieldName];
}
});
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op)).then(() => this.reduceRelationKeys(className, query, queryOptions)).then(() => this.reduceInRelation(className, query, schemaController)).then(() => {
let protectedFields;
if (!isMaster) {
query = this.addPointerPermissions(schemaController, className, op, query, aclGroup);
/* Don't use projections to optimize the protectedFields since the protectedFields
based on pointer-permissions are determined after querying. The filtering can
overwrite the protected fields. */
protectedFields = this.addProtectedFields(schemaController, className, query, aclGroup, auth, queryOptions);
}
if (!query) {
if (op === 'get') {
throw new _node.Parse.Error(_node.Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
} else {
return [];
}
}
if (!isMaster) {
if (op === 'update' || op === 'delete') {
query = addWriteACL(query, aclGroup);
} else {
query = addReadACL(query, aclGroup);
}
}
validateQuery(query, isMaster, false);
if (count) {
if (!classExists) {
return 0;
} else {
return this.adapter.count(className, schema, query, readPreference, undefined, hint);
}
} else if (distinct) {
if (!classExists) {
return [];
} else {
return this.adapter.distinct(className, schema, query, distinct);
}
} else if (pipeline) {
if (!classExists) {
return [];
} else {
return this.adapter.aggregate(className, schema, pipeline, readPreference, hint, explain);
}
} else if (explain) {
return this.adapter.find(className, schema, query, queryOptions);
} else {
return this.adapter.find(className, schema, query, queryOptions).then(objects => objects.map(object => {
object = untransformObjectACL(object);
return filterSensitiveData(isMaster, aclGroup, auth, op, schemaController, className, protectedFields, object);
})).catch(error => {
throw new _node.Parse.Error(_node.Parse.Error.INTERNAL_SERVER_ERROR, error);
});
}
});
});
});
}
Did you make any schema changes to user table in mongo database?
I did replace the schema of the user to the moralis server _SCHEMA and deleted the default schema
Maybe the error Tried to encode an invalid date
is due to that๐ค.
Do you think you can try to revert the changes of the _user table?
When I create the mongoDB database, there is no table of _Users (like the one that existed in Moralis server), I migrate my moralis parse server db to mongoDB. Here are the tables which I migrated from moralisDB and all of them are from my moralis server. All of these collections are from moralis db, I deleted the default collections like _SCHEMA, _ROLE because there was some overlapping data
In general, we should not make any schema updates to the tables starting with _
as they are internal tables. You can add new columns of data, but the existing schema should not be updated.
Then how would I be able to migrate my moralisDB collections? Because when I create the db there no collection other than _User, _Role and _SCHEMA
So, I should create a new Database and should update the collections rather than deleting the default and importing the moralisDB ones?
I believe that should be the case.
In this tutorial he did not manually add any user related tables in MongoDB. They were auto-generated when he signed up from the frontend. Mostly everything is handled internally through code.
I have only one confusion and that is then how would I migrate my pervious moralisDB data to mongoDB (collections having the same name). Also, I am gonna try to redo my DB and then, Iโll let you know if it fixed my issue. Thankyou for the help till now. Will get back to you soon. Thankyou
@johnversus This is what is displayed when I create a new database and open collections, which should I select?
Click on add my own data.
No need to add anything now. Try to get the connection url of the database and use it in your code
Should I migrate the moralisDB to mongoDB with the collections that start with _
Test the authentication first. It that works as expected then we can try the migrations.