We don't have a dedicated recipe for CLI login, but you can use our passwordless recipe to get this to work.
The flow would be as follows:
- Create an API that the CLI can call. In this API, you want to call a function from our backend SDK to generate a magic link and send it to the CLI.
- The link contains a one time use token and another ID called
preAuthSessionId
. You want to save the
preAuthSessionId
on the CLI and query an API that checks if the
preAuthSessionId
has been consumed or not.
- When the user clicks on the displayed magic link, it would open the browser.
- Then, on that page, you would check if a session already exists (using our frontend SDK). If it does, you want to call an API giving it the one time use token + the
preAuthSessionId
. In the API, you can do session verification to get the userID and consume the one time use token (using our backend SDK). If the one time use token was consumed successfully, you want to map the
preAuthSessionId
to the userID in your own db.
- If the user is not logged in, you can store the one time use token +
preAuthSessionId
in localstorage and ask them to login. Post login, you would do as per the previous step.
- Finally, the API that the CLI is calling would eventually see that there is a userID mapped to the
preAuthSessionId
, which it could return to the CLI.
One catch here is that to generate the magic link in the first place, you would need to provide an email to the backend SDK function. You can give it a random / unique email and then delete this user once the magic link code is consumed.