|
| 1 | +/* |
| 2 | + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. |
| 3 | + Copyright (C) 2023 Spacebar and Spacebar Contributors |
| 4 | + |
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU Affero General Public License as published |
| 7 | + by the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU Affero General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU Affero General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +import { route } from "@spacebar/api"; |
| 20 | +import { |
| 21 | + EmailDomainLookupResponse, |
| 22 | + EmailDomainLookupSchema, |
| 23 | + EmailDomainLookupVerifyCodeSchema, |
| 24 | + FieldErrors, |
| 25 | +} from "@spacebar/util"; |
| 26 | +import emailProviders from "email-providers/all.json"; |
| 27 | +import { Request, Response, Router } from "express"; |
| 28 | +import { HTTPError } from "lambert-server"; |
| 29 | + |
| 30 | +const router = Router(); |
| 31 | + |
| 32 | +router.post( |
| 33 | + "/", |
| 34 | + route({ |
| 35 | + requestBody: "EmailDomainLookupSchema", |
| 36 | + responses: { |
| 37 | + 200: { |
| 38 | + body: "EmailDomainLookupResponse", |
| 39 | + }, |
| 40 | + 400: { |
| 41 | + body: "APIErrorResponse", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }), |
| 45 | + async (req: Request, res: Response) => { |
| 46 | + const { email } = req.body as EmailDomainLookupSchema; |
| 47 | + |
| 48 | + const [_, tld] = email.split("@"); |
| 49 | + |
| 50 | + if (emailProviders.includes(tld.toLowerCase())) { |
| 51 | + throw FieldErrors({ |
| 52 | + name: { |
| 53 | + message: |
| 54 | + "That looks like a personal email address. Please use your official student email.", |
| 55 | + code: "EMAIL_IS_UNOFFICIAL", |
| 56 | + }, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + return res.json({ |
| 61 | + guilds_info: [], |
| 62 | + has_matching_guild: false, |
| 63 | + } as EmailDomainLookupResponse); |
| 64 | + }, |
| 65 | +); |
| 66 | + |
| 67 | +router.post( |
| 68 | + "/verify-code", |
| 69 | + route({ |
| 70 | + requestBody: "EmailDomainLookupVerifyCodeSchema", |
| 71 | + responses: { |
| 72 | + // 200: { |
| 73 | + // body: "EmailDomainLookupVerifyCodeResponse", |
| 74 | + // }, |
| 75 | + 400: { |
| 76 | + body: "APIErrorResponse", |
| 77 | + }, |
| 78 | + 501: {}, |
| 79 | + }, |
| 80 | + }), |
| 81 | + async (req: Request, res: Response) => { |
| 82 | + const { email } = req.body as EmailDomainLookupVerifyCodeSchema; |
| 83 | + |
| 84 | + const [_, tld] = email.split("@"); |
| 85 | + |
| 86 | + if (emailProviders.includes(tld.toLowerCase())) { |
| 87 | + throw FieldErrors({ |
| 88 | + name: { |
| 89 | + message: |
| 90 | + "That looks like a personal email address. Please use your official student email.", |
| 91 | + code: "EMAIL_IS_UNOFFICIAL", |
| 92 | + }, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + throw new HTTPError("Not implemented", 501); |
| 97 | + |
| 98 | + // return res.json({ |
| 99 | + // guild: null, |
| 100 | + // joined: false, |
| 101 | + // } as EmailDomainLookupVerifyCodeResponse); |
| 102 | + }, |
| 103 | +); |
| 104 | + |
| 105 | +export default router; |
0 commit comments