refactor: centralize base64 encoding
This commit is contained in:
@@ -9,6 +9,7 @@ import { EmailVerificationToken } from './auth.types';
|
||||
import type { CouchDBDocument } from '../../types';
|
||||
import { getDatabaseConfig } from '../../config/unified.config';
|
||||
import { logger } from '../logging';
|
||||
import { encodeBase64 } from '../../utils/base64';
|
||||
|
||||
export interface PasswordResetToken {
|
||||
userId: string;
|
||||
@@ -42,14 +43,8 @@ function fromISO(date: string | Date): Date {
|
||||
return date instanceof Date ? date : new Date(date);
|
||||
}
|
||||
|
||||
function base64Auth(user: string, pass: string): string {
|
||||
// btoa may not exist in some environments (e.g., Node). Fallback to Buffer.
|
||||
if (typeof btoa !== 'undefined') {
|
||||
return btoa(`${user}:${pass}`);
|
||||
}
|
||||
|
||||
return Buffer.from(`${user}:${pass}`).toString('base64');
|
||||
}
|
||||
const base64Auth = (user: string, pass: string): string =>
|
||||
encodeBase64(`${user}:${pass}`);
|
||||
|
||||
export class TokenService {
|
||||
private couchBaseUrl: string | null = null;
|
||||
|
||||
@@ -12,6 +12,7 @@ import { AccountStatus } from '../auth/auth.constants';
|
||||
import { DatabaseStrategy, DatabaseError } from './types';
|
||||
import { getDatabaseConfig } from '../../config/unified.config';
|
||||
import { logger } from '../logging';
|
||||
import { encodeBase64 } from '../../utils/base64';
|
||||
|
||||
export class ProductionDatabaseStrategy implements DatabaseStrategy {
|
||||
private baseUrl: string;
|
||||
@@ -22,7 +23,7 @@ export class ProductionDatabaseStrategy implements DatabaseStrategy {
|
||||
const dbConfig = getDatabaseConfig();
|
||||
|
||||
this.baseUrl = dbConfig.url;
|
||||
this.auth = btoa(`${dbConfig.username}:${dbConfig.password}`);
|
||||
this.auth = encodeBase64(`${dbConfig.username}:${dbConfig.password}`);
|
||||
|
||||
logger.db.query('Initializing production database strategy', {
|
||||
url: dbConfig.url,
|
||||
|
||||
@@ -7,6 +7,7 @@ import { getMailgunConfig, type MailgunConfig } from './mailgun.config';
|
||||
import { getAppConfig } from '../config/unified.config';
|
||||
import { logger } from './logging';
|
||||
import { normalizeError } from '../utils/error';
|
||||
import { encodeBase64 } from '../utils/base64';
|
||||
|
||||
interface EmailTemplate {
|
||||
subject: string;
|
||||
@@ -146,7 +147,7 @@ export class MailgunService {
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Basic ${btoa(`api:${this.config.apiKey}`)}`,
|
||||
Authorization: `Basic ${encodeBase64(`api:${this.config.apiKey}`)}`,
|
||||
},
|
||||
body: formData,
|
||||
}
|
||||
|
||||
7
utils/base64.ts
Normal file
7
utils/base64.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const encodeBase64 = (value: string): string => {
|
||||
if (typeof btoa !== 'undefined') {
|
||||
return btoa(value);
|
||||
}
|
||||
|
||||
return Buffer.from(value, 'utf-8').toString('base64');
|
||||
};
|
||||
Reference in New Issue
Block a user