Kamil
Kamil
Explore posts from servers
CDCloudflare Developers
Created by Kamil on 3/8/2025 in #workers-help
How to update Worker secrets via REST API (or TS SDK)?
Anyone?
3 replies
CDCloudflare Developers
Created by ankagar on 3/9/2025 in #workers-help
Next.js + D1 + SSG .. would that work?
Ask on OpenNext Discord server. You won't get much help here.
6 replies
CDCloudflare Developers
Created by ankagar on 3/9/2025 in #workers-help
Next.js + D1 + SSG .. would that work?
Not sure how you do it. Have you tried OpenNext adapter? It's solution to make Next.js apps running on Workers. It's backed by folks from Cloudflare and it works quite well even though it's not recommended on production yet. I suggest to try it and ask for help on their Discord server if you face any issues.
6 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
For your case I'd store data in two tables:
CREATE TABLE credential_identifiers (
id UUID PRIMARY KEY DEFAULT,
login TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL
);

CREATE TABLE credential_secrets (
identifier_id UUID PRIMARY KEY REFERENCES credential_identifiers(id) ON DELETE CASCADE,
password_hash TEXT NOT NULL
);
CREATE TABLE credential_identifiers (
id UUID PRIMARY KEY DEFAULT,
login TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL
);

CREATE TABLE credential_secrets (
identifier_id UUID PRIMARY KEY REFERENCES credential_identifiers(id) ON DELETE CASCADE,
password_hash TEXT NOT NULL
);
15 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
If you could precisely determine the key - KV would work here great. Unfortunately you can't. So the issue is you need more flexibility on querying and KV doesn't offer it. KV has simple data shape: the key and associated value. And the only way to get the value is by knowing the key. D1 is the SQL database so table shape can be more complex having separate columns for login, email and password so each column can be separately referenced. That way you can query it having only partial information - an email or login.
15 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
That's how I'd aproach it. Mixing KV and D1 together here is overkill imo. D1 gives you much more flexibility in quering the data.
15 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
As far as I understand the key must be fully accurate with the one stored in namespace - you cannot query the namespace with only the part of key (only email or login). In your case I'd store it in D1 with some hashing on the password value for extra security. Then you can freely query the database with only the login or email.
15 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
I use cf worker api to login to the account
Please precise what kind of API you use
15 replies
CDCloudflare Developers
Created by flutch on 3/8/2025 in #workers-help
Anyway I can lookup username:email format in KV using worker (without list) ?
Can you give some example? I don't get what you'd like to achieve
15 replies
CDCloudflare Developers
Created by Kamil on 3/8/2025 in #workers-help
How to update Worker secrets via REST API (or TS SDK)?
Leaving my source code below:
export const setAccessTokenSecret = async (accessTokenValue: string) => {
const createSecretUrl =
'https://api.cloudflare.com/client/v4/accounts/' +
process.env.CLOUDFLARE_ACCOUNT_ID +
'/workers/scripts/' +
process.env.CLOUDFLARE_WORKER_NAME +
'/secrets'
const deleteSecretUrl = `${createSecretUrl}/${INSTAGRAM_TOKEN_ENV_VARIABLE_NAME}`

const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
}

try {
const batchResponses = await Promise.all([
fetch(deleteSecretUrl, { method: 'DELETE', headers }),
fetch(createSecretUrl, {
method: 'PUT',
headers,
body: JSON.stringify({
name: INSTAGRAM_TOKEN_ENV_VARIABLE_NAME,
text: accessTokenValue,
type: 'secret_text',
}),
}),
])

const batchData = await Promise.all(
batchResponses.map((response) => response.json<CloudflareApiResponse>()),
)

const errors = batchData.map((data) => data.errors).flat()

const isSuccess =
errors.length === 0 && batchData.every((data) => data.success)

if (!isSuccess) {
throw new Error('Failed to update access token secret', {
cause: errors,
})
}

return isSuccess
} catch (error) {
console.error('[setAccessTokenSecret]', error)
return false
}
}
export const setAccessTokenSecret = async (accessTokenValue: string) => {
const createSecretUrl =
'https://api.cloudflare.com/client/v4/accounts/' +
process.env.CLOUDFLARE_ACCOUNT_ID +
'/workers/scripts/' +
process.env.CLOUDFLARE_WORKER_NAME +
'/secrets'
const deleteSecretUrl = `${createSecretUrl}/${INSTAGRAM_TOKEN_ENV_VARIABLE_NAME}`

const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
}

try {
const batchResponses = await Promise.all([
fetch(deleteSecretUrl, { method: 'DELETE', headers }),
fetch(createSecretUrl, {
method: 'PUT',
headers,
body: JSON.stringify({
name: INSTAGRAM_TOKEN_ENV_VARIABLE_NAME,
text: accessTokenValue,
type: 'secret_text',
}),
}),
])

const batchData = await Promise.all(
batchResponses.map((response) => response.json<CloudflareApiResponse>()),
)

const errors = batchData.map((data) => data.errors).flat()

const isSuccess =
errors.length === 0 && batchData.every((data) => data.success)

if (!isSuccess) {
throw new Error('Failed to update access token secret', {
cause: errors,
})
}

return isSuccess
} catch (error) {
console.error('[setAccessTokenSecret]', error)
return false
}
}
3 replies