Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/auth-svc/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
permissionBindsApp,
apiKeysRoutes,
} from './routes/index.js'
import defaultSeedFunc from './seed.js'

Check warning on line 22 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

There should be at least one empty line between import groups
import { Buffer } from 'buffer'

Check warning on line 23 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

`buffer` import should occur before import of `@hono/node-server`
import { generateKeyPairSync, randomUUID } from 'node:crypto'

Check warning on line 24 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

`node:crypto` import should occur before import of `@hono/node-server`

Check warning on line 24 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

There should be at least one empty line between import groups
import { eq } from 'drizzle-orm'

Check warning on line 25 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

`drizzle-orm` import should occur before import of `hono/cors`

Check warning on line 25 in apps/auth-svc/src/main.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

There should be at least one empty line between import groups
import { TableAuthKeys } from './schema.js'
import type { DB } from './db.js'
import type { AuthServerConfig } from './types.js'
import type { AuthServerConfig, EmailSender, SMSSender } from './types.js'

declare module 'hono' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
Expand Down Expand Up @@ -130,7 +130,9 @@
}

export type AuthSvcOptions = {
seedFunc: (authServer: AuthServer) => void
seedFunc?: (authServer: AuthServer) => void
emailSender?: EmailSender
smsSender?: SMSSender
}

export async function main(options?: AuthSvcOptions) {
Expand All @@ -151,7 +153,7 @@
console.log('uncaughtException', err.toString())
})

const authServer = new AuthServer(db, config)
const authServer = new AuthServer(db, config, options?.smsSender, options?.emailSender)
startServer(authServer)

console.log('Started')
Expand Down
9 changes: 1 addition & 8 deletions apps/auth-svc/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TableBasicAuthAccounts,
} from './schema.js'
import type { AuthServerConfig } from './types.js'
import type { EmailSender, SMSSender } from './types.js'

export const LoginSchema = z.object({
username: z.string().optional(),
Expand All @@ -52,14 +53,6 @@ export type UserWithTokens = {
refreshToken: string
}

type SMSSender = {
sendSms(phone: string, text: string): TResult<boolean>
}

type EmailSender = {
sendEmail(email: string, text: string): TResult<boolean>
}

type NewUserWithPassword = {
tenant: string
password: string
Expand Down
12 changes: 10 additions & 2 deletions apps/auth-svc/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ export const authRoutes = app.openapi(
const code = codeResult.value

if (authServer.emailSender) {
const sendResult = authServer.emailSender.sendEmail(body.email, `Your verification code: ${code}`)
const sendResult = await authServer.emailSender.sendEmail(
body.email,
`Your verification code: ${code}`,
{ code },
)
if (sendResult.err !== null) {
console.warn('[auth] failed to send verification email', sendResult.err)
}
Expand Down Expand Up @@ -323,7 +327,11 @@ export const authRoutes = app.openapi(
const code = codeResult.value

if (authServer.emailSender) {
const sendResult = authServer.emailSender.sendEmail(body.email, `Your password change code: ${code}`)
const sendResult = await authServer.emailSender.sendEmail(
body.email,
`Your password change code: ${code}`,
{ code },
)
if (sendResult.err !== null) {
console.warn('[auth] failed to send password change email', sendResult.err)
}
Expand Down
10 changes: 10 additions & 0 deletions apps/auth-svc/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { TResult } from '@jetstyle/utils'

export type AuthMethod = 'username-password'
| 'email-password'
| 'email-opt'
Expand All @@ -6,6 +8,14 @@ export type AuthMethod = 'username-password'
| 'magic-link'
| 'github'

export type SMSSender = {
sendSms(phone: string, text: string, extra?: { code: string }): Promise<TResult<boolean>>
}

export type EmailSender = {
sendEmail(email: string, text: string, extra?: { code: string }): Promise<TResult<boolean>>
}

export type AuthServerConfig = {
port: number
authMethods: Array<AuthMethod>
Expand Down
Loading