# Logging in

Logging in here lets these pages run requests for you. For a script, create a token of its own
instead: [Creating a token for a script](/docs/login/script-token.md).

## How it works

1. Select **Log in**. The page asks the login server for a link.
2. Open the link, in another tab or on another device, log in with your reconnectchina.org
   account and confirm.
3. This page notices by itself and is logged in. Every operation page can now run its request,
   and **Copy with your token** fills your access token into its curl command.

No password passes through this page: you enter it on account.reconnectchina.org only. We
warmly recommend turning on two-factor authentication there. It is optional, and it keeps a
stolen password from being enough.

You can log in as soon as you have registered. The API answers once we have reviewed your
registration and your account is in an organization. Until then a request is refused with `403`
and the problem type [`no-organization`](/docs/v1/problems/no-organization.md), and the panel says
what that means under the answer.

The samples on the right log in the same way from a notebook, a Python shell or a terminal:
each prints the link and waits until you have confirmed. The operation pages' Python and Node
samples then run on their `client` ([Client libraries](/docs/clients.md)). The shell sample saves
the refresh token in `reconnectchina-token`, where [Access tokens](/docs/login/access-token.md) and
[Logging out](/docs/login/logout.md) read it.

Below them, the panel shows this page's own two requests as they are made: the first asks for the link,
the second is repeated every few seconds until you have confirmed, and then answers with the
tokens.

## What this login is

It lasts as long as your session at reconnectchina.org, and ends with this tab: the page keeps
it in the tab's own storage. A new tab, and every API host, is a login of its
own, so the sandbox and production never share one.

The page holds an access token, which it sends with each request and replaces every few
minutes, and a refresh token, from which it makes the next one
([Access tokens](/docs/login/access-token.md)). Neither is ever part
of a link, and the page shows either only masked.

Log out when you are done ([Logging out](/docs/login/logout.md)). Closing the tab forgets the login too, and it ends
by itself with your session.

## Samples

Console programs that do what this page is about, logging in at the API's own /oauth. The Shell ones need curl and jq.

Every API host serves these pages for its own API. The samples are written for production, https://api.reconnectchina.org, as the pages are before they know their host. On another host, such as the sandbox, use that host instead: in curl's URL and the Shell samples' `login=` line, and as `server` for the Python and Node clients, which log in there too.

### Python

```python
from reconnectchina import Client, ConsolePrompt

# To try things out: a login that ends with your session.
client = Client(
    prompt=ConsolePrompt(),
)
client.login()
```

### Shell

```sh
login=https://api.reconnectchina.org/oauth
device=$(curl -s "$login/device" -d client_id=reconnectchina-api)
echo "$device" | jq -r '"Open \(.verification_uri_complete) to log in."'
interval=$(echo "$device" | jq -r '.interval // 5')
while sleep "$interval"; do
  answer=$(echo "$device" | jq -r '"device_code=\(.device_code)"' |
    curl -s "$login/token" -d client_id=reconnectchina-api -d grant_type=urn:ietf:params:oauth:grant-type:device_code -d @-)
  case $(echo "$answer" | jq -r '.error // empty') in
    '') (umask 077 && echo "$answer" | jq -r .refresh_token > reconnectchina-token); break ;;
    authorization_pending) ;;
    slow_down) interval=$((interval + 5)) ;;
    *) echo "$answer" >&2; break ;;
  esac
done
```

### Node

```js
import { Client } from "@reconnectchina/client";

// To try things out: a login that ends with your session.
const client = new Client();
await client.login({
  offline: false,
  prompt: ({ link }) => console.log(`Open ${link} to log in.`),
});
```
