Skip to content

feat: add Better Auth authentication system #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Conversation

zlwaterfield
Copy link
Collaborator

@zlwaterfield zlwaterfield commented Jun 19, 2025

  • Install Better Auth with Prisma adapter for PostgreSQL
  • Add User, Session, Account, Verification models to database schema
  • Create auth API controller with catch-all Better Auth handler
  • Add authentication guard and middleware for protected routes
  • Create login/signup UI with email/password forms
  • Add auth provider for route protection and session management
  • Configure Next.js proxy to route /api requests to backend
  • Set up CORS configuration for cross-origin auth requests

zlwaterfield and others added 4 commits June 18, 2025 22:02
- Install Better Auth with Prisma adapter for PostgreSQL
- Add User, Session, Account, Verification models to database schema
- Create auth API controller with catch-all Better Auth handler
- Add authentication guard and middleware for protected routes
- Create login/signup UI with email/password forms
- Add auth provider for route protection and session management
- Configure Next.js proxy to route /api requests to backend
- Set up CORS configuration for cross-origin auth requests

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add Better Auth dependency to backend package.json
- Create auth module with controller, guard, and service
- Add Prisma migration for Better Auth schema
- Update app module to include auth module
- Add auth guard to tasks controller for route protection
- Configure global /api prefix and enhanced CORS settings

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Fix auth controller to properly convert Express requests to Web API requests
- Add cookie name synchronization between frontend and backend
- Configure Better Auth with correct baseURL including /api prefix
- Add fetchOptions with credentials: 'include' for cross-origin cookie handling
- Update CORS configuration to expose Set-Cookie headers
- Use NestJS route pattern '*path' instead of deprecated '*'

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Configure auth client to call backend directly at localhost:9991
- Add credentials: 'include' for cross-origin cookie handling
- Minor login page refinements

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@zlwaterfield zlwaterfield self-assigned this Jun 19, 2025
@zlwaterfield zlwaterfield marked this pull request as draft June 19, 2025 02:25
@Controller('auth')
export class AuthController {
@All('*path')
async handleAuth(@Req() request: Request, @Res() response: Response) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this but struggled to get the auth handler to work.

cookieName: 'better-auth.session', // Match frontend cookie name
},
secret: process.env.BETTER_AUTH_SECRET || 'fallback-secret-change-in-production',
baseURL: process.env.BETTER_AUTH_URL || 'http://localhost:9991/api/auth',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backend base url

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both secret and baseURL should be read from your env, you don't have to provide them unless you need the fallback. Also you don't need to pass /api/auth the origin is enough since base path defaults to /api/auth

},
secret: process.env.BETTER_AUTH_SECRET || 'fallback-secret-change-in-production',
baseURL: process.env.BETTER_AUTH_URL || 'http://localhost:9991/api/auth',
trustedOrigins: [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need both of these?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the frontend url

@@ -13,11 +13,17 @@ async function bootstrap() {
try {
const app = await NestFactory.create(AppModule);

// Set global prefix for all routes
app.setGlobalPrefix('api');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how the the current /api routes are working I sorta needed this but feels wrong

async rewrites() {
return [
{
source: '/api/:path*',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this proxy, do we need it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed

import { createAuthClient } from 'better-auth/react';

export const authClient = createAuthClient({
baseURL: 'http://localhost:9991/api/auth',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend base url which makes me think we don't need the next proxy

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we don't. and also you don't need to pass /api/auth only the base url without the path is enough

session: {
expiresIn: 60 * 60 * 24 * 7, // 7 days
updateAge: 60 * 60 * 24, // 24 hours (update session if it's older than this)
cookieName: 'better-auth.session', // Match frontend cookie name
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this config isn't needed. This is the default, you probably should remove it.

'http://localhost:9991', // Backend URL
],
advanced: {
crossSubDomainCookies: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default behavior, you don't need to pass this config.


export const authClient = createAuthClient({
baseURL: 'http://localhost:9991/api/auth',
session: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invalid config, you can remove it

cookieName: 'better-auth.session',
},
fetchOptions: {
credentials: 'include', // Include cookies in cross-origin requests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default behavior

@atupem
Copy link
Contributor

atupem commented Jun 19, 2025

Is it possible to have the auth optionally configured with a environment variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants