oapi: finish users

This commit is contained in:
Puyodead1 2023-03-25 18:01:56 -04:00
parent 1ce7879ee8
commit 860b9d583e
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
14 changed files with 107 additions and 37 deletions

Binary file not shown.

Binary file not shown.

View File

@ -35,8 +35,8 @@ router.post(
"/", "/",
route({ route({
responses: { responses: {
200: { 204: {
body: "TokenResponse", body: "TokenOnlyResponse",
}, },
400: { 400: {
body: "APIErrorResponse", body: "APIErrorResponse",

View File

@ -45,7 +45,7 @@ router.post(
route({ route({
requestBody: "RegisterSchema", requestBody: "RegisterSchema",
responses: { responses: {
200: { body: "TokenResponse" }, 200: { body: "TokenOnlyResponse" },
400: { body: "APIErrorOrCaptchaResponse" }, 400: { body: "APIErrorOrCaptchaResponse" },
}, },
}), }),

View File

@ -38,7 +38,7 @@ router.post(
requestBody: "PasswordResetSchema", requestBody: "PasswordResetSchema",
responses: { responses: {
200: { 200: {
body: "TokenResponse", body: "TokenOnlyResponse",
}, },
400: { 400: {
body: "APIErrorOrCaptchaResponse", body: "APIErrorOrCaptchaResponse",

View File

@ -28,17 +28,37 @@ import { Request, Response, Router } from "express";
const router = Router(); const router = Router();
// GET doesn't exist on discord.com // GET doesn't exist on discord.com
router.get("/", route({}), async (req: Request, res: Response) => { router.get(
const user = await Member.findOneOrFail({ "/",
where: { id: req.user_id, guild_id: req.params.guild_id }, route({
select: ["settings"], responses: {
}); 200: {},
return res.json(user.settings); 404: {},
}); },
}),
async (req: Request, res: Response) => {
const user = await Member.findOneOrFail({
where: { id: req.user_id, guild_id: req.params.guild_id },
select: ["settings"],
});
return res.json(user.settings);
},
);
router.patch( router.patch(
"/", "/",
route({ requestBody: "UserGuildSettingsSchema" }), route({
requestBody: "UserGuildSettingsSchema",
responses: {
200: {},
400: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const body = req.body as UserGuildSettingsSchema; const body = req.body as UserGuildSettingsSchema;

View File

@ -31,7 +31,17 @@ const router = Router();
router.post( router.post(
"/", "/",
route({ requestBody: "TotpDisableSchema" }), route({
requestBody: "TotpDisableSchema",
responses: {
200: {
body: "TokenOnlyResponse",
},
400: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const body = req.body as TotpDisableSchema; const body = req.body as TotpDisableSchema;

View File

@ -32,7 +32,20 @@ const router = Router();
router.post( router.post(
"/", "/",
route({ requestBody: "TotpEnableSchema" }), route({
requestBody: "TotpEnableSchema",
responses: {
200: {
body: "TokenWithBackupCodesResponse",
},
400: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const body = req.body as TotpEnableSchema; const body = req.body as TotpEnableSchema;

View File

@ -21,21 +21,31 @@ import { SecurityKey, User } from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
const router = Router(); const router = Router();
router.delete("/", route({}), async (req: Request, res: Response) => { router.delete(
const { key_id } = req.params; "/",
route({
responses: {
204: {},
},
}),
async (req: Request, res: Response) => {
const { key_id } = req.params;
await SecurityKey.delete({ await SecurityKey.delete({
id: key_id, id: key_id,
user_id: req.user_id, user_id: req.user_id,
}); });
const keys = await SecurityKey.count({ where: { user_id: req.user_id } }); const keys = await SecurityKey.count({
where: { user_id: req.user_id },
});
// disable webauthn if there are no keys left // disable webauthn if there are no keys left
if (keys === 0) if (keys === 0)
await User.update({ id: req.user_id }, { webauthn_enabled: false }); await User.update({ id: req.user_id }, { webauthn_enabled: false });
res.sendStatus(204); res.sendStatus(204);
}); },
);
export default router; export default router;

View File

@ -73,7 +73,17 @@ router.get("/", route({}), async (req: Request, res: Response) => {
router.post( router.post(
"/", "/",
route({ requestBody: "WebAuthnPostSchema" }), route({
requestBody: "WebAuthnPostSchema",
responses: {
200: {
body: "WebAuthnCreateResponse",
},
400: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
if (!WebAuthn.fido2) { if (!WebAuthn.fido2) {
// TODO: I did this for typescript and I can't use ! // TODO: I did this for typescript and I can't use !

View File

@ -1,6 +1,15 @@
import { UserSettings } from "../../entities"; import { BackupCode, UserSettings } from "../../entities";
export interface TokenResponse { export interface TokenResponse {
token: string; token: string;
settings: UserSettings; settings: UserSettings;
} }
export interface TokenOnlyResponse {
token: string;
}
export interface TokenWithBackupCodesResponse {
token: string;
backup_codes: BackupCode[];
}

View File

@ -1,5 +1,5 @@
import { DmChannelDTO } from "../../dtos"; import { DmChannelDTO } from "../../dtos";
import { Guild, PrivateUser, PublicUser, User } from "../../entities"; import { BackupCode, Guild, PrivateUser, PublicUser } from "../../entities";
export type PublicUserResponse = PublicUser; export type PublicUserResponse = PublicUser;
export type PrivateUserResponse = PrivateUser; export type PrivateUserResponse = PrivateUser;
@ -12,11 +12,4 @@ export type UserGuildsResponse = Guild[];
export type UserChannelsResponse = DmChannelDTO[]; export type UserChannelsResponse = DmChannelDTO[];
export interface UserBackupCodesResponse { export type UserBackupCodesResponse = BackupCode[];
expired: unknown;
user: User;
code: string;
consumed: boolean;
id: string;
}
[];

View File

@ -0,0 +1,4 @@
export interface WebAuthnCreateResponse {
name: string;
id: string;
}

View File

@ -44,4 +44,5 @@ export * from "./UserProfileResponse";
export * from "./UserRelationshipsResponse"; export * from "./UserRelationshipsResponse";
export * from "./UserRelationsResponse"; export * from "./UserRelationsResponse";
export * from "./UserResponse"; export * from "./UserResponse";
export * from "./WebAuthnCreateResponse";
export * from "./WebhookCreateResponse"; export * from "./WebhookCreateResponse";