Authentication a Dev Hub from a VS Code hosted via GitHub CodeSpaces

code-buildercodespacesdev-hubvs-code

GitHub recently released CodeSpaces which allows you to run VSCode in the cloud, which I believe the up coming Salesforce Code Builder will be based on.

I am trying to get it to work on my Chromebook, but I am struggling to authenticate with my dev hub.

Below are the steps completed so far:

  1. Enabled Codespaces on our GitHub account, under Organisations (free until Sept 10th 2021)
  2. Created a GitHub repo
  3. From the Code drop-down menu, and in the Codespaces tab, I've created a New Codespace
  4. Waiting a couple of moments for it to load
  5. Boom! it's working
  6. Installed the Salesforce Extension Pack
  7. Installed the Salesforce CLI using the Terminal using command npm install sfdx-cli --global
  8. Create a project using the command palette

Next I want to authenticate with my Dev Hub, using command palette, I run the usual command, and authenicate against my org.

But the redirect back goes to:

http://localhost:1717/OauthRedirect?code=XXXXXXXXXXXXXXXXX%3D%3D&state=XXXXXXXXXXXX

Looking in the hosted VSCode, it's created a port forward for port 1717 with a custom URL:

https://me-mycompany-my-codespace-test-9vxgvj9g29rq-1717.githubpreview.dev/

Therefore, I guess the OAuth redirect isn't making it back?

Any idea how to make this work?

Best Answer

You can do this via any of the below methods without making the port entry public,

  1. Create your own connected app in DevHub instead of using the default one that comes with the Salesforce CLI and have a redirect URL in the connected app equal to the codespace domain URL.

    sfdx auth:web:login -i <consumer key of your new connected app>

or

  1. Simply use Device login flow for the CodeSpaces. The advantage of this is you do not have create your own connected app for this one.

    sfdx auth:device:login

Related Topic