Authentication providers
WeGotWork supports the following authentication methods:Email and password
Users can sign up and sign in using email and password credentials.Google OAuth
Users can authenticate using their Google account:Google OAuth requires
GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables.API endpoints
Better Auth automatically creates authentication endpoints at/api/auth/*. These endpoints handle all authentication flows.
Available endpoints
Sign in with email and password
Create a new account with email and password
Initiate Google OAuth flow
Sign out the current user
Get the current session information
Session management
WeGotWork uses cookie-based sessions with the following configuration:Session configuration
- Session duration: 7 days
- Session update interval: 24 hours (session expiration is extended every 24 hours)
- Cookie cache: Enabled for 1 hour
Custom session data
Sessions are extended with custom user data:The custom session plugin automatically fetches the user’s current organization and all organizations they belong to.
Getting authentication tokens
Client-side authentication
Use theuseSession hook to access session data in React components:
Server-side authentication
In server actions and API routes, use theauth.api.getSession method:
Server actions should always validate the session before performing any operations.
Authentication flows
Sign up flow
- User submits email, password, and name
- Better Auth creates a new user account
- User is automatically signed in
- Session cookie is set
- User is redirected to dashboard
Sign in flow
- User submits email and password
- Better Auth validates credentials
- Session is created and cookie is set
- User is redirected to dashboard
OAuth flow
- User clicks “Sign in with Google”
- User is redirected to Google OAuth consent screen
- User approves access
- Google redirects back to
/api/auth/callback/google - Better Auth creates or updates user account
- Session is created and cookie is set
- User is redirected to the specified callback URL
Protected routes
All server actions are protected by default. Unauthenticated requests are redirected to the home page:Trusted origins
Better Auth is configured to accept requests from the following origins:http://localhost:3000(development)https://wegotwork.co(production)https://www.wegotwork.co(production)- Any URL specified in the
BASE_URLenvironment variable
Database adapter
WeGotWork uses the Prisma adapter for Better Auth, which stores authentication data in PostgreSQL:Client configuration
The authentication client is configured with the base URL:Set the
NEXT_PUBLIC_BASE_URL environment variable to your production URL.Best practices
Always validate sessions
Never trust client-side session data. Always validate sessions on the server:Use TypeScript types
Leverage the exported session type for type safety:Handle redirects gracefully
Use Next.jsredirect instead of throwing errors:
Troubleshooting
Session not persisting
Ensure cookies are enabled in the browser and theBASE_URL matches the current domain.
CORS errors
Add your domain to thetrustedOrigins array in the Better Auth configuration.
OAuth callback fails
Verify that the OAuth provider’s callback URL is set tohttps://yourdomain.com/api/auth/callback/google.
Next steps
API overview
Learn about the API architecture
Server actions
Explore available server actions