Skip to content

Commit a4d2820

Browse files
committed
rename some args
1 parent b9bb209 commit a4d2820

File tree

15 files changed

+65
-60
lines changed

15 files changed

+65
-60
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ Don't forget to re-login after verifying the email verification code.
187187
Registers a new user in the database if they don’t already exist. It handles OAuth authentication by registering the OAuth account, creating a session, and linking the user’s details.
188188
- **Returns**: A tuple containing the user ID and the created session details.
189189

190-
##### `getUser(userId: string)`
190+
##### `getUser(id: string)`
191191

192192
Fetches a user by its user ID.
193193

194-
##### `getSession(sessionId: string)`
194+
##### `getSession(id: string)`
195195

196196
Fetches a session by its session ID.
197197

198-
##### `deleteSession(sessionId: string)`
198+
##### `deleteSession(id: string)`
199199

200200
Deletes a session by its session ID.
201201

@@ -262,6 +262,12 @@ Sets custom methods for hashing and verifying passwords.
262262

263263
Sets custom method for reset password token hashing.
264264

265+
##### `setLoginRateLimiter(fn: () => Storage)`
266+
##### `setAskEmailRateLimiter(fn: () => Storage)`
267+
##### `setVerifyEmailRateLimiter(fn: () => Storage)`
268+
##### `setAskResetPasswordRateLimiter(fn: () => Storage)`
269+
##### `setVerifyResetPasswordRateLimiter(fn: () => Storage)`
270+
265271
## Database migraions
266272

267273
By default, nuxt-slip-auth will create tables in your database for you !
@@ -323,7 +329,7 @@ You should have your migrations in the migrations folder.
323329
- [x] rate-limit login
324330
- [x] rate-limit email verification
325331
- [x] rate-limit forgot password
326-
- [ ] rate-limit reset password
332+
- [x] rate-limit reset password
327333
- [x] ~~rate limit register~~ (rate-limit ask email verification)
328334
- [ ] error message strategy (email already taken, etc)
329335
- [ ] oauth accounts linking

playground/server/api/session.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default defineEventHandler(async (event) => {
22
const auth = useSlipAuth();
33
const { id } = await requireUserSession(event);
44

5-
const session = await auth.getSession({ sessionId: id });
5+
const session = await auth.getSession({ id: id });
66

77
return session;
88
});

