-
Notifications
You must be signed in to change notification settings - Fork 116
feat: better auth #3685
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
Merged
Merged
feat: better auth #3685
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4d5cd0a
feat: better auth
rebelchris 500fb88
Merge branch 'main' of github.com:dailydotdev/daily-api into feat-bet…
rebelchris 32178cf
fix: auth secrets
rebelchris 4e35c46
fix: version 1.5
rebelchris 692e3e5
fix: turnstile
rebelchris 6137ec4
fix: security and tests
rebelchris 5780cc8
fix: some more fixes for linking
rebelchris e222b9f
Merge branch 'main' of github.com:dailydotdev/daily-api into feat-bet…
rebelchris 54336ff
fix: add mailing
rebelchris e7eb36f
Merge branch 'main' into feat-better-auth
rebelchris 0a46d49
fix: address security scanner findings in betterAuth
github-actions[bot] d4fff85
Potential fix for code scanning alert no. 51: Missing rate limiting
rebelchris 65009cf
fix: add rate limiting to social redirect handler
github-actions[bot] 4b88500
fix: feedback PR
rebelchris 2816519
fix: more cleanup
rebelchris c7bf89f
Merge branch 'main' into feat-better-auth
rebelchris bb47aac
fix: tests
rebelchris abdf09f
fix: feedback
rebelchris cdb7ba5
fix: feedback
rebelchris df9194e
fix: move to BA config
rebelchris 7528cd8
Merge branch 'main' into feat-better-auth
rebelchris a7e6108
fix: refactor naming
rebelchris 4a7482a
Merge remote-tracking branch 'origin/feat-better-auth' into feat-bett…
rebelchris ee131b5
fix: default
rebelchris c04e6e4
fix: lint issues
rebelchris 0ead503
fix: auth rename and minor feedback
rebelchris c8eef45
Potential fix for code scanning alert no. 53: Missing rate limiting
rebelchris 46b355d
fix: pr feedback
rebelchris d262dde
Merge remote-tracking branch 'origin/feat-better-auth' into feat-bett…
rebelchris 7fefba3
fix: pr feedback
rebelchris 0bdb04d
fix: add commentary
rebelchris c024a70
fix: include allocation client
rebelchris f5217c1
Merge branch 'main' into feat-better-auth
rebelchris 5b73f5a
Merge branch 'main' into feat-better-auth
rebelchris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export const createAuthMiddleware = ( | ||
|
rebelchris marked this conversation as resolved.
|
||
| fn: (ctx: Record<string, unknown>) => unknown, | ||
| ) => fn; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export const fromNodeHeaders = ( | ||
| headers: Record<string, string | string[] | undefined>, | ||
| ): Headers => { | ||
| const h = new Headers(); | ||
| for (const [key, value] of Object.entries(headers)) { | ||
| if (!value) continue; | ||
| if (Array.isArray(value)) { | ||
| value.forEach((v) => h.append(key, v)); | ||
| } else { | ||
| h.set(key, value); | ||
| } | ||
| } | ||
| return h; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const emailOTP = () => ({ | ||
| id: 'email-otp', | ||
| endpoints: {}, | ||
| hooks: { after: [] }, | ||
| rateLimit: [], | ||
| options: {}, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export const captcha = () => ({ | ||
| id: 'captcha', | ||
| options: {}, | ||
| }); | ||
|
|
||
| export const emailOTP = () => ({ | ||
| id: 'email-otp', | ||
| endpoints: {}, | ||
| hooks: { after: [] }, | ||
| rateLimit: [], | ||
| options: {}, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const betterAuth = () => ({ | ||
| handler: async () => new Response('{}', { status: 200 }), | ||
| api: { | ||
| getSession: async () => null, | ||
| }, | ||
| }); | ||
|
|
||
| export type BetterAuthOptions = Record<string, unknown>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { cleanExpiredBetterAuthSessions as cron } from '../../src/cron/cleanExpiredBetterAuthSessions'; | ||
| import { expectSuccessfulCron, saveFixtures } from '../helpers'; | ||
| import { DataSource } from 'typeorm'; | ||
| import createOrGetConnection from '../../src/db'; | ||
| import { User } from '../../src/entity/user/User'; | ||
| import { usersFixture } from '../fixture'; | ||
| import { crons } from '../../src/cron/index'; | ||
| import { sub, add } from 'date-fns'; | ||
|
|
||
| let con: DataSource; | ||
|
|
||
| beforeAll(async () => { | ||
| con = await createOrGetConnection(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| await saveFixtures( | ||
| con, | ||
| User, | ||
| usersFixture.map((user) => ({ | ||
| ...user, | ||
| id: `${user.id}-bac`, | ||
| username: `${user.username}-bac`, | ||
| })), | ||
| ); | ||
|
|
||
| const now = new Date(); | ||
| await con.query( | ||
| `INSERT INTO ba_session (id, token, "userId", "expiresAt", "createdAt", "updatedAt") | ||
| VALUES ($1, $2, $3, $4, $5, $5), | ||
| ($6, $7, $8, $9, $10, $10), | ||
| ($11, $12, $13, $14, $15, $15) | ||
| ON CONFLICT (id) DO NOTHING`, | ||
| [ | ||
| 'expired-1', | ||
| 'token-expired-1', | ||
| '1-bac', | ||
| sub(now, { days: 2 }), | ||
| sub(now, { days: 9 }), | ||
| 'expired-2', | ||
| 'token-expired-2', | ||
| '2-bac', | ||
| sub(now, { hours: 1 }), | ||
| sub(now, { days: 3 }), | ||
| 'active-1', | ||
| 'token-active-1', | ||
| '1-bac', | ||
| add(now, { days: 5 }), | ||
| now, | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| describe('cleanExpiredBetterAuthSessions cron', () => { | ||
| it('should be registered', () => { | ||
| const registeredCron = crons.find((item) => item.name === cron.name); | ||
| expect(registeredCron).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should delete expired sessions and keep active ones', async () => { | ||
| const before: { id: string }[] = await con.query( | ||
| `SELECT id FROM ba_session ORDER BY id`, | ||
| ); | ||
| expect(before).toHaveLength(3); | ||
|
|
||
| await expectSuccessfulCron(cron); | ||
|
|
||
| const after: { id: string }[] = await con.query( | ||
| `SELECT id FROM ba_session ORDER BY id`, | ||
| ); | ||
| expect(after).toEqual([{ id: 'active-1' }]); | ||
| }); | ||
|
|
||
| it('should handle no expired sessions gracefully', async () => { | ||
| await con.query( | ||
| `DELETE FROM ba_session WHERE id IN ('expired-1', 'expired-2')`, | ||
| ); | ||
|
|
||
| await expectSuccessfulCron(cron); | ||
|
|
||
| const after: { id: string }[] = await con.query( | ||
| `SELECT id FROM ba_session WHERE id = 'active-1'`, | ||
| ); | ||
| expect(after).toHaveLength(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import request from 'supertest'; | ||
| import { FastifyInstance } from 'fastify'; | ||
| import { DataSource } from 'typeorm'; | ||
| import createOrGetConnection from '../../src/db'; | ||
| import { saveFixtures } from '../helpers'; | ||
| import { User } from '../../src/entity/user/User'; | ||
| import { usersFixture } from '../fixture'; | ||
| import { ioRedisPool } from '../../src/redis'; | ||
|
|
||
| let app: FastifyInstance; | ||
| let con: DataSource; | ||
|
|
||
| beforeAll(async () => { | ||
| con = await createOrGetConnection(); | ||
|
|
||
| process.env.BETTER_AUTH_SECRET = 'a]3Bv!k9Pz@mQ7wX#rL2sY&dN5fH8jT-test-only'; | ||
| try { | ||
| const { initializeBetterAuth } = await import('../../src/betterAuth'); | ||
| initializeBetterAuth(); | ||
| } catch { | ||
| // BA initialization may fail in test with mocked module | ||
| } | ||
|
|
||
| const appFunc = (await import('../../src')).default; | ||
| app = await appFunc(); | ||
| return app.ready(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| if (app) { | ||
| await app.close(); | ||
| } | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| await ioRedisPool.execute((client) => client.flushall()); | ||
| await saveFixtures(con, User, usersFixture); | ||
| }); | ||
|
|
||
| describe('betterAuth routes', () => { | ||
| describe('native Better Auth routes', () => { | ||
| it('should return 200 for the health endpoint', async () => { | ||
| const res = await request(app.server).get('/auth/ok'); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.