@xcelsior/auth 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +0,0 @@
1
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
2
- import {
3
- DynamoDBDocumentClient,
4
- PutCommand,
5
- GetCommand,
6
- UpdateCommand,
7
- DeleteCommand,
8
- QueryCommand,
9
- } from '@aws-sdk/lib-dynamodb';
10
- import type { User } from '../types';
11
- import type { IStorageProvider, DynamoDBConfig } from './types';
12
-
13
- export class DynamoDBStorageProvider implements IStorageProvider {
14
- private client: DynamoDBDocumentClient;
15
- private tableName: string;
16
-
17
- constructor(config: DynamoDBConfig) {
18
- const dbClient = new DynamoDBClient({ region: config.region });
19
- this.client = DynamoDBDocumentClient.from(dbClient, {});
20
- this.tableName = config.tableName;
21
- }
22
-
23
- async createUser(user: User): Promise<void> {
24
- await this.client.send(
25
- new PutCommand({
26
- TableName: this.tableName,
27
- Item: user,
28
- ConditionExpression: 'attribute_not_exists(email)',
29
- })
30
- );
31
- }
32
-
33
- async getUserByResetPasswordToken(resetPasswordToken: string): Promise<User | null> {
34
- const response = await this.client.send(
35
- new QueryCommand({
36
- TableName: this.tableName,
37
- IndexName: 'ResetPasswordTokenIndex',
38
- KeyConditionExpression: 'resetPasswordToken = :token',
39
- ExpressionAttributeValues: {
40
- ':token': resetPasswordToken,
41
- },
42
- })
43
- );
44
-
45
- return response.Items?.[0] as User | null;
46
- }
47
-
48
- async getUserByVerifyEmailToken(verifyEmailToken: string): Promise<User | null> {
49
- const response = await this.client.send(
50
- new QueryCommand({
51
- TableName: this.tableName,
52
- IndexName: 'VerifyEmailTokenIndex',
53
- KeyConditionExpression: 'verificationToken = :token',
54
- ExpressionAttributeValues: {
55
- ':token': verifyEmailToken,
56
- },
57
- })
58
- );
59
-
60
- return response.Items?.[0] as User | null;
61
- }
62
-
63
- async getUserById(id: string): Promise<User | null> {
64
- const response = await this.client.send(
65
- new GetCommand({
66
- TableName: this.tableName,
67
- Key: { id },
68
- })
69
- );
70
-
71
- return response.Item as User | null;
72
- }
73
-
74
- async getUserByEmail(email: string): Promise<User | null> {
75
- const response = await this.client.send(
76
- new QueryCommand({
77
- TableName: this.tableName,
78
- IndexName: 'EmailIndex',
79
- KeyConditionExpression: 'email = :email',
80
- ExpressionAttributeValues: {
81
- ':email': email,
82
- },
83
- })
84
- );
85
-
86
- return response.Items?.[0] as User | null;
87
- }
88
-
89
- async updateUser(id: string, updates: Partial<User>): Promise<void> {
90
- const toSet: Record<string, any> = {};
91
- const toRemove: string[] = [];
92
-
93
- // Separate attributes to set and remove
94
- Object.entries(updates).forEach(([key, value]) => {
95
- if (value === undefined) {
96
- toRemove.push(key);
97
- } else {
98
- toSet[key] = value;
99
- }
100
- });
101
-
102
- // If no updates at all, return early
103
- if (Object.keys(toSet).length === 0 && toRemove.length === 0) {
104
- return;
105
- }
106
-
107
- // Build the update expression
108
- const setPart =
109
- Object.keys(toSet).length > 0
110
- ? `SET ${Object.keys(toSet)
111
- .map((key) => `#${key} = :${key}`)
112
- .join(', ')}`
113
- : '';
114
-
115
- const removePart =
116
- toRemove.length > 0 ? `REMOVE ${toRemove.map((key) => `#${key}`).join(', ')}` : '';
117
-
118
- const updateExpression = [setPart, removePart].filter(Boolean).join(' ');
119
-
120
- // Build expression attribute names (needed for both SET and REMOVE)
121
- const expressionAttributeNames = [...Object.keys(toSet), ...toRemove].reduce(
122
- (acc, key) => ({ ...acc, [`#${key}`]: key }),
123
- {}
124
- );
125
-
126
- // Build expression attribute values (only needed for SET)
127
- const expressionAttributeValues = Object.entries(toSet).reduce(
128
- (acc, [key, value]) => ({ ...acc, [`:${key}`]: value }),
129
- {}
130
- );
131
-
132
- await this.client.send(
133
- new UpdateCommand({
134
- TableName: this.tableName,
135
- Key: { id },
136
- UpdateExpression: updateExpression,
137
- ExpressionAttributeNames: expressionAttributeNames,
138
- ...(Object.keys(expressionAttributeValues).length > 0 && {
139
- ExpressionAttributeValues: expressionAttributeValues,
140
- }),
141
- })
142
- );
143
- }
144
-
145
- async deleteUser(id: string): Promise<void> {
146
- await this.client.send(
147
- new DeleteCommand({
148
- TableName: this.tableName,
149
- Key: { id },
150
- })
151
- );
152
- }
153
- }