Using @turnkey/sdk-react
Setting up social linking with @turnkey/sdk-react using the<Auth/>
component is straightforward.
Simply add the socialLinking
property to your authConfig
object and set it to true
. This will enable social linking for your application.
handleOAuthLogin
function within the Auth
component in the @turnkey/sdk-react GitHub repository.
Manual Implementation
If you prefer to implement social linking manually, you can follow this guide to create a backendhandleOAuthLogin
function / endpoint that can optionally allow for social linking. Code references are in TypeScript, but you can easily adapt this to your backend language of choice.
Parameters
This function will accept the following parametersoidcToken
: The OIDC token received from the social login provider.providerName
: The name of the social login provider (e.g., “google”, “apple”, etc.).publicKey
: A public key generated by the client.socialLinking
: A boolean indicating whether social linking is enabled. Defaults tofalse
.
Parse the OIDC Token
Extract theemail
and issuer
(iss
) from the user’s OIDC token.
Look Up Sub-Orgs by OIDC Token
Check if this OIDC token is already linked to a sub-org user within your organization. If it is, you can simply continue to the OAuth login step.If No OIDC Match and Google Issuer, Try Finding the Email
If the token is from Google and has a valid email, try getting the sub-org by verified email. If a sub-org is found, you can create the OAuth provider for the user. Note, this should only be done for social linking flows.If Still No Match, Create a New Sub-Organization
If no match is found, create a new sub-org and include the email in the payload for a social linking flow. The email passed in will be marked as verified provided the OIDC token comes from Google and the email within the token matches the email passed in.Complete the OAuth Login
Finally, perform the OAuth login using theoidcToken
, publicKey
, and the determined organizationId
.
Final Implementation
Here’s the complete implementation of thehandleOAuthLogin
function as well as a helper function to create a sub-organization (can also be implemented inline).