How to implement automatic sign in from a link?
How to implement automatic sign in from a link?
Context
For my issue we are implementing a custom provider. In our flow, when a user signs up for an account they get an email back with a verification link. When a user clicks on that link they are sent back to our site, their email is verified and then they automatically signed in without having to enter their credentials.
They way this has been setup is the team I am working with sends back a single use url in the verification response. That url looks something like this:
https://<auth_service_domain>/oauth2/authorize?response_type=code&client_id=<client_id>&redirect_uri=https://<app_domain>/api/auth/callback/<provider_id>&scope=openid&uuid=<uuid>&uid=<uid>&state=<state_string>
Basically what happens is that the api makes a request to the token endpoint for us, and then gives us a link to redirect to in order to sign in the user.
The issue I am getting is (obviously) state and expected state in the auth process don't match:
[next-auth][debug][oauth_callback_protection] Comparing received and expected state {
state: 'c923d39e-c887-49d5-80c1-6f774ce3e013',
expectedState: '224926d03516a454dc90d55c1454ae369418c387ed35d0b5da1961ed1ab8c498'
}
Summary
I have tried making a call to the /api/auth/csrf endpoint and overriding their sent state but I clearly don't really know how everything is supposed to work together as that string is different from the one matched when signing in.
If I could get a nudge in the right direction on how to implement this I would really appreciate it! I couldn't find anything in the docs or on the discussion board related to this.
I'm not sure you want to start the login flow outside of the app. Without being able to tell a good reason, it feels unsafe. What you could do is simply create a link like https://<app_domain>?login
And use some client-side logic that triggers the signIn("provider") function when the login param is present in the url.