Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Login via CLI #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 18, 2022
Merged

feat: Login via CLI #298

merged 19 commits into from
Feb 18, 2022

Conversation

bryphe-coder
Copy link
Contributor

@bryphe-coder bryphe-coder commented Feb 16, 2022

Fixes #210 - this isPR implements coder login in the case where the default user is already created.

This change adds:

  • A prompt in the case where there is not an initial user that opens the server URL + requests a session token
    • This ports over some code from v1 for the openURL and isWSL functions to support opening the browser
  • A /api/v2/api-keys endpoint that can be POST'd to in order to request a new api key for a user
    • This route was inspired by the v1 functionality
  • A cli-auth route + page that shows the generated api key
  • Tests for the new code + storybook for the new UI

The /cli-auth route, like in v1, is very minimal:

Screen Shot 2022-02-16 at 5 05 07 PM

And the terminal UX looks like this:

2022-02-16 17 13 29

TODO:

@codecov
Copy link

codecov bot commented Feb 16, 2022

Codecov Report

Merging #298 (0d829b3) into main (e5db936) will increase coverage by 2.66%.
The diff coverage is 52.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #298      +/-   ##
==========================================
+ Coverage   64.81%   67.47%   +2.66%     
==========================================
  Files          67      140      +73     
  Lines         756     7407    +6651     
  Branches       74       77       +3     
==========================================
+ Hits          490     4998    +4508     
- Misses        251     1899    +1648     
- Partials       15      510     +495     
Flag Coverage Δ
unittest-go-macos-latest 66.15% <55.20%> (?)
unittest-go-ubuntu-latest 67.09% <54.40%> (?)
unittest-go-windows-latest 65.89% <52.80%> (?)
unittest-js 63.61% <30.00%> (-1.21%) ⬇️
Impacted Files Coverage Δ
codersdk/users.go 61.22% <0.00%> (ø)
site/components/SignIn/index.tsx 0.00% <0.00%> (ø)
site/pages/cli-auth.tsx 0.00% <0.00%> (ø)
coderd/users.go 58.76% <52.63%> (ø)
cli/login.go 60.46% <67.14%> (ø)
coderd/coderd.go 95.28% <100.00%> (ø)
site/components/SignIn/CliAuthToken.tsx 100.00% <100.00%> (ø)
httpmw/userparam.go 76.66% <0.00%> (ø)
coderd/projectversion.go 55.22% <0.00%> (ø)
... and 69 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5db936...0d829b3. Read the comment docs.

@bryphe-coder bryphe-coder marked this pull request as ready for review February 17, 2022 01:14
@bryphe-coder bryphe-coder self-assigned this Feb 17, 2022
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nits. Overall, wonderful change!

cli/login.go Outdated
return nil
},
}
}

func saveSessionToken(cmd *cobra.Command, client *codersdk.Client, sessionToken string, serverURL *url.URL) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to rip this into a distinct function. We already validate the "me" user request above, so feels like that should be reused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative is to duplicate the persistence code both in the 'create initial user' and 'login' flows - inlined this function in 13696a7

Another thing we could consider is bringing the "me" check above (since it's only required for initial login), and factor out the common save-session-token-and-url logic into this function. Happy with whichever you prefer.

bryphe-coder added a commit that referenced this pull request Feb 17, 2022
…307)

Fixes #304 
Unblocks #298 

After logging in, the login flow should redirect to a whatever path is specified by the `?redirect` query parameter. This is important for cases like #298 - where we need to set `?redirect=%2Fcli_auth`, but also really any case where the user is linked and might have to go back to the login screen.

The fix is simple - just check if the `redirect` query parameter is set, and if it is, use that as the path to redirect to on success. Also adds a test case - we had one checking that we redirect to the default (root `/`) url, but not one of the `?redirect` param
@bryphe-coder bryphe-coder merged commit 3f77814 into main Feb 18, 2022
@bryphe-coder bryphe-coder deleted the bryphe/feat/210/cli-login branch February 18, 2022 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI support for login
2 participants