src/runtime/core/core.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class SlipAuthCore {
131131
await this.#rateLimiters.login.reset(existingUser.id);
132132
const sessionToLoginId = this.#createRandomSessionId();
133133
const sessionToLogin = await this.#repos.sessions.insert({
134-
sessionId: sessionToLoginId,
134+
id: sessionToLoginId,
135135
userId: existingUser.id,
136136
expiresAt: Date.now() + this.#sessionMaxAge,
137137
ip: values.ip,
@@ -156,14 +156,14 @@ export class SlipAuthCore {
156156

157157
try {
158158
const user = await this.#repos.users.insert({
159-
userId,
159+
id: userId,
160160
email,
161161
password: passwordHash,
162162
});
163163
await this.askEmailVerificationCode(h3Event, { user });
164164
const sessionToLoginId = this.#createRandomSessionId();
165165
const sessionToLogin = await this.#repos.sessions.insert({
166-
sessionId: sessionToLoginId,
166+
id: sessionToLoginId,
167167
userId: user.id,
168168
expiresAt: Date.now() + this.#sessionMaxAge,
169169
ip: values.ip,
@@ -201,7 +201,7 @@ export class SlipAuthCore {
201201
if (!existingUser) {
202202
const userId = this.#createRandomUserId();
203203

204-
await this.#repos.users.insert({ userId: userId, email: params.email });
204+
await this.#repos.users.insert({ id: userId, email: params.email });
205205

206206
const _insertedOAuthAccount = await this.#repos.oAuthAccounts.insert({
207207
email: params.email,
@@ -212,7 +212,7 @@ export class SlipAuthCore {
212212

213213
const sessionFromRegistrationId = this.#createRandomSessionId();
214214
const sessionFromRegistration = await this.#repos.sessions.insert({
215-
sessionId: sessionFromRegistrationId,
215+
id: sessionFromRegistrationId,
216216
userId,
217217
expiresAt: Date.now() + this.#sessionMaxAge,
218218
ip: params.ip,
@@ -234,7 +234,7 @@ export class SlipAuthCore {
234234
if (existingAccount) {
235235
const sessionFromLoginId = this.#createRandomSessionId();
236236
const sessionFromLogin = await this.#repos.sessions.insert({
237-
sessionId: sessionFromLoginId,
237+
id: sessionFromLoginId,
238238
userId: existingUser.id,
239239
expiresAt: Date.now() + this.#sessionMaxAge,
240240
ua: params.ua,
@@ -303,7 +303,7 @@ export class SlipAuthCore {
303303
throw new EmailVerificationFailedError();
304304
}
305305

306-
await this.#repos.users.updateEmailVerifiedByUserId({ userId: databaseCode.user_id, value: true });
306+
await this.#repos.users.updateEmailVerifiedByUserId({ id: databaseCode.user_id, value: true });
307307
// TODO: All sessions should be invalidated when the email is verified (and create a new one for the current user so they stay signed in).
308308
return true;
309309
}
@@ -397,7 +397,7 @@ export class SlipAuthCore {
397397

398398
await this.#repos.sessions.deleteAllByUserId(token.user_id);
399399
const passwordHash = await this.#passwordHashingMethods.hash(params.newPassword);
400-
await this.#repos.users.updatePasswordByUserId({ userId: token.user_id, password: passwordHash });
400+
await this.#repos.users.updatePasswordByUserId({ id: token.user_id, password: passwordHash });
401401

402402
// await this.#rateLimiters.verifyResetPassword.reset(token.user_id);
403403
return true;
@@ -428,7 +428,6 @@ export class SlipAuthCore {
428428
setLoginRateLimiter: (fn: () => Storage) => {
429429
this.#rateLimiters.login.storage = fn();
430430
},
431-
432431
setAskEmailRateLimiter: (fn: () => Storage) => {
433432
this.#rateLimiters.askEmailVerification.storage = fn();
434433
},
@@ -443,16 +442,16 @@ export class SlipAuthCore {
443442
},
444443
};
445444

446-
public getUser({ userId }: { userId: string }) {
447-
return this.#repos.users.findById({ userId });
445+
public getUser({ id }: { id: string }) {
446+
return this.#repos.users.findById({ id });
448447
}
449448

450-
public getSession({ sessionId }: { sessionId: string }) {
451-
return this.#repos.sessions.findById({ sessionId });
449+
public getSession({ id }: { id: string }) {
450+
return this.#repos.sessions.findById({ id: id });
452451
}
453452

454-
public deleteSession({ sessionId }: { sessionId: string }) {
455-
return this.#repos.sessions.deleteById({ sessionId });
453+
public deleteSession({ id }: { id: string }) {
454+
return this.#repos.sessions.deleteById({ id });
456455
}
457456

458457
public deleteExpiredSessions({ timestamp }: { timestamp: number }) {

src/runtime/core/repositories/SessionsRepository.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ import { TableRepository } from "./_repo";
33
import type { ICreateSessionsParams } from "../types";
44

55
export class SessionsRepository extends TableRepository<"sessions"> {
6-
async insert({ sessionId, userId, expiresAt, ip, ua }: ICreateSessionsParams): Promise<typeof this.table.$inferSelect> {
6+
async insert({ id, userId, expiresAt, ip, ua }: ICreateSessionsParams): Promise<typeof this.table.$inferSelect> {
77
await this._orm
88
.insert(this.table)
99
.values({
10-
id: sessionId,
10+
id: id,
1111
expires_at: expiresAt,
1212
user_id: userId,
1313
ip,
1414
ua,
1515
}).run();
1616

17-
const sessionInserted = await this.findById({ sessionId });
17+
const sessionInserted = await this.findById({ id: id });
1818
if (!sessionInserted) {
19-
throw new Error(`Session ${sessionId} not found after insert`);
19+
throw new Error(`Session ${id} not found after insert`);
2020
}
2121

2222
this._hooks.callHookParallel("sessions:create", sessionInserted);
2323

2424
return sessionInserted;
2525
}
2626

27-
async findById({ sessionId }: { sessionId: string }): Promise<typeof this.table.$inferSelect | undefined> {
27+
async findById({ id }: { id: string }): Promise<typeof this.table.$inferSelect | undefined> {
2828
const rows = await this._orm
2929
.select()
3030
.from(this.table)
3131
.where(
32-
eq(this.table.id, sessionId),
32+
eq(this.table.id, id),
3333
);
3434
const user = this.getRawSQlResults(rows).at(0);
3535

@@ -66,23 +66,23 @@ export class SessionsRepository extends TableRepository<"sessions"> {
6666
return { success: this.getRawSQlResults(deletedSessions).length === 0, count: this.getRawSQlResults(sessionsToDelete).length };
6767
}
6868

69-
async deleteById({ sessionId }: { sessionId: string }) {
70-
const sessionToDelete = await this.findById({ sessionId });
69+
async deleteById({ id }: { id: string }) {
70+
const sessionToDelete = await this.findById({ id: id });
7171

7272
if (!sessionToDelete) {
73-
throw new Error(`Unable to delete session with id ${sessionId}`);
73+
throw new Error(`Unable to delete session with id ${id}`);
7474
}
7575

7676
await this._orm
7777
.delete(this.table)
7878
.where(
79-
eq(this.table.id, sessionId),
79+
eq(this.table.id, id),
8080
)
8181
.run();
8282

8383
// TODO: fix typings in db0 / drizzle
8484
// as the delete from drizzle returns any we do an extra query to check if the deletion went fine
85-
const expiredSession = await this.findById({ sessionId });
85+
const expiredSession = await this.findById({ id: id });
8686

8787
if (expiredSession) {
8888
return { success: false };

src/runtime/core/repositories/UsersRepository.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ import { eq } from "drizzle-orm";
22
import { TableRepository } from "./_repo";
33

44
export class UsersRepository extends TableRepository<"users"> {
5-
async insert({ userId, email, password }: { userId: string, email: string, password?: string }): Promise<typeof this.table.$inferSelect> {
5+
async insert({ id, email, password }: { id: string, email: string, password?: string }): Promise<typeof this.table.$inferSelect> {
66
await this._orm
77
.insert(this.table)
88
.values({
9-
id: userId,
9+
id: id,
1010
email,
1111
password,
1212
}).run();
1313

14-
const user = await this.findById({ userId });
14+
const user = await this.findById({ id });
1515
if (!user) {
16-
throw new Error(`User ${userId} not found after insert`);
16+
throw new Error(`User ${id} not found after insert`);
1717
}
1818

1919
this._hooks.callHookParallel("users:create", user);
2020

2121
return user;
2222
}
2323

24-
async findById({ userId }: { userId: string }): Promise<typeof this.table.$inferSelect | undefined> {
24+
async findById({ id }: { id: string }): Promise<typeof this.table.$inferSelect | undefined> {
2525
const rows = await this._orm
2626
.select()
2727
.from(this.table)
2828
.where(
29-
eq(this.table.id, userId),
29+
eq(this.table.id, id),
3030
);
3131
const user = this.getRawSQlResults(rows).at(0);
3232

@@ -45,23 +45,23 @@ export class UsersRepository extends TableRepository<"users"> {
4545
return user;
4646
}
4747

48-
updatePasswordByUserId = async ({ userId, password }: { userId: string, password: string }): Promise<void> => {
48+
updatePasswordByUserId = async ({ id, password }: { id: string, password: string }): Promise<void> => {
4949
return await this._orm
5050
.update(this.table)
5151
.set({
5252
password,
5353
})
54-
.where(eq(this.table.id, userId))
54+
.where(eq(this.table.id, id))
5555
.run();
5656
};
5757

58-
updateEmailVerifiedByUserId = async ({ userId, value }: { userId: string, value: boolean }): Promise<void> => {
58+
updateEmailVerifiedByUserId = async ({ id, value }: { id: string, value: boolean }): Promise<void> => {
5959
return await this._orm
6060
.update(this.table)
6161
.set({
6262
email_verified: value,
6363
})
64-
.where(eq(this.table.id, userId))
64+
.where(eq(this.table.id, id))
6565
.run();
6666
};
6767
}

src/runtime/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface ICreateSessionsParams extends ISessionCreateMetada {
1919
expiresAt: number
2020
ip?: string
2121
ua?: string
22-
sessionId: string
22+
id: string
2323
}
2424

2525
export interface ICreateUserParams extends ISessionCreateMetada {

src/runtime/h3/routes/ask-email-verification.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineEventHandler(async (event) => {
88
const userId = session.user.id;
99

1010
try {
11-
const user = await auth.getUser({ userId });
11+
const user = await auth.getUser({ id: userId });
1212

1313
if (!user) {
1414
throw new Error("no user");

src/runtime/h3/routes/ask-password-reset.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineEventHandler(async (event) => {
77
const userId = session.user.id;
88

99
try {
10-
const user = await auth.getUser({ userId });
10+
const user = await auth.getUser({ id: userId });
1111

1212
if (!user) {
1313
throw new Error("no user");

src/runtime/h3/routes/login.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
1212
...body,
1313
ua: getHeader(event, "User-Agent"),
1414
});
15-
const user = await auth.getUser({ userId });
15+
const user = await auth.getUser({ id: userId });
1616

1717
if (!user) {
1818
return false;

src/runtime/h3/routes/register.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
1313
ua: getHeader(event, "User-Agent"),
1414
});
1515

16-
const user = await auth.getUser({ userId });
16+
const user = await auth.getUser({ id: userId });
1717

1818
if (!user) {
1919
return false;

0 commit comments

Comments
 (0)