1 |
import { Request } from "express"; |
import { Request } from "express"; |
2 |
import User from "../../models/User"; |
import User from "../../models/User"; |
3 |
import Controller from "../Controller"; |
import Controller from "../Controller"; |
4 |
import { body, validationResult } from 'express-validator'; |
import { body } from 'express-validator'; |
5 |
import bcrypt from 'bcrypt'; |
import bcrypt from 'bcrypt'; |
6 |
import jwt from 'jsonwebtoken'; |
import jwt from 'jsonwebtoken'; |
7 |
import KeyValuePair from "../../types/KeyValuePair"; |
import KeyValuePair from "../../types/KeyValuePair"; |
|
import { promise } from "zod"; |
|
8 |
import Response from "../Response"; |
import Response from "../Response"; |
9 |
|
import ValidatorError from "../middleware/ValidatorError"; |
10 |
|
import RequireAuth from "../middleware/RequireAuth"; |
11 |
|
|
12 |
export default class UserController extends Controller { |
export default class UserController extends Controller { |
13 |
middleware(): KeyValuePair<Function[]> { |
middleware(): KeyValuePair<Function[]> { |
24 |
return username; |
return username; |
25 |
}), |
}), |
26 |
body(["discord_id"]).custom(value => /\d+/g.test(value) ? value : Promise.reject("Invalid Snowflake Given")) |
body(["discord_id"]).custom(value => /\d+/g.test(value) ? value : Promise.reject("Invalid Snowflake Given")) |
27 |
|
], |
28 |
|
login: [ |
29 |
|
body(["username", "password"]).isLength({ min: 2 }), |
30 |
|
], |
31 |
|
delete: [ |
32 |
|
RequireAuth, |
33 |
|
body(["username", "password"]).isLength({ min: 2 }), |
34 |
] |
] |
35 |
}; |
}; |
36 |
} |
} |
37 |
|
|
38 |
|
globalMiddleware(): Function[] { |
39 |
|
return [ValidatorError]; |
40 |
|
} |
41 |
|
|
42 |
public async index() { |
public async index() { |
43 |
return await User.find().limit(30); |
return new Response(403); |
44 |
|
return await User.find().select(["_id", "username", "createdAt"]).limit(30); |
45 |
} |
} |
46 |
|
|
47 |
public async create(request: Request) { |
public async create(request: Request) { |
48 |
return new Response(403); |
return new Response(403); |
|
|
|
|
const errors = validationResult(request); |
|
|
|
|
|
if (!errors.isEmpty()) { |
|
|
return { errors: errors.array(), error_type: 'validation' }; |
|
|
} |
|
49 |
|
|
50 |
const user = new User(); |
const user = new User(); |
51 |
|
|
52 |
user.username = request.body.username; |
user.username = request.body.username; |
53 |
user.discord_id = request.body.discord_id; |
user.discord_id = request.body.discord_id; |
54 |
user.createdAt = new Date(); |
user.createdAt = new Date(); |
55 |
|
user.tokenUpdatedAt = new Date(); |
56 |
|
|
57 |
try { |
try { |
58 |
await user.save(); |
await user.save(); |
85 |
user.password = undefined; |
user.password = undefined; |
86 |
return user; |
return user; |
87 |
} |
} |
88 |
|
|
89 |
|
public async delete(request: Request) { |
90 |
|
const { username, password } = request.body; |
91 |
|
const user = await User.findOne({ username }); |
92 |
|
|
93 |
|
if (!user) { |
94 |
|
return { error: "Username is incorrect." }; |
95 |
|
} |
96 |
|
|
97 |
|
if (!(await bcrypt.compare(password, user.password!))) { |
98 |
|
return { error: "Password is incorrect." }; |
99 |
|
} |
100 |
|
|
101 |
|
await user.delete(); |
102 |
|
|
103 |
|
user.password = undefined; |
104 |
|
user.token = undefined; |
105 |
|
user.tokenUpdatedAt = undefined; |
106 |
|
|
107 |
|
return { |
108 |
|
message: "Account deletion successful", |
109 |
|
user |
110 |
|
}; |
111 |
|
} |
112 |
|
|
113 |
|
public async login(request: Request) { |
114 |
|
const { username, password } = request.body; |
115 |
|
const user = await User.findOne({ username }); |
116 |
|
|
117 |
|
if (!user) { |
118 |
|
return { error: "Username is incorrect." }; |
119 |
|
} |
120 |
|
|
121 |
|
if (!(await bcrypt.compare(password, user.password!))) { |
122 |
|
return { error: "Password is incorrect." }; |
123 |
|
} |
124 |
|
|
125 |
|
let { token } = user; |
126 |
|
|
127 |
|
try { |
128 |
|
if (!token) { |
129 |
|
throw new Error("Token is not set"); |
130 |
|
} |
131 |
|
|
132 |
|
if (!jwt.verify(token, process.env.JWT_SECRET!)) { |
133 |
|
throw new Error("Token is not valid"); |
134 |
|
} |
135 |
|
} |
136 |
|
catch (e) { |
137 |
|
console.log(e); |
138 |
|
|
139 |
|
const newToken = await jwt.sign({ |
140 |
|
username: user.username, |
141 |
|
discord_id: user.discord_id, |
142 |
|
_id: user.id |
143 |
|
}, process.env.JWT_SECRET!, { |
144 |
|
expiresIn: "2 days", |
145 |
|
issuer: "SudoBot API", |
146 |
|
}); |
147 |
|
|
148 |
|
token = newToken; |
149 |
|
user.tokenUpdatedAt = new Date(); |
150 |
|
user.token = newToken; |
151 |
|
await user.save(); |
152 |
|
} |
153 |
|
|
154 |
|
return { |
155 |
|
message: "Login successful", |
156 |
|
username, |
157 |
|
token, |
158 |
|
expires: new Date(user.tokenUpdatedAt!.getTime() + (2 * 24 * 60 * 60 * 1000)) |
159 |
|
}; |
160 |
|
} |
161 |
} |
} |