Search⌘ K
AI Features

Creating the Authentication API

Explore how to create an authentication API in Flask leveraging JWT tokens to handle user registration, login, and secure access. Understand token creation, validation, and managing user sessions without storing passwords directly in the database.

The script goes in the langman directory, and we run it with:

$ export FLASK_ENV=dev_lite
$ export FLASK_APP=server.run_auth_api
$ pipenv run flask run --host=0.0.0.0 --port=3000

Using httpie

Now we can play with it using httpie.

Register new account

First, we register a new account with the username SomeName and password Secret.

$ http POST 0.0.0.0:3000/auth username=SomeName password=Secret

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODAwMDc0NDgsIm5iZiI6MTU4MDAwNzQ0OCwianRpIjoiNTIyM2UzZjYtZmU2MS00ZDYzLWI5NzItYjM1Y2U1NjIwMGE1IiwiZXhwIjoxNTgwMDkzODQ4LCJpZGVudGl0eSI6IjUwZDgyM2FmLTRmYmYtM2M3MS05OWUyLTNhNzQ5NzhlMjA1MCIsImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyIsInVzZXJfY2xhaW1zIjp7ImFjY2VzcyI6InBsYXllciIsIm5hbWUiOiJTb21lTmFtZSJ9fQ.k6qaZ0LQyghPKePHD46j3EuFjNh6yA7ugOYepy7GvuE"
}

We can also log in using the httpie. This produces a token, too, but it’s a different one: ...