src/lib/auth/pin_code.ts
import { App } from '$lib/app/app'
import { randomInt } from 'crypto'
export class PinCode {
private static readonly _pin_code_chars = '0123456789'
private readonly _code: string
public constructor(code: string | undefined) {
if (!code) throw new Error('PIN code is required')
// if (code.length < 6) throw new Error('PIN code is too short')
if (code.length > 50) throw new Error('PIN code is too long')
this._code = code
}
public get code(): string {
return this._code
}
public static generate(length = 6): PinCode {
let code = ''
while (code.length < length) {
code += PinCode._pin_code_chars[randomInt(PinCode._pin_code_chars.length)]
}
const pin_code = new PinCode(code)
return pin_code
}
public get_html(): string {
const html = `
<html>
<body>
<div style="margin-left: auto;margin-right: auto;width: 100%;max-width: 500px;font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";">
<h1 style="margin-top: 2.5rem;margin-bottom: 0.5rem;font-size: 2rem;line-height: 2.25rem;font-weight: 800;">${App.company_and_app_name}</h1>
<h2 style="margin-top: 2.5rem;margin-bottom: 0.5rem;font-size: 1.875rem;line-height: 2.25rem;font-weight: 600;">Sign in with code</h2>
<p style="margin-bottom: 1.25rem;font-size: 1.25rem;line-height: 1.75rem;">Enter the following code into the app to sign in.</p>
<p style="width: 100%;background-color: #F3F4F6;padding-top: 30px;padding-bottom: 30px;text-align: center;font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;font-size: 1.875rem;line-height: 2.25rem; margin-bottom: 3rem;">
${this._code}
</p>
<p style="margin-bottom: 1.25rem;font-size: 1.05rem;line-height: 1.75rem;">If you didn’t request this email, you can safely ignore it.</p>
</div>
</body>
</html>`
return html
}
}