Creating a user and authenticating at once (with passwordless login) #7121
-
|
Hi! Context
We have two flows for signing up 1. Initiated by userThe user goes to a general login/signup page and just enters the email, which will create a temporary user if not existing, sending a otp email to the user email, and on clicking that link they will verify the user and get logged in. Ephemeral (unverified) users are cleaned up when the otp itself expires. 2. Initiated by usBut we also want to support the case of us sending out an invitation email. We could of course send an email with some link to the signup-page described above, and somehow leverage it. But then the users experience would be to "first get one mail for the invitation, and then have to open yet another mail to verify that you are in control of that mail address" The user has already proven that it has control of that email address. So the question is, can we provide a nicer user experience for flow 2 without completely sacrificing security? I'm trying to figure out what APIs i can play with here. Is it possible to initiate a OTP request from pocketbase (js)? I was looking for something like I suppose we can just call the web api also, but it would be nice not have to do a external If above not works (leveraging OTP both for login and for invite) my plan is it possible to use the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There is no binding for
const user = ...
// generate a password similar to the default one
// OR if you are using it as a "magic link" change it to something more complex
const otpPassword = $security.randomStringWithAlphabet(user.collection().otp.length, "1234567890")
const otpCollection = $app.findCollectionByNameOrId("_otps")
// create OTP Record
const otp = new Record(otpCollection)
otp.set("collectionRef", user.collection().id)
otp.set("recordRef", user.id)
otp.set("password", otpPassword)
$app.save(otp)
// send an email with the OTP
$mails.sendRecordOTP($app, user, otp.id, otpPassword)
Only the bindings under the "PocketBase" section can be initialized in the JSVM. Everything else (aka. the types under "Namespaces") is a TypeScript type/interface that mirrors the Go equivalent package definition (ideally only for the used fields and methods). For example, compare the following docs: The JSVM shows that there is a Why it is shown in the docs if you can't initialize it? |
Beta Was this translation helpful? Give feedback.
There is no binding for
core.NewOTPin the JSVM at the moment but I'm not sure if it is really necessary.core.OTPis a typed Go proxy struct with some helper methods likeHasExpiredbut under the hood it is a regularRecord, meaning that you can create it on your own. This could look something like: