Anyway I can lookup username:email format in KV using worker (without list) ?

For example, i have a format username:email if user logins with their username it can use READ and able to get username even with username:email format, however, with email I can't manage to get the email prefix
12 Replies
Kamil
Kamil2mo ago
Can you give some example? I don't get what you'd like to achieve
flutch
flutchOP2mo ago
in KV Database I have a key lets say (password is in value thats not a problem) flutch2:[email protected] I use cf worker api to login to the account so if i login as flutch2 Testing123!@# it is able to find the key flutch2:[email protected] and logs me in if i login as
[email protected] Testing123!@# it isnt able to find the key flutch2:[email protected] and doesn't log me in
Kamil
Kamil2mo ago
I use cf worker api to login to the account
Please precise what kind of API you use
flutch
flutchOP2mo ago
CF Worker using KV Bindings
Kamil
Kamil2mo ago
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.
flutch
flutchOP2mo ago
So D1's the only route Got it, thanks!
Kamil
Kamil2mo ago
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.
flutch
flutchOP2mo ago
So what's the point for KV then? It gives the same speed etc.
Kamil
Kamil2mo ago
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.
flutch
flutchOP2mo ago
I see
Kamil
Kamil2mo ago
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
);
flutch
flutchOP2mo ago
Oh dont worry I got it figured out and migrated it already, thanks for the help :D

Did you find this page helpful?