Advertisement
Guest User

Untitled

a guest
May 1st, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function fetchOAuthRedirect(oAuthProvider) {
  2.   return await fetch(
  3.     `http://localhost:8080/api/auth/authorisationurl?thirdPartyId=${oAuthProvider}`,
  4.     {
  5.       method: 'GET',
  6.       headers: {
  7.         rid: 'thirdparty',
  8.       },
  9.     },
  10.   ).then((response) => response.json());
  11. }
  12.  
  13. function appendRedirectUri(url, oAuthProvider) {
  14.   let urlObj = new URL(url);
  15.   urlObj.searchParams.append(
  16.     'redirect_uri',
  17.     `http://localhost:8080/api/auth/callback/${oAuthProvider}`,
  18.   );
  19.   return urlObj.toString();
  20. }
  21.  
  22. async function doSignInUp(code, thirdPartyId) {
  23.   return await fetch('http://localhost:8080/api/auth/signinup', {
  24.     method: 'POST',
  25.     headers: {
  26.       rid: 'thirdparty',
  27.       'Content-Type': 'application/json',
  28.     },
  29.     body: JSON.stringify({
  30.       code,
  31.       redirectURI: 'http://localhost:8080/auth/callback/github',
  32.       thirdPartyId: thirdPartyId,
  33.     }),
  34.   });
  35. }
  36.  
  37. async function signinup(oAuthProvider) {
  38.   const redirect = await fetchOAuthRedirect(oAuthProvider);
  39.   window.location.href = appendRedirectUri(redirect.url, oAuthProvider);
  40.  
  41.   //I don't understand where exactly I should place code down below, cause it's obvious that it's unreachable after the user is redirected to the third party auth page.
  42.   let code = new URL(window.location.href).searchParams.get('code');
  43.   const response = await doSignInUp(code, oAuthProvider);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement