helper functions supertokens-website to supertoken...
# support-questions
r
helper functions supertokens-website to supertokens-web-js
We have some more questions about the old helper functions moving from
supertokens-web-js
to
supertokens-website
. I will make a thread!
In
supertokens-website
we had to manually call the APIs. For the third party login, I used the function:
Copy code
export const getThirdPartyAuthUrl = async ({
  thirdPartyId,
}: {
  thirdPartyId: 'google' | 'facebook' | 'apple'
}): Promise<GetThirdPartyAuthUrlResponse> =>
  fetch(
    `${url}/api/auth/authorisationurl?thirdPartyId=${thirdPartyId}`,
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        rid: 'thirdpartyemailpassword',
      },
    },
  ).then(res => res.json())
And then onThirdPartyLogin I would call:
Copy code
export const onThirdPartyLogin = async ({
  provider,
  platform,
  repository,
}: {
  repository: Repository
  provider: 'google' | 'facebook' | 'apple'
  platform: Platform
}) => {
  const response = await getThirdPartyAuthUrl({
    thirdPartyId: provider,
    repository,
  })

  if (response.status === 'OK') {
    const url = new URL(response.url)
    if (platform === 'APP') {
      console.log('app came here')
      url.searchParams.append(
        'redirect_uri',
        `${
          repository === 'CONTRACTOR'
            ? REDIRECT_URL_CONTRACTOR
            : REDIRECT_URL_CLIENT
        }/api/auth/redirect?provider=${provider}`,
      )


      window.open(url)
    }
    if (platform !== 'APP') {
      url.searchParams.append(
        'redirect_uri',
        `${
          repository === 'CONTRACTOR'
            ? FRONTEND_URL_CONTRACTOR
            : FRONTEND_URL_CLIENT
        }/auth/callback/${provider}`,
      )
      window.location.href = url.href
    }
  }

  if (response.status !== 'OK') {
    alert(`Er is iets misgegaan met inloggen via ${provider}, probeer opnieuw`)
  }
}
Because I am using Capacitor as well, I had to have different redirect URI's, I couldn't just redirect to the callback, had to go to API first. Should I overwrite in
supertokens-web-js
or something? And what functions should I use?
Thank you for your time and we look forward to your answer 🌈
n
Hi
For the
CookieHandlerInterface
and
WindowHandlerInterface
, the
supertokens-web-js
SDK inherits types from
supertokens-website
so you can keep that as it is.
supertokens-website
is a dependency of
supertokens-web-js
so the import will still be available after migrating
r
Thanks for the clarification
What about the helper functions?
n
You can replace the manual API calls with function calls exposed by the web js SDK. For example:
Copy code
export const getThirdPartyAuthUrl = async ({
  thirdPartyId,
}: {
  thirdPartyId: 'google' | 'facebook' | 'apple'
}): Promise<GetThirdPartyAuthUrlResponse> =>
  fetch(
    `${url}/api/auth/authorisationurl?thirdPartyId=${thirdPartyId}`,
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        rid: 'thirdpartyemailpassword',
      },
    },
  ).then(res => res.json())
Can be replaced by
Copy code
import ThirdPartyEmailPassword from "supertokens-web-js/recipe/thirdpartyemailpassword"

async function getThirdPartyURL(thirdPartyId: 'google' | 'facebook' | 'apple') {
  return await ThirdPartyEmailPassword.getAuthorisationURLWithQueryParamsAndSetState({
    providerId: thirdPartyId,
    authorisationURL: "<REDIRECT_URI_FOR_SOCIAL_LOGIN>"
  })
}
So you should never need to call the APIs manually anymore
r
Thanks so much! I’ll try it out, this Discord makes supertokens a charm to work with. I will try to get the third party login to work, also on capacitor (Apple/android). If I run into issues I’ll post a message here for people in the future to see.
n
Thanks for the kind words! And yep we are happy to help