Slackbot
07/05/2023, 2:31 PMShane Jordan
07/05/2023, 2:38 PM401: {"message": "Forbidden - a valid `x-typesense-api-key` header must be sent."}
Code block
Future<Configuration> getTypeSenseConfig(Environment environment) async {
final typeSenseConfig = TypeSenseConfig(environment);
var generatedScopeKey =
await callGenerateScopedSearchKey('userId', 'accountId');
// Create and return the Configuration instance
final config = Configuration(
generatedScopeKey,
nodes: {
Node.withUri(
Uri(
scheme: 'https',
host: typeSenseConfig.typeSenseHost,
port: 443,
),
),
},
numRetries: 3, // A total of 4 tries (1 original try + 3 retries)
connectionTimeout: const Duration(seconds: 2),
);
return config;
}Jason Bosco
07/05/2023, 5:01 PMJason Bosco
07/05/2023, 5:02 PMJason Bosco
07/05/2023, 5:02 PMShane Jordan
07/05/2023, 5:12 PM// @ts-check
const Typesense = require("typesense");
// Initialize Typesense client
const typesenseClient = new Typesense.Client({
// Set the appropriate endpoint and API key for your Typesense instance
nodes: [
{
host: "redacted-1.a1.typesense.net",
port: 443,
protocol: "https", // Use 'https' for secure connection
},
],
apiKey: "redacted",
});
// Function to generate a scoped search key
async function generateScopedSearchKeyForTypesense(userId, accountId) {
try {
// Make sure that the parent search key you use to generate a scoped search key
// has no other permissions besides `documents:search`
// Generate a scoped search API key with embedded filter
const searchKey = "redacted"; // Replace with your main search API key
const parameters = {
filter_by: `accountId:=${accountId}`,
};
const scopedSearchKey = await typesenseClient.keys().generateScopedSearchKey(searchKey, parameters);
// Make sure that the parent search key you use to generate a scoped search key
// has no other permissions besides `documents:search`
// Return the scoped search key
return scopedSearchKey;
} catch (error) {
console.error("Error generating scoped search key:", error);
throw error;
}
}
module.exports = generateScopedSearchKeyForTypesense;Jason Bosco
07/05/2023, 5:17 PM// Make sure that the parent search key you use to generate a scoped search key
// has no other permissions besides `documents:search`Jason Bosco
07/05/2023, 5:17 PM[ "documents:search", "documents:get" ]Jason Bosco
07/05/2023, 5:18 PMdocuments:search permission), vs the documents:get endpointShane Jordan
07/05/2023, 5:29 PMShane Jordan
07/05/2023, 6:27 PM400: {"message": "No search fields specified for the query."}
I'm using that cloud function in order to generate the scope searched key with the only required parameter (for security purposes) that is required and that is accountId.
I'm taking that scoped security key and using it in my Configuration object from Flutter. Then I'm querying with what the user would be searching. Maybe i move this all to the server side cloud function?
final searchParameters = {
'q': searchModel
.searchKey, // Access searchKey property using the null-aware operator
'per_page': pageSize.toString(),
'page': (pageKey + 1).toString(),
'preset': 'inventory_search',
'filter_by': filterBy,
"sort_by": "modifiedOn:desc",
'facet_by': 'category,status',
};Jason Bosco
07/05/2023, 7:18 PMShane Jordan
07/05/2023, 9:39 PMJason Bosco
07/05/2023, 9:39 PMpreset param in your code snippet above. My bad.Jason Bosco
07/05/2023, 9:40 PMShane Jordan
07/05/2023, 9:40 PMShane Jordan
07/06/2023, 12:25 AMq parameter, but the purpose of this function is get my search scope key, and then call the actual search with that key and a query. is that how it's supposed to work?Jason Bosco
07/06/2023, 3:20 AMthe purpose of this function is get my search scope keyYou would use the code in this function to get your search scoped key
Jason Bosco
07/06/2023, 3:21 AM