Follow these simple steps to set up Civic Auth with a Hono backend (a working example is available in our github examples repo).
1. Install dependencies
npminstall@civic/auth
yarnadd@civic/auth
pnpminstall@civic/auth
bunadd@civic/auth
2. Configure your App
Your app will need the following configuration:
constconfig= { clientId:// Client ID from auth.civic.com redirectUrl: 'http://localhost:3000/auth/callback',// change to your domain when deploying postLogoutRedirectUrl:'http://localhost:3000/'// The postLogoutRedirectUrl is the URL where the user will be redirected after successfully logging out from Civic's auth server.};
Note: redirectUrl and postLogoutRedirectUrl must be absolute URLs.
3. Set up Cookies
Civic Auth uses cookies for storing the login state by default
import { isLoggedIn } from'@civic/auth/server';// Apply authentication middleware to any routes that need itapp.use('/admin/*',async (c, next) => {if (!isLoggedIn(c.get('storage'))) returnc.text('Unauthorized',401);returnnext();});
8. Use the Session
If needed, get the logged-in user information.
import { user } from'@civic/auth/server';app.get('/admin/hello',async (c) => {constuser=awaitgetUser(c.get('storage'));returnc.text(`Hello, ${user?.name}!`);});
PKCE and Client Secrets
Civic Auth uses PKCE (Proof Key for Code Exchange), to protect users and clients from unauthorized access to user information. This, alongside domain registration for apps in production environments, mean that you don't need to provide a client secret in your backend.
When using the Civic Auth SDK, PKCE is handled entirely by the library.