Quick Start
Prerequisites
- A Civic Auth Client ID (get it from auth.civic.com)
- Configure your redirect URL in the Civic Auth dashboard (typically
http://localhost:3000
for development)
Installation
NPMWe highly recommend using Vite for the best development experience with modern JavaScript features, fast hot reloading, and seamless ES module support.
Simple Setup
- HTML:
- JavaScript (
main.js
):
YOUR_CLIENT_ID
with your actual client ID and you’re done.
Alternative: Backend Integration
If you prefer backend session management, you can configure the client to get login URLs from your Express backend. The magic is theloginUrl
option:
Custom Backend Endpoints
When using backend integration, you can customize the API endpoints that the client calls on your backend:The
backendEndpoints
configuration is only used when loginUrl
is provided. Each endpoint is optional - if not
specified, the default will be used.Configuration Options
Field | Required | Default | Description |
---|---|---|---|
clientId | Yes | - | Your Civic Auth client ID from auth.civic.com |
targetContainerElement | No | - | DOM element where embedded iframe will be rendered |
redirectUrl | No | Current URL | OAuth redirect URL after authentication |
displayMode | No | modal | How the auth UI is displayed: embedded , modal , redirect , or new_tab |
scopes | No | ['openid', 'profile', 'email'] | OAuth scopes to request |
loginUrl | No | - | Backend URL for login redirect (enables backend integration) |
backendEndpoints | No | See below | Custom backend API endpoints (only used with loginUrl ) |
Backend Endpoints Default Values
When usingloginUrl
for backend integration, the following default endpoints are used:
Display Modes
ThedisplayMode
option controls how the authentication UI is presented:
embedded
(default): The auth UI loads in an iframe within your specified container elementmodal
: The auth UI opens in a modal overlay on top of your current pageredirect
: Full page navigation to the Civic auth server and back to your sitenew_tab
: Opens auth flow in a new browser tab/popup window
Logout
Logging out is very simple.- Use
authClient.getCurrentUser()
to retrieve current user information before logout - Use
authClient.isAuthenticated()
to check if user is currently logged in
Troubleshooting
Module Resolution Error
If you encounter an error likeFailed to resolve module specifier "@civic/auth/vanillajs"
, this is typically caused by a corrupted module cache or installation issue.
Solution:
@civic/auth
package or when the package installation is interrupted.
Common Issues
- CORS errors: Ensure your redirect URL in the Civic Auth dashboard exactly matches your development server URL
- Authentication not starting: Verify your client ID is correct and your redirect URL is properly configured
- Container element not found: Make sure the target container element exists in the DOM before initializing Civic Auth