> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bhuwanpandey.com.np/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

`fastdaisy-admin` provides an optional authentication system for securing the admin panel.
To enable authentication mode, set `authentication=True` when initializing the `Admin` instance.

If you are satisfied with the default `User` table, you can create a superuser by running the following command:

```bash theme={null}
fastdaisy-admin createsuperuser

```

And also, you can provide file path which must contain `Admin` instance as

```python theme={null}
# app.py

from fastdaisy_admin.application import Admin
....

admin = Admin(
    app, 
    secret_key, 
    engine,
    authentication=True,
    auth_model=User
)
....

```

`filepath:` user/app.py

```bash theme={null}

fastdaisy-admin createsuperuser user/app.py
```

### Working with User table

By default, the `User` table is automatically created when you run the above command.
However, it is not recommended to use the built-in `User` model if you plan to establish relationships with other tables or need to add custom fields.

The default `User` table contains the following fields:

* id
* username
* hashed\_password
* is\_active
* is\_superuser
* date\_joined

If you need a customized User model, you can override it as shown below:

```python {6,17-19,45-46} theme={null}

import contextlib
from sqlalchemy import Column, Integer, String, Text, create_engine, ForeignKey
from sqlalchemy.orm import declarative_base,relationship,sessionmaker
from starlette.applications import Starlette
from fastdaisy_admin.auth.models import BaseUser
from fastdaisy_admin.application import ModelView,Admin

secret_key="secret_key"
Base = declarative_base()
engine = create_engine(
    "sqlite:///example.db",
    connect_args={"check_same_thread": False},
)
Session = sessionmaker(bind=engine)

class User(Base, BaseUser):
    __tablename__ = "users"
    books = relationship("Book", back_populates="author", cascade="all, delete-orphan")


class Book(Base):
    __tablename__ = 'books'

    id = Column(Integer, primary_key=True)
    title = Column(String)
    description = Column(Text)
    author_id = Column(Integer, ForeignKey('users.id'))
    author = relationship("User", back_populates="books")


@contextlib.asynccontextmanager
async def lifespan(app):
    Base.metadata.create_all(engine)
    yield


app = Starlette(lifespan=lifespan)


admin = Admin(
    app, 
    secret_key, 
    engine,
    authentication=True,
    auth_model=User
)


```

In the `Admin` class, ensure that:

* `authentication=True` is set to enable authentication.
* `auth_model=User` is passed if you are using a custom `User` model.

### Creating superuser

To create a superuser, run:

```bash theme={null}
fastdaisy-admin createsuperuser

```

When executed:

1. The User table will be created automatically (if it does not already exist).
2. You will be prompted to enter a username and password.
3. A superuser account will then be created.

To access the admin interface, log in using valid credentials on the login page.
Authentication is managed internally using session cookies, which remain valid for two weeks.

You will see an Edit Profile button in the navigation bar only when you register a user admin as shown below:

```python theme={null}

from fastdaisy_admin import ModelView

class UserAdmin(ModelView):
    model = User

admin.add_view(UserAdmin)

```

To `logout`, click the Logout button located in the navigation bar. This will clear the session data from your cookies.
