code_verifier, is generated and stored by the app on the user’s device. The verifier then is hashed to produce a code_challenge. This challenge is passed along with the user’s email to Stytch when the user is asked to verify their identity.
Stytch sends the user an email containing a magic link. When the user clicks on that link, the user is redirected back to the app with a secure code. Next, the app submits the code along with the original code_verifier. Stytch hashes the submitted code_verifier and validates that it matches the code_challenge passed as a query parameter earlier. This check ensures that the token has not been leaked, and that it is being handled by the same user that was seen at the start of the flow.
This guide will show you how to add PKCE support to your Stytch magic link integration for added security. A PKCE integrations is required for applications that use native callback URLs, for example appname://some/callback on mobile. Stytch’s support for PKCE differs slightly from the original specification - for example we do not permit plain verifiers to be used - but the original idea of strengthening app security with a unique secret on every request remains the same.
If you get stuck while working through this guide, feel free to ask questions via support@stytch.com or in our Slack community.
Confirm PKCE makes sense for your application
PKCE provides cryptographic assurance that a login flow starts and ends on the same device. While this is almost always a desired property, there is one notable exception to the rule: mobile webviews. Consider the following scenario:
- A user sees a link to your application embedded in another mobile application, such as Twitter or Slack.
- The user clicks the link and the application is opened inside a webview owned by the parent application.
- The user is logged out by default, because cookies and session state are not shared between webviews and the native mobile browser.
- The user attempts to log in via an email magic link. The PKCE code_verifier is generated, and stored inside the webview.
- The user leaves the initial webview and opens their email application. They click the magic link in the email, and your application is opened again, this time in the native mobile browser.
- Your application attempts to submit the magic link token, but does not have access to the original code_verifier. The user fails to log in with a pkce_expected_code_verifier error, and needs to request a new magic link from the native mobile browser. Depending on your application’s user base, mobile webviews could make up for a significant amount of traffic, and PKCE would add friction to that user experience. For cases like these, you might choose to omit PKCE and supplement your login flow with a second factor like SMS or WebAuthn instead.
Enable PKCE for Magic Links
First, enable PKCE for the JavaScript SDK. In order to do so, toggle the option in the Frontend SDK page of the Stytch Dashboard. Once this configuration is set, the SDK will automatically start applying PKCE to all magic link login flows it creates.
Send the Magic Link
Call the magicLinks.email.loginOrCreate() method to start the magic link flow. The SDK will generate a code_verifier and store it in local storage before asking Stytch to send the email. If you use the SDK’s built in Magic Link UI this step is already handled for you.
Authenticate the Magic Link token
At the route that the user is redirected to after the Magic Link flow, pull the token out of the URL params and call magicLinks.authenticate(token) to exchange the token for a Stytch session. The SDK will automatically retrieve the code_verifier created earlier from local storage and pass it on to the Stytch servers.
You're done
You just finished all the critical components to authenticate your users via magic links with PKCE! Have any feedback after integrating? Get in touch with us and tell us what you think by contacting us.