Advertisement
lovestaco

OAuth signin only returning sub in userinfo API

May 24th, 2025 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import { Configuration } from "@goauthentik/api";
  2.  
  3. // import {
  4. //   CoreApi,
  5. //   CoreUsersListRequest,
  6. //   PoliciesApi,
  7. //   Policy,
  8. //   PolicyTestRequest,
  9. //   PolicyTestResult,
  10. //   User,
  11. // } from "@goauthentik/api";
  12.  
  13. const basePath = 'http://localhost:9000';
  14. const clientId = 'EU2Dko0AvWUflP2ZnjQbqINWrUEJMkiINJs5Onxd';
  15. const clientSecret = 'IUD4ioCmq4wAbNDmZ4Ta8zm6GALUXMNhEelgTOwi1XDUCPjfUvNuwRCN8m7Mad1QHTwDNzbebvoxCrZqPl3Y7OJmBhrwqK2xdBPZAbqQI23KnEBa5ARCW1rKmlkSFbmL';
  16. const tokenUrl = `${basePath}/application/o/token/`;
  17. const redirectUri = 'http://localhost:3000';
  18. const code = '852bc4b4088b41788936f704b3b2c3e9';
  19.  
  20. // Endpoint URL
  21. // Authorization    /application/o/authorize/
  22. // Token    /application/o/token/
  23. // User Info    /application/o/userinfo/
  24. // Token Revoke /application/o/revoke/
  25. // End Session  /application/o/<application slug>/end-session/
  26. // JWKS /application/o/<application slug>/jwks/
  27. // OpenID Configuration /application/o/<application slug>/.well-known/openid-configuration
  28.  
  29. async function fetchData() {
  30.     try {
  31.      
  32.      
  33.         const params = new URLSearchParams();
  34.         params.append('grant_type', 'authorization_code');
  35.         params.append('code', code);
  36.         params.append('redirect_uri', redirectUri);
  37.         params.append('client_id', clientId);
  38.         params.append('client_secret', clientSecret);
  39.         params.append('scope', 'api read_user openid profile email');
  40.        
  41.  
  42.         const tokenRes = await fetch(tokenUrl, {
  43.             method: 'POST',
  44.             headers: {
  45.                 'Content-Type': 'application/x-www-form-urlencoded'
  46.             },
  47.             body: params
  48.         });
  49.  
  50.         if (!tokenRes.ok) {
  51.             const err = await tokenRes.text();
  52.             throw new Error(`Token exchange failed: ${err}`);
  53.         }
  54.  
  55.         const tokenData = await tokenRes.json();
  56.         console.log(tokenData);
  57.         const accessToken = tokenData.access_token;
  58.  
  59.     //   // Create new config with token
  60.     //   const configWithToken = new Configuration({
  61.     //     basePath: basePath + '/api/v3',
  62.     //     accessToken: accessToken, // ✅ use this instead of setting headers manually
  63.        
  64.     // });
  65.           const userInfo = await fetch(`${basePath}/application/o/userinfo/`, {
  66.             headers: {
  67.               Authorization: `Bearer ${accessToken}`
  68.             }
  69.           });
  70.           console.log(await userInfo.json());
  71.  
  72.  
  73.     } catch (error) {
  74.         console.error('Error:', error);
  75.     }
  76. }
  77. fetchData();
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement