2023-06-26 13:46:12 +00:00
|
|
|
import { Collection } from "https://deno.land/x/mongo@v0.31.2/mod.ts";
|
2023-06-26 15:07:25 +00:00
|
|
|
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
|
|
|
|
|
2023-06-27 03:18:19 +00:00
|
|
|
import { auth, body, rest, schema } from "../server/decorators.ts";
|
2023-06-26 14:06:38 +00:00
|
|
|
import HttpError from "../server/HttpError.ts";
|
2023-06-26 13:46:12 +00:00
|
|
|
import User from "../models/User.ts";
|
|
|
|
import AppRouter from "./AppRouter.ts";
|
2023-06-27 03:18:19 +00:00
|
|
|
import jwt from "../server/decorators/jwt.ts";
|
|
|
|
import JWT, { JWTPayload } from "../utils/JWT.ts";
|
|
|
|
import now from "../utils/now.ts";
|
2023-06-26 13:46:12 +00:00
|
|
|
|
2023-06-27 03:05:11 +00:00
|
|
|
export interface SignupRequest {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
export interface LoginRequest {
|
2023-06-26 13:46:12 +00:00
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class UserRouter extends AppRouter {
|
2023-06-27 03:18:19 +00:00
|
|
|
@rest('GET', '/')
|
2023-06-26 13:46:12 +00:00
|
|
|
async get(@schema('string') username: string) {
|
|
|
|
const res = await this.users.findOne({ username });
|
|
|
|
|
|
|
|
if (res === undefined) throw new HttpError('User not found.');
|
|
|
|
|
|
|
|
return { username: res.username };
|
|
|
|
}
|
2023-06-27 03:18:19 +00:00
|
|
|
@rest('GET', '/self')
|
|
|
|
async self(@jwt(self => self.salt, true) @auth() auth: JWTPayload) {
|
|
|
|
if (auth === undefined) throw new HttpError('You are not logged in.');
|
|
|
|
const res = await this.users.findOne({ username: auth.name });
|
|
|
|
|
|
|
|
if (res === undefined) throw new HttpError('User not found.');
|
|
|
|
|
|
|
|
return { username: res.username };
|
|
|
|
}
|
2023-06-27 03:05:11 +00:00
|
|
|
|
2023-06-26 13:46:12 +00:00
|
|
|
@rest('POST', '/signup')
|
|
|
|
async signup(
|
|
|
|
@schema({
|
2023-06-27 03:05:11 +00:00
|
|
|
username: 'string',
|
|
|
|
password: 'string',
|
2023-06-26 13:46:12 +00:00
|
|
|
}) @body() body: SignupRequest
|
|
|
|
) {
|
|
|
|
if (await this.users.countDocuments({ username: body.username }) > 0) {
|
|
|
|
throw new HttpError('User with the same username already exists.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const password = await bcrypt.hash(body.password, this.salt);
|
|
|
|
|
|
|
|
await this.users.insertOne({
|
|
|
|
username: body.username,
|
|
|
|
password: password,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2023-06-27 03:05:11 +00:00
|
|
|
@rest('POST', '/login')
|
|
|
|
async login(
|
|
|
|
@schema({
|
|
|
|
username: 'string',
|
|
|
|
password: 'string'
|
|
|
|
}) @body() body: LoginRequest
|
|
|
|
) {
|
|
|
|
const res = await this.users.findOne({ username: body.username });
|
|
|
|
if (!res) throw new HttpError('Incorrect username or password.');
|
2023-06-27 03:18:19 +00:00
|
|
|
const hashed = await bcrypt.hash(res.password, this.salt);
|
|
|
|
|
|
|
|
if (res.password !== hashed) throw new HttpError('Incorrect username or password.');
|
|
|
|
|
|
|
|
const time = now();
|
|
|
|
return JWT.encode({
|
|
|
|
iat: time,
|
|
|
|
exp: time + 3600 * 12,
|
|
|
|
name: res.username,
|
|
|
|
}, this.salt);
|
2023-06-27 03:05:11 +00:00
|
|
|
}
|
2023-06-26 13:46:12 +00:00
|
|
|
|
|
|
|
public constructor(private salt: string, private users: Collection<User>) {
|
|
|
|
super();
|
|
|
|
users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] });
|
|
|
|
}
|
|
|
|
}
|