2024-11-06 17:49:18 +00:00
|
|
|
import 'reflect-metadata';
|
2024-11-08 21:57:13 +00:00
|
|
|
import {Controller} from '$lib/server/api/common/types/controller';
|
|
|
|
|
import {signupUsernameEmailDto} from '$lib/server/api/dtos/signup-username-email.dto';
|
|
|
|
|
import {limiter} from '$lib/server/api/middleware/rate-limiter.middleware';
|
|
|
|
|
import {LoginRequestsService} from '$lib/server/api/services/loginrequest.service';
|
|
|
|
|
import {SessionsService} from '$lib/server/api/services/sessions.service';
|
|
|
|
|
import {UsersService} from '$lib/server/api/services/users.service';
|
|
|
|
|
import {zValidator} from '@hono/zod-validator';
|
|
|
|
|
import {setCookie} from 'hono/cookie';
|
|
|
|
|
import {TimeSpan} from 'oslo';
|
|
|
|
|
import {inject, injectable} from 'tsyringe';
|
2024-08-08 19:38:17 +00:00
|
|
|
|
|
|
|
|
@injectable()
|
2024-09-04 23:04:41 +00:00
|
|
|
export class SignupController extends Controller {
|
2024-08-08 19:38:17 +00:00
|
|
|
constructor(
|
2024-09-01 19:22:00 +00:00
|
|
|
@inject(UsersService) private readonly usersService: UsersService,
|
|
|
|
|
@inject(LoginRequestsService) private readonly loginRequestService: LoginRequestsService,
|
2024-11-06 17:49:18 +00:00
|
|
|
@inject(SessionsService) private luciaService: SessionsService,
|
2024-09-04 23:04:41 +00:00
|
|
|
) {
|
2024-11-06 17:49:18 +00:00
|
|
|
super();
|
2024-09-04 23:04:41 +00:00
|
|
|
}
|
2024-08-08 19:38:17 +00:00
|
|
|
|
|
|
|
|
routes() {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.controller.post('/', zValidator('json', signupUsernameEmailDto), limiter({ limit: 10, minutes: 60 }), async (c) => {
|
2024-11-06 17:49:18 +00:00
|
|
|
const { firstName, lastName, email, username, password, confirm_password } = await c.req.valid('json');
|
|
|
|
|
const existingUser = await this.usersService.findOneByUsername(username);
|
2024-08-10 17:03:30 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
if (existingUser) {
|
2024-11-06 17:49:18 +00:00
|
|
|
return c.body('User already exists', 400);
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|
2024-08-10 17:03:30 +00:00
|
|
|
|
2024-11-06 17:49:18 +00:00
|
|
|
const user = await this.usersService.create({ firstName, lastName, email, username, password, confirm_password });
|
2024-08-08 19:38:17 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
if (!user) {
|
2024-11-06 17:49:18 +00:00
|
|
|
return c.body('Failed to create user', 500);
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|
2024-08-08 19:38:17 +00:00
|
|
|
|
2024-11-06 17:49:18 +00:00
|
|
|
const session = await this.loginRequestService.createUserSession(user.id, c.req, undefined);
|
|
|
|
|
const sessionCookie = this.luciaService.lucia.createSessionCookie(session.id);
|
|
|
|
|
console.log('set cookie', sessionCookie);
|
2024-09-01 19:22:00 +00:00
|
|
|
setCookie(c, sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
path: sessionCookie.attributes.path,
|
|
|
|
|
maxAge:
|
|
|
|
|
sessionCookie?.attributes?.maxAge && sessionCookie?.attributes?.maxAge < new TimeSpan(365, 'd').seconds()
|
|
|
|
|
? sessionCookie.attributes.maxAge
|
|
|
|
|
: new TimeSpan(2, 'w').seconds(),
|
|
|
|
|
domain: sessionCookie.attributes.domain,
|
|
|
|
|
sameSite: sessionCookie.attributes.sameSite as any,
|
|
|
|
|
secure: sessionCookie.attributes.secure,
|
|
|
|
|
httpOnly: sessionCookie.attributes.httpOnly,
|
|
|
|
|
expires: sessionCookie.attributes.expires,
|
2024-11-06 17:49:18 +00:00
|
|
|
});
|
|
|
|
|
return c.json({ message: 'ok' });
|
|
|
|
|
});
|
2024-08-08 19:38:17 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|