mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-13 21:01:21 +08:00
Some checks failed
all-good: Did all the other checks pass? / all-good (push) Has been cancelled
Ensure Prisma migrations are in sync with the schema / check_prisma_migrations (22.x) (push) Has been cancelled
DB migration compat / Check if migrations changed (push) Has been cancelled
Docker Server Build and Push / Docker Build and Push Server (push) Has been cancelled
Docker Server Build and Run / docker (push) Has been cancelled
Runs E2E API Tests (Local Emulator) / E2E Tests (Local Emulator, Node ${{ matrix.node-version }}) (22.x) (push) Has been cancelled
Runs E2E API Tests / E2E Tests (Node ${{ matrix.node-version }}, Freestyle ${{ matrix.freestyle-mode }}) (mock, 22.x) (push) Has been cancelled
Runs E2E API Tests / E2E Tests (Node ${{ matrix.node-version }}, Freestyle ${{ matrix.freestyle-mode }}) (prod, 22.x) (push) Has been cancelled
Runs E2E API Tests with custom port prefix / build (22.x) (push) Has been cancelled
Lint & build / lint_and_build (latest) (push) Has been cancelled
Dev Environment Test With Custom Base Port / restart-dev-and-test-with-custom-base-port (push) Has been cancelled
Dev Environment Test / restart-dev-and-test (push) Has been cancelled
Run setup tests with custom base port / setup-tests-with-custom-base-port (push) Has been cancelled
Run setup tests / setup-tests (push) Has been cancelled
TOC Generator / TOC Generator (push) Has been cancelled
DB migration compat / Back-compat — Current branch migrations with ${{ needs.check-migrations-changed.outputs.base_branch }} branch code (push) Has been cancelled
DB migration compat / Forward-compat — Current branch code with ${{ needs.check-migrations-changed.outputs.base_branch }} branch migrations (push) Has been cancelled
DB migration compat / No migration changes (skipped) (push) Has been cancelled
62 lines
2.0 KiB
Plaintext
62 lines
2.0 KiB
Plaintext
---
|
|
title: CLI Authentication
|
|
description: How to authenticate a command line application using Stack Auth
|
|
---
|
|
|
|
If you're building a command line application that runs in a terminal, you can use Stack Auth to let your users log in to their accounts.
|
|
|
|
To do so, we provide a Python template that you can use as a starting point. [Download it here](https://github.com/stack-auth/stack-auth/tree/main/docs/public/stack-auth-cli-template.py) and copy it into your project, for example:
|
|
|
|
```
|
|
└─ my-python-app
|
|
├─ main.py
|
|
└─ stack_auth_cli_template.py # <- the file you just downloaded (rename to use underscores for Python import)
|
|
```
|
|
|
|
Then, you can import the `prompt_cli_login` function:
|
|
|
|
```py
|
|
from stack_auth_cli_template import prompt_cli_login
|
|
|
|
# prompt the user to log in
|
|
refresh_token = prompt_cli_login(
|
|
app_url="https://your-app-url.example.com",
|
|
project_id="your-project-id-here",
|
|
publishable_client_key="your-publishable-client-key-here",
|
|
)
|
|
|
|
if refresh_token is None:
|
|
print("User cancelled the login process. Exiting")
|
|
exit(1)
|
|
|
|
# you can also store the refresh token in a file, and only prompt the user to log in if the file doesn't exist
|
|
|
|
# you can now use the REST API with the refresh token
|
|
def stack_auth_request(method, endpoint, **kwargs):
|
|
# ... see Stack Auth's Getting Started section to see how this function should look like
|
|
# https://docs.stack-auth.com/python/getting-started/setup
|
|
|
|
def get_access_token(refresh_token):
|
|
access_token_response = stack_auth_request(
|
|
'post',
|
|
'/api/v1/auth/sessions/current/refresh',
|
|
headers={
|
|
'x-stack-refresh-token': refresh_token,
|
|
}
|
|
)
|
|
|
|
return access_token_response['access_token']
|
|
|
|
def get_user_object(access_token):
|
|
return stack_auth_request(
|
|
'get',
|
|
'/api/v1/users/me',
|
|
headers={
|
|
'x-stack-access-token': access_token,
|
|
}
|
|
)
|
|
|
|
user = get_user_object(get_access_token(refresh_token))
|
|
print("The user is logged in as", user['display_name'] or user['primary_email'])
|
|
```
|