authkit: Access to fetch has been blocked by CORS policy

Sometimes timing couldn’t be more perfect, I’m so thrilled to be able to tap into the brainpower at WorkOS with AuthKit. I’m sure this problem is me. I’ve been following along the docs and the nextjs example, tried both the AuthKit version and Jason’s, thank you for the awesome post btw!

  • WorkOS dashboard set up done
  • All steps completed in the tutorial
  • I can see the token in my application tab
  • However, after I authenticate with Google, I get this https://rizv.io/i/JhAnbC, there is no redirect to the home page and I cannot see the logged in/auth view
  • Not sure if relevant, but I can see these errors in browser console:
Access to fetch at 'https://api.workos.com/user_management/authorize?client_id=client_01HGG60KYN9F25CHGM3TYV56WB&provider=authkit&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code' (redirected from 'http://localhost:3000/account?_rsc=acgkz') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Any help or guidance would be appreciated as I’m in the process of working on this. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Hi @adss332, please don’t re-open issues if your problem is different to the original issue.

In your case it’s likely because you are using AuthKit with the SSO API rather than UserManagement. Try generating the redirect URL with:

WorkOS\WorkOS::setApiKey("sk_test_123");
WorkOS\WorkOS::setClientId("client_123");

$userManagement = new WorkOS\UserManagement();

$authorizationUrl = $userManagement->getAuthorizationUrl(
    redirectUri: "http://localhost:8000/callback",
    provider: 'authkit',
);

Then get the user profile later with:

$user = $userManagement->authenticateWithCode(    
    clientId: "client_123",
    code: $code,    
);