Skip to content

Commit

Permalink
feat: username+password auth support (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
OldManFroggy authored Jun 3, 2022
1 parent 20fe317 commit 84184f8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface RedisConnectionOptions {
tls?: boolean;
db?: number;
password?: string;
username?: string;
name?: string;
maxRetryCount?: number;
// TODO: Provide more flexible retry strategy
Expand Down Expand Up @@ -95,7 +96,7 @@ export class RedisConnection implements Connection {

try {
if (options?.password != null) {
await this.authenticate(options.password);
await this.authenticate(options?.username, options?.password);
}
if (options?.db) {
await this.selectDb(options.db);
Expand All @@ -109,8 +110,13 @@ export class RedisConnection implements Connection {
};
}

private async authenticate(password: string): Promise<void> {
await this.sendCommand("AUTH", password);
private async authenticate(
username: string | undefined,
password: string,
): Promise<void> {
password && username
? await this.sendCommand("AUTH", username, password)
: await this.sendCommand("AUTH", password);
}

private async selectDb(
Expand Down

0 comments on commit 84184f8

Please sign in to comment.