# How to use basic auth middleware?

**URL:** <https://community.traefik.io/t/how-to-use-basic-auth-middleware/6415>\
**Category:** Traefik v2\
**Tags:** middleware\
**Created:** [June 7, 2020, 3:57pm UTC](https://community.traefik.io/t/how-to-use-basic-auth-middleware/6415 "2020-06-07T15:57:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxon755](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@maxon755](https://community.traefik.io/u/maxon755)\
**Post date:** [June 7, 2020, 3:57pm UTC](https://community.traefik.io/t/how-to-use-basic-auth-middleware/6415/1 "2020-06-07T15:57:34Z")

</div>

I am trying to set up the basic auth with a docker provider.  
I've added auth label from documentation as is to the service, but there is no auth prompt in the browser.

docker-compose.yml

```auto
version: '3'

services:
  reverse-proxy:
    # The official v2.0 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --api.insecure=true
      - --providers.docker
      - --accesslog=true
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      - .:/app

  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=PathPrefix(`/`)
      - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"  

```

The labels were properly added to the container:

```auto
            "Labels": {
                "com.docker.compose.config-hash": "446f86d3954c8693a3e1c245593062cf0de299568fb5e1af27e1eb910b4e13fe",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "traefik",
                "com.docker.compose.project.config_files": "docker-compose.yml",
                "com.docker.compose.project.working_dir": "/home/maks/Learning/traefik",
                "com.docker.compose.service": "whoami",
                "com.docker.compose.version": "1.25.5",
                "traefik.http.middlewares.test-auth.basicauth.users": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
                "traefik.http.routers.whoami.rule": "PathPrefix(`/`)"
            }

```

I have also tried with different parameters and in different projects. Result the same - nothing happens.  
What I am doing wrong? 😂

---

<div class="post-metadata">

**Author:** ![maxon755](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@maxon755](https://community.traefik.io/u/maxon755)\
**Post date:** [December 12, 2020, 8:42am UTC](https://community.traefik.io/t/how-to-use-basic-auth-middleware/6415/2 "2020-12-12T08:42:09Z")

</div>

Ok, my problem was that line

```auto
- "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"

```

only creates a middleware, but it should be explicitly applied to the router by:

```auto
- traefik.http.routers.whoami.middlewares=test-auth

```

So the final configuration of whoami service:

```auto
  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=PathPrefix(`/`)
      - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0" 
     - traefik.http.routers.whoami.middlewares=test-auth

```

that's what I missed from the middleware overview documentation page 🤣
