title | description | weight |
---|---|---|
🛂 Authentication System |
This guide explains how to use the user authentication system of LibreChat, which offers secure and easy email and social logins. You will learn how to set up sign up, log in, password reset, and more. |
-5 |
LibreChat has a user authentication system that allows users to sign up and log in securely and easily. The system is scalable and can handle a large number of concurrent users without compromising performance or security.
By default, we have email signup and login enabled, which means users can create an account using their email address and a password. They can also reset their password if they forget it.
Additionally, our system can integrate social logins from various platforms such as Google, GitHub, Discord, OpenID, and more. This means users can log in using their existing accounts on these platforms, without having to create a new account or remember another password.
❗Important: When you run the app for the first time, you need to create a new account by clicking on "Sign up" on the login page. The first account you make will be the admin account. The admin account doesn't have any special features right now, but it might be useful if you want to make an admin dashboard to manage other users later.
Note: The first account created should ideally be a local account (email and password).
Here's an overview of the general configuration, located in the .env
file at the root of the LibreChat folder.
ALLOW_EMAIL_LOGIN
: Email login. Set totrue
orfalse
to enable or disable ONLY email login.ALLOW_REGISTRATION
: Email registration of new users. Set totrue
orfalse
to enable or disable Email registration.ALLOW_SOCIAL_LOGIN
: Allow users to connect to LibreChat with various social networks, see below. Set totrue
orfalse
to enable or disable.ALLOW_SOCIAL_REGISTRATION
: Enable or disable registration of new user using various social network. Set totrue
orfalse
to enable or disable.
Note: OpenID does not support the ability to disable only registration.
Quick Tip: Even with registration disabled, add users directly to the database using
npm run create-user
. If you can't get npm to work, trysudo docker exec -ti LibreChat sh
first to "ssh" into the container. Quick Tip: To delete a user, you can rundocker-compose exec api npm run delete-user [email protected]
ALLOW_EMAIL_LOGIN=true
ALLOW_REGISTRATION=true
ALLOW_SOCIAL_LOGIN=false
ALLOW_SOCIAL_REGISTRATION=false
- Default values: session expiry: 15 minutes, refresh token expiry: 7 days
- For more information: GitHub PR #927 - Refresh Token
SESSION_EXPIRY=1000 * 60 * 15
REFRESH_TOKEN_EXPIRY=(1000 * 60 * 60 * 24) * 7
sequenceDiagram
Client->>Server: Login request with credentials
Server->>Passport: Use authentication strategy (e.g., 'local', 'google', etc.)
Passport-->>Server: User object or false/error
Note over Server: If valid user...
Server->>Server: Generate access and refresh tokens
Server->>Database: Store hashed refresh token
Server-->>Client: Access token and refresh token
Client->>Client: Store access token in HTTP Header and refresh token in HttpOnly cookie
Client->>Server: Request with access token from HTTP Header
Server-->>Client: Requested data
Note over Client,Server: Access token expires
Client->>Server: Request with expired access token
Server-->>Client: Unauthorized
Client->>Server: Request with refresh token from HttpOnly cookie
Server->>Database: Retrieve hashed refresh token
Server->>Server: Compare hash of provided refresh token with stored hash
Note over Server: If hashes match...
Server-->>Client: New access token and refresh token
Client->>Server: Retry request with new access token
Server-->>Client: Requested data
- You should use new secure values. The examples given are 32-byte keys (64 characters in hex).
- Use this replit to generate some quickly: JWT Keys
JWT_SECRET=16f8c0ef4a5d391b26034086c628469d3f9f497f08163ab9b40137092f2909ef
JWT_REFRESH_SECRET=eaa5191f2914e30b9387fd84e254e4ba6fc51b4654968a9b0803b456a54b8418
The Automated Moderation System is enabled by default. It uses a scoring mechanism to track user violations. As users commit actions like excessive logins, registrations, or messaging, they accumulate violation scores. Upon reaching a set threshold, the user and their IP are temporarily banned. This system ensures platform security by monitoring and penalizing rapid or suspicious activities.
To set up the mod system, review the setup guide.
Please Note: If you want this to work in development mode, you will need to create a file called
.env.development
in the root directory and setDOMAIN_CLIENT
tohttp://localhost:3090
or whatever port is provided by vite when runnningnpm run frontend-dev
in the .env file modify these variables:
EMAIL_SERVICE= # eg. gmail - see https://community.nodemailer.com/2-0-0-beta/setup-smtp/well-known-services/
EMAIL_HOST= # eg. example.com - if EMAIL_SERVICE is not set, connect to this server.
EMAIL_PORT=25 # eg. 25 - mail server port to connect to with EMAIL_HOST (usually 25, 465, 587)
EMAIL_ENCRYPTION= # eg. starttls - valid values: starttls (force STARTTLS), tls (obligatory TLS), anything else (use STARTTLS if available)
EMAIL_ENCRYPTION_HOSTNAME= # eg. example.com - check the name in the certificate against this instead of EMAIL_HOST
EMAIL_ALLOW_SELFSIGNED= # eg. true - valid values: true (allow self-signed), anything else (disallow self-signed)
EMAIL_USERNAME= # eg. [email protected] - the username used for authentication. For consumer services, this MUST usually match EMAIL_FROM.
EMAIL_PASSWORD= # eg. password - the password used for authentication
EMAIL_FROM_NAME= # eg. LibreChat - the human-readable address in the From is constructed as "EMAIL_FROM_NAME <EMAIL_FROM>". Defaults to APP_TITLE.
If you want to use one of the predefined services, configure only these variables:
EMAIL_SERVICE is the name of the email service you are using (Gmail, Outlook, Yahoo Mail, ProtonMail, iCloud Mail, etc.) as defined in the NodeMailer well-known services linked above. EMAIL_USERNAME is the username of the email service (usually, it will be the email address, but in some cases, it can be an actual username used to access the account). EMAIL_PASSWORD is the password used to access the email service. This is not the password to access the email account directly, but a password specifically generated for this service. EMAIL_FROM is the email address that will appear in the "from" field when a user receives an email. EMAIL_FROM_NAME is the name that will appear in the "from" field when a user receives an email. If left unset, it defaults to the app title.
If you want to use a generic SMTP service or need advanced configuration for one of the predefined providers, configure these variables:
EMAIL_HOST is the hostname to connect to, or an IP address.
EMAIL_PORT is the port to connect to. Be aware that different ports usually come with different requirements - 25 is for mailserver-to-mailserver, 465 requires encryption at the start of the connection, and 587 allows submission of mail as a user.
EMAIL_ENCRYPTION defines if encryption is required at the start (tls
) or started after the connection is set up (starttls
). If either of these values are set, they are enforced. If they are not set, an encrypted connection is started if available.
EMAIL_ENCRYPTION_HOSTNAME allows specification of a hostname against which the certificate is validated. Use this if the mail server does have a valid certificate, but you are connecting with an IP or a different name for some reason.
EMAIL_ALLOW_SELFSIGNED defines whether self-signed certificates can be accepted from the server. As the mails being sent contain sensitive information, ONLY use this for testing.
NOTE:
- Create a Google Account and enable 2-step verification.
- In the Google Account settings, click on the "Security" tab and open "2-step verification."
- Scroll down and open "App passwords." Choose "Mail" for the app and select "Other" for the device, then give it a random name.
- Click on "Generate" to create a password, and copy the generated password.
- In the .env file, modify the variables as follows:
EMAIL_SERVICE=gmail
EMAIL_USERNAME=your-email
EMAIL_PASSWORD=your-app-password
EMAIL_FROM=email address for the from field, e.g., [email protected]
EMAIL_FROM_NAME="My LibreChat Server"
- Gather your SMTP login data from your provider. The steps are different for each, but they will usually list values for all variables.
- In the .env file, modify the variables as follows, assuming some sensible example values:
EMAIL_HOST=mail.example.com
EMAIL_PORT=587
EMAIL_ENCRYPTION=starttls
EMAIL_USERNAME=your-email
EMAIL_PASSWORD=your-app-password
EMAIL_FROM=email address for the from field, e.g., [email protected]
EMAIL_FROM_NAME="My LibreChat Server"
-
Go to Discord Developer Portal
-
Create a new Application and give it a name
- In the OAuth2 general settings add a valid redirect URL:
- Example for localhost:
http://localhost:3080/oauth/discord/callback
- Example for a domain:
https://example.com/oauth/discord/callback
- Example for localhost:
- In
Default Authorization Link
, selectIn-app Authorization
and set the scopes toapplications.commands
- Save changes and reset the Client Secret
- Paste your
Client ID
andClient Secret
in the.env
file:
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DISCORD_CLIENT_ID=your_client_id
DISCORD_CLIENT_SECRET=your_client_secret
DISCORD_CALLBACK_URL=/oauth/discord/callback
- Save the
.env
file
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes
⚠️ Warning: Work in progress, not currently functional
❗ Note: Facebook Authentication will not work from
localhost
-
Go to the Facebook Developer Portal
-
Click on "My Apps" in the header menu
- Create a new application
- Select "Authenticate and request data from users with Facebook Login"
- Choose "No, I'm not creating a game"
- Provide an
app name
andApp contact email
and clickCreate app
- In the side menu, select "Use cases" and click "Customize" under "Authentication and account creation."
- Add the
email permission
- Now click
Go to settings
- Ensure that
Client OAuth login
,Web OAuth login
andEnforce HTTPS
are enabled.
- Add a
Valid OAuth Redirect URIs
and "Save changes"- Example for a domain:
https://example.com/oauth/facebook/callback
- Example for a domain:
- Click
Go back
and selectBasic
in theApp settings
tab
- Click "Show" next to the App secret.
- Copy the
App ID
andApp Secret
and paste them into the.env
file as follows:
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
FACEBOOK_CLIENT_ID=your_app_id
FACEBOOK_CLIENT_SECRET=your_app_secret
FACEBOOK_CALLBACK_URL=/oauth/facebook/callback
- Save the
.env
file.
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes
- Go to your Github Developer settings
- Create a new Github app
- Give it a
GitHub App name
and set yourHomepage URL
- Example for localhost:
http://localhost:3080
- Example for a domain:
https://example.com
- Example for localhost:
- Add a valid
Callback URL
:- Example for localhost:
http://localhost:3080/oauth/github/callback
- Example for a domain:
https://example.com/oauth/github/callback
- Example for localhost:
- Uncheck the box labeled
Active
in theWebhook
section
- Scroll down to
Account permissions
and setEmail addresses
toAccess: Read-only
- Click on
Create GitHub App
- Click
Generate a new client secret
- Copy the
Client ID
andClient Secret
in the.env
file
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
GITHUB_CLIENT_ID=your_client_id
GITHUB_CLIENT_SECRET=your_client_secret
GITHUB_CALLBACK_URL=/oauth/github/callback
- Save the
.env
file
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes
- Visit: Google Cloud Console and open the
Console
- Create a New Project and give it a name
- Select the project you just created and go to
APIs and Services
- Select
Credentials
and clickCONFIGURE CONSENT SCREEN
- Select
External
then clickCREATE
- Fill in your App information
Note: You can get a logo from your LibreChat folder here:
docs\assets\favicon_package\android-chrome-192x192.png
- Configure your
App domain
and add yourDeveloper contact information
then clickSAVE AND CONTINUE
- Configure the
Sopes
- Add
email
,profile
andopenid
- Click
UPDATE
andSAVE AND CONTINUE
- Add
-
Click
SAVE AND CONTINUE
-
Review your app and go back to dashboard
-
Go back to the
Credentials
tab, click on+ CREATE CREDENTIALS
and selectOAuth client ID
- Select
Web application
and give it a name
- Configure the
Authorized JavaScript origins
, you can add both your domain and localhost if you desire- Example for localhost:
http://localhost:3080
- Example for a domain:
https://example.com
- Example for localhost:
- Add a valid
Authorized redirect URIs
- Example for localhost:
http://localhost:3080/oauth/google/callback
- Example for a domain:
https://example.com/oauth/google/callback
- Example for localhost:
- Click
CREATE
and copy yourClient ID
andClient secret
- Add them to your
.env
file:
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_CALLBACK_URL=/oauth/github/callback
- Save the
.env
file
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes
- Visit: https://console.aws.amazon.com/cognito/
- Sign in as Root User
- Click on
Create user pool
Your Cognito user pool sign-in options should include User Name
and Email
.
You can configure the password requirements now if you desire
Choose the attributes required at signup. The minimum required is name
. If you want to require users to use their full name at sign up use: given_name
and family_name
as required attributes.
Send email with Cognito can be used for free for up to 50 emails a day
Select Use Cognitio Hosted UI
and chose a domain name
Set the app type to Confidential client
Make sure Generate a client secret
is set.
Set the Allowed callback URLs
to https://YOUR_DOMAIN/oauth/openid/callback
Under Advanced app client settings
make sure Profile
is included in the OpenID Connect scopes
(in the bottom)
You can now make last minute changes, click on Create user pool
when you're done reviewing the configuration
- Open your User Pool
- The
User Pool ID
and your AWS region will be used to construct theOPENID_ISSUER
(see below)
- Go to the
App Integrations
tab
- Open the app client
- Toggle
Show Client Secret
-
Use the
Client ID
forOPENID_CLIENT_ID
-
Use the
Client secret
forOPENID_CLIENT_SECRET
-
Generate a random string for the
OPENID_SESSION_SECRET
The
OPENID_SCOPE
andOPENID_CALLBACK_URL
are pre-configured with the correct values
- Open the
.env
file at the root of your LibreChat folder and add the following variables with the values you copied:
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
OPENID_CLIENT_ID=Your client ID
OPENID_CLIENT_SECRET=Your client secret
OPENID_ISSUER=https://cognito-idp.[AWS REGION].amazonaws.com/[USER POOL ID]/.well-known/openid-configuration
OPENID_SESSION_SECRET=Any random string
OPENID_SCOPE=openid profile email
OPENID_CALLBACK_URL=/oauth/openid/callback
- Save the .env file
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes
- Go to the Azure Portal and sign in with your account.
- In the search box, type "Azure Active Directory" and click on it.
- On the left menu, click on App registrations and then on New registration.
- Give your app a name and select Web as the platform type.
- In the Redirect URI field, enter
http://localhost:3080/oauth/openid/callback
and click on Register. - You will see an Overview page with some information about your app. Copy the Application (client) ID and the Directory (tenant) ID and save them somewhere.
- On the left menu, click on Authentication and check the boxes for Access tokens and ID tokens under Implicit grant and hybrid flows.
- On the left menu, click on Certificates & Secrets and then on New client secret. Give your secret a name and an expiration date and click on Add.
- You will see a Value column with your secret. Copy it and save it somewhere. Don't share it with anyone!
- Open the .env file in your project folder and add the following variables with the values you copied:
DOMAIN_CLIENT=https://your-domain.com #use http://localhost:3080 if not using a custom domain
DOMAIN_SERVER=https://your-domain.com #use http://localhost:3080 if not using a custom domain
OPENID_CLIENT_ID=Your Application (client) ID
OPENID_CLIENT_SECRET=Your client secret
OPENID_ISSUER=https://login.microsoftonline.com/Your Directory (tenant ID)/v2.0/
OPENID_SESSION_SECRET=Any random string
OPENID_SCOPE=openid profile email #DO NOT CHANGE THIS
OPENID_CALLBACK_URL=/oauth/openid/callback # this should be the same for everyone
- Save the .env file
Note: If using docker, run
docker compose up -d
to apply the .env configuration changes