Repository navigation
feat: Add passwordless OTP login to the email identity provider - #5869
marcelomendoncasoares merged 35 commits into
Conversation
20822a0 to
91f602f
Compare
91f602f to
ffc556c
Compare
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
567a227 to
e3fcfef
Compare
|
I have read the CLA Document and I hereby sign the CLA |
40350a4 to
b8d1aff
Compare
…tils Move the mapping from `SecretChallengeException` to the email account request and password reset exceptions into a shared helper, so that the passwordless login can reuse the email account request mapping. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`defaultVerificationCodeGenerator` gains an optional `length`, still defaulting to 8, so registration and password reset codes are unchanged. `defaultOtpVerificationCodeGenerator` returns 6 digit codes for the passwordless login. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
b8d1aff to
2602f08
Compare
… provider Users request a one-time password for their email, verify it, and are then signed in, or create an account if none exists for the email. The flow reuses the secret challenge pattern of registrations and password resets, and is exposed through `EmailIdp.startOtpLogin`, `verifyOtpLoginCode`, `finishOtpLogin` and `finishOtpRegistration`. - `EmailAccountOtpLoginRequest` stores the pending logins, and `EmailAccountOtpVerificationResult` tells the client whether an account exists and whether a password may be set on account creation. - Tokens issued by the passwordless login use the `email_otp` method (`EmailIdp.otpMethod`), so they can be told apart from password logins. - `EmailIdpConfig` gains the code generator (6 digits by default), lifetime (15 minutes), allowed attempts per code (3), rate limit on requested codes (5 per 15 minutes), sender and `allowPasswordOnOtpRegistration` (defaults to `false`). - Without `sendOtpLoginVerificationCode`, codes are sent with `sendRegistrationVerificationCode` and a warning is logged once. Starting a login fails with a `StateError` if neither sender is set. - Completing a login or an account creation clears the code requests logged for the email, like a successful password login clears its failed attempts, so users signing in repeatedly are not locked out. Codes requested by others still count, since only the owner of the email can complete a request. - The admin can delete expired login requests and logged code requests. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The endpoint extends `EmailIdpBaseEndpoint` with `startOtpLogin`, `verifyOtpLoginCode`, `finishOtpLogin` and `finishOtpRegistration`, so servers opt in to the passwordless login by extending it instead of `EmailIdpBaseEndpoint`. The password based endpoint is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`ServerpodCloudEmailIdpConfig` sets `sendOtpLoginVerificationCode`, sending the codes with the sign-up email type. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Generated by `serverpod generate` for the new models and `EmailOtpIdpBaseEndpoint`, without manual changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ontroller` Move the state, text inputs and requests the email flows have in common, such as the password login, the password reset and the guarded request handling, into the abstract `EmailFlowController`, parameterized by the screens of a flow. `EmailAuthController` extends it without changes to its public interface or behavior, so a passwordless flow can reuse the shared parts without depending on the password based flow. `dispose` now also cancels the debounce timer of the email validation. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Move the verification, password reset, set password, email field and back to sign-in forms into internal shared forms working with any `EmailFlowController`. The public forms keep their signatures and wrap the shared ones. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`EmailSignInTexts` gains `resendCode`, read by `ResendCodeButton` from the localization provider instead of a hard-coded English text. It defaults to the previous text. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Users enter their email and receive a one-time password. Verifying it signs in existing accounts, while emails without an account continue to a screen to accept the terms and create one, with an optional password if the server allows it. Apps can also let users sign in with their password, and reset it, from the start screen with `allowPasswordSignIn` on the widget or `EmailOtpAuthController`, defaulting to `false`. - `EmailOtpAuthController` is a standalone controller on top of `EmailFlowController`, independent of the password based `EmailAuthController`. It requires a server endpoint extending `EmailOtpIdpBaseEndpoint`. Its default email validation accepts email aliases. - If completing the sign-in fails, verifying again retries the completion without the used code, and asks for a new code once the server rejects it (`OtpLoginRejectedException`). - `EmailWithOtpSignInTexts` holds the texts of the flow, set through `SignInLocalizationProvider.emailWithOtp`. - The forms of the flow place the action right below their content and grow with it, so expanding fields, wrapped texts and narrow layouts do not overflow. The password field on the start screen expands in place, and the action button cross-fades its label. - The email account request errors no longer mention registrations, since the passwordless sign-in raises them too. Co-authored-by: Cursor Agent <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…lable `SignInWidget` shows `EmailOtpSignInWidget` when the server exposes an endpoint extending `EmailOtpIdpBaseEndpoint`, detected with the new `hasEmailOtp` of the client's identity providers, and `EmailSignInWidget` otherwise. Upgrading a server endpoint is all it takes to switch an app to the passwordless flow. Pass `emailOtpSignInWidget` to customize the widget. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
When the app is resumed, the verification code input fills in a code of the right length from the clipboard, which completes it right away. The code of a previous sign-in, still in the clipboard, was therefore submitted as soon as the user returned to the app, and counted as a failed attempt. Codes completed in any verification code input of the app are now skipped. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The email stories are grouped into `Email/OTP and Password` for the passwordless flow and `Email/Password Only` for the password based flow, backed by an in-memory mock of both email endpoints. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The `EmailIdpEndpoint` of the project template extends `EmailOtpIdpBaseEndpoint`, so new projects offer the passwordless sign-in, with the password sign-in still available. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The example server extends `EmailOtpIdpBaseEndpoint` and logs the one-time passwords, and the example app customizes the texts of the passwordless flow. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…kill The code length is not sent to the client, so the skill explains that the server's `otpLoginVerificationCodeGenerator` and the widget's `otpVerificationCodeConfig` must be changed together. Both default to 6 digits. Co-authored-by: Cursor Agent <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Generated by `serverpod generate` after extending `EmailOtpIdpBaseEndpoint` in the example server, without manual changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Created by `serverpod create-migration` for the `serverpod_auth_idp_email_account_otp_login_request` table, in the auth module, the projects depending on it and the auth example, without manual changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ation (review) The completion of a passwordless login has its own expiry check, but no test covered `finishOtpLogin` or `finishOtpRegistration` on a request that expired after its code was verified, unlike the registration and password reset flows. Both now throw `EmailAccountRequestException` with reason `expired`, delete the request and, for the registration, do not create an account. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2602f08 to
3479410
Compare
The default validation of both email flows now accepts email aliases, e.g. [email protected], instead of only the passwordless flow. Aliases are commonly used, e.g. to test a deployed app with several accounts on one inbox, and rejecting them is not needed for a secure sign-in. `EmailFlowController.validateEmail` and `EmailAuthController.validateEmail` now default `allowEmailAliases` to `true`, so `EmailOtpAuthController` no longer overrides the validation. Apps can still reject aliases with a custom `emailValidation`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
marcelomendoncasoares
left a comment
There was a problem hiding this comment.
Implementation looks very solid! Found only one small bug on the workflow that is throwing the wrong exception. Most of my comments are around UX, consistency with the IDP contract, and integration with the existing EmailIdp.
On the Flutter side, my main concern is that, although the components are shared, they’re wrapped in separate screen implementations with different spacing and layouts. We will end up maintaining two versions of very similar screens, and the new flow loses some UX consistency. I'd favor a bit more reuse of the existing code and keep the differences limited to the OTP flow needs if possible.
Like the other `*Texts` classes, the constructor requires every text, so apps localizing the passwordless flow can not miss one. The English texts move to `EmailWithOtpSignInTexts.defaults`, and partial overrides use `copyWith`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…(review) The title, terms checkbox and action already explain the screen, which now matches the password based registration and has one less text to localize. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ssage (review) The default verification message of the passwordless flow now uses the same wording as the password based flow. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…y (review) `EmailSignInTexts` is back to its interface on `main`, so the password based flow is unchanged. `ResendCodeButton` takes an optional `label`, defaulting to the text it had hard-coded, now exposed as `ResendCodeButton.defaultLabel`. The passwordless flow passes `EmailWithOtpSignInTexts.resendCode` on its verification screens, whose default refers to that constant, so the English text is defined once. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… flow (review) The test checking the controller's type hierarchy and the assertion on the gap below the email field only pinned implementation details, not behavior. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The scenarios of the `SignInWidget` detection tests were split between the group and the "when" of each test. Each test now states its own given. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…rdless flow (review) The passwordless screens use `FormStandardLayout` like the password based screens, so the title and the action button stay at the same positions while proceeding through the flow, and the password sign-in matches the spacing of the password login. Content that does not fit the form, e.g. on narrow screens or with the password requirements expanded, scrolls instead of overflowing. `FormInlineActionLayout` is removed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Tokens from passwordless logins used a separate `email_otp` method, which overloaded the identity provider's method as token metadata: it was not reserved by the provider registration, and code listing or revoking tokens by `EmailIdp.method` missed these sessions. Both ways of signing in now issue tokens with `EmailIdp.method`, so the password reset revokes them with its single revocation as before. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…iew) Generated by `serverpod generate` for the updated doc comments of `EmailOtpIdpBaseEndpoint`, without manual changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… (review) Two concurrent `finishOtpLogin` or `finishOtpRegistration` calls with the same token both passed the completion challenge. The second then failed deleting the already deleted request with a `DatabaseUnexpectedResultException` instead of the expected `invalid` reason. The request is now deleted with a check of the deleted rows, which throws `EmailAccountRequestNotFoundException` for the losing call, so it reports `invalid` while still completing a single login per verified code. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The password reset sender is only needed when users can sign in with a password, and the registration sender serves the password based registration and the fallback for one-time passwords. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e (review) The example app enables the password sign-in of `EmailOtpSignInWidget`, and the example server lets users set a password when creating an account. The comments on the email senders now describe when the passwordless setup uses each of them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
0307fc9 to
2b6154d
Compare
Adds passwordless login with a one-time password (OTP) sent by email to the existing email identity provider. Servers opt in by extending the new
EmailOtpIdpBaseEndpointinstead ofEmailIdpBaseEndpoint.SignInWidgetdetects that endpoint and shows the newEmailOtpSignInWidget. The existing password flow,EmailIdpBaseEndpoint,EmailSignInWidgetandEmailAuthControllerkeep working as before, so this can ship in a minor version.New projects created from the template use the OTP flow by default. OTP proves ownership of the email on every sign-in, and anyone with access to the inbox could reset the password anyway. Existing apps keep the password flow until their server endpoint extends
EmailOtpIdpBaseEndpoint.Fixes #2100
Important
Opinions wanted: OTP codes fall back to the registration sender.
To upgrade, an app should only need to extend
EmailOtpIdpBaseEndpoint, regenerate and migrate. Apps with custom email senders don't have the newEmailIdpConfig.sendOtpLoginVerificationCodeyet. So when it is not set, the codes are sent with the app's existingsendRegistrationVerificationCode, passing the OTP login request ID as itsaccountRequestId, and a warning is logged on first use. If neither sender is set, requesting a code fails with aStateError.accountRequestId, e.g. to complete a registration, get links that don't work for a login request.sendOtpLoginVerificationCode.ServerpodCloudEmailIdpConfig, used by new projects, already does.How it works
allowPasswordSignIn: trueonEmailOtpSignInWidgetorEmailOtpAuthController. It defaults tofalse.defaultOtpVerificationCodeGenerator). To change it, setEmailIdpConfig.otpLoginVerificationCodeGeneratoron the server andotpVerificationCodeConfigonEmailOtpSignInWidget(see the decision on the code length below). Verification follows the existing secret challenge pattern and returns a completion token.finishOtpLogin). Otherwise the user accepts the terms and, if the server allows it, can expand an optional password field before the account is created (finishOtpRegistration). The server setting isEmailIdpConfig.allowPasswordOnOtpRegistration, which defaults tofalse, and it is returned with the verification result.Tokens and sessions created through OTP use the same
emailmethod (EmailIdp.method) as password logins, since both authenticate the sameEmailAccountthrough the same identity provider. A password reset therefore revokes them as before. Recording how a session was authenticated, e.g. to audit whether an account was accessed through its password or the inbox, is left for a follow-up as session metadata in core.Decision: the code length is not sent by the server
The server and the client configure the code independently, the same way as the existing registration and password reset codes, so both have to be changed together. Sending the length with each code request was tried and dropped: the client would still need the allowed characters and other input settings from its own configuration, so the server can't take over the configuration, and keeping the existing pattern is simpler. The
serverpod-authskill and the doc comments point out that both sides must match.Server
EmailAccountOtpLoginRequesttable.EmailOtpIdpBaseEndpoint, extendingEmailIdpBaseEndpoint, withstartOtpLogin,verifyOtpLoginCode,finishOtpLoginandfinishOtpRegistration.EmailIdpBaseEndpointand its generated client class are unchanged.EmailIdpConfigoptions:sendOtpLoginVerificationCodecallback, falling back tosendRegistrationVerificationCode(see the highlighted question above)allowPasswordOnOtpRegistration, which defaults tofalsedefaultVerificationCodeGeneratorgains an optionallengthparameter. It still defaults to 8, so registration and password reset codes are unchanged.EmailAccountRequestException, and the user facing messages for those errors are now worded neutrally, without mentioning registration.ServerpodCloudEmailIdpConfigsends the codes with the existingsignupemail type. The Cloud email client itself is unchanged.Flutter
SignInWidgetshowsEmailOtpSignInWidget, with its own client and callbacks, when the server exposes an endpoint extendingEmailOtpIdpBaseEndpoint, andEmailSignInWidgetotherwise.client.auth.idp.hasEmailOtpexposes the detection. A customized widget can still be passed asemailSignInWidgetoremailOtpSignInWidget.EmailOtpSignInWidgetand itsEmailOtpAuthControllerform a separate widget branch next toEmailSignInWidget.controlleror aclient.allowPasswordSignIn, on the widget or the controller. It defaults tofalse.[email protected]), which are commonly used to test a deployed app with several accounts on one inbox. Pass a customemailValidationto reject them, e.g. withvalidateEmail(email, allowEmailAliases: false).EmailFlowControllerbase holding the shared state and requests. The forms both flows use (verification, password reset, email field) are shared widgets, which the existing public forms delegate to with unchanged interfaces.EmailFlowScreen,EmailSignInWidgetand the public API ofEmailAuthControllerare unchanged.FormStandardLayout, so the title and the action button stay at the same positions through the flow. Content that does not fit, e.g. on narrow screens or with the password requirements expanded, scrolls instead of overflowing.OtpLoginRejectedException, whichconvertToUserFacingExceptionturns into a message asking for a new code.SignInLocalizationProvider(emailWithOtp: EmailWithOtpSignInTexts.defaults.copyWith(...)), next to the texts of the other widgets. Like the other*Textsclasses, all of its texts are required, with the English ones indefaults. The screens shared with the password flow useEmailSignInTexts, which is unchanged.ResendCodeButtongains an optionallabel, defaulting to its previously hard-coded text, which the OTP flow sets fromEmailWithOtpSignInTexts.resendCode.EmailOtpIdpBaseEndpoint, so their apps show the OTP widget without changes. The auth example also enables the password sign-in and passwords on account creation, to showcase the full flow. Theserverpod-authskill documents the setup.Email:Email/OTP and Passwordhas every screen of the new widget.Email/Password Onlyhas the existing password screens.Commits
The history was rebuilt on the latest
mainas if the final design had been written at once, without separate commits for fixes made during the review. The commits are meant to be reviewed one at a time:EmailIdp,EmailOtpIdpBaseEndpointand the Serverpod Cloud sender.EmailFlowControllerand shared forms refactors, which keep the password flow unchanged, the localizable resend text,EmailOtpSignInWidget, theSignInWidgetdetection, the clipboard fix (a bug that also exists onmain) and the storybook stories.serverpod-authskill.(review)address review comments, so they can be checked against the comment they answer:test: Cover finishing a passwordless login that expired after verification (review)feat: Accept email aliases in the default email validation (review)SignInWidgettest structure, the stable layout, theemailtoken method (with its generated code), the rejection of concurrent completions and the notes on the example senders.Generated code (
chore: Generate code ...) and migrations (chore: Add migrations ...) are in separate commits with no manual changes, so the server commits before the generated code commit don't compile on their own.Known limitations / follow-ups
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.Breaking changes
None for existing code. Some changes to be aware of:
EmailAuthControllernow extends the newEmailFlowControllerinstead ofChangeNotifierdirectly. Its public API is unchanged; it gains a few@protectedmembers used by the base class.EmailSignInWidgetandEmailAuthControlleraccepts email aliases.EmailAuthController.validateEmailandEmailFlowController.validateEmailnow defaultallowEmailAliasestotrue; apps that relied on rejecting aliases passallowEmailAliases: falsein a customemailValidation.🤖 Generated with Claude Code