File size: 4,284 Bytes
ece5841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require("express");
const {
    createJSONToken,
    isValidPassword,
    validateJSONToken,
    generatePasswordHash,
    checkAuth,
} = require("../util/auth");
const { isValidEmail, isValidText } = require("../util/validation");
const { User } = require("../models/user");
const { z, ZodError } = require("zod");

const router = express.Router();

const loginSchema = z
    .object({
        username: z
            .string()
            .min(5, { message: "Username should not be less than 5 characters" })
            .max(15, { message: "Username should not be more than 15 characters" }),
        password: z
            .string()
            .min(8, { message: "Password should not be less than 8 characters" })
            .max(15, { message: "Password should not be more than 15 characters" }),
    })
    .required();

const signupSchema = z
    .object({
        username: z
            .string()
            .min(5, { message: "Username should not be less than 5 characters" })
            .max(15, { message: "Username should not be more than 15 characters" }),
        email: z.string().email({ message: "Please enter a valid email address" }),
        password: z
            .string()
            .min(8, { message: "Password should not be less than 8 characters" })
            .max(15, { message: "Password should not be more than 15 characters" }),
    })
    .required();

router.post("/signup", async (req, res, next) => {
    try {
        const data = { email: req.body.email, password: req.body.password, username: req.body.username };

        signupSchema.parse(data);

        // check if username or email already exists
        if (await User.findOne({ username: data.username }))
            return res.status(409).json({ error: { username: "username already taken" } });
        if (await User.findOne({ email: data.email }))
            return res.status(409).json({ error: { email: "user with this email already exists" } });

        let userData = {
            email: data.email,
            username: data.username,
            password_hash: await generatePasswordHash(data.password),
        };
        let userDoc = await User.create(userData);
        const authToken = createJSONToken(userDoc.id);

        const { id, username, email } = userDoc;
        res.setHeader('Host',process.env.HOSTNAME).status(201).cookie("auth-token", authToken, { httpOnly: true, sameSite: "strict" }).json({
            user: { id, username, email },
            token: authToken,
        });
    } catch (err) {
        if (err instanceof ZodError) {
            return res.status(400).json({ message: "Invalid data submitted", description: "Invalid schema" });
        }
        next(err);
    }
});

router.post("/login", async (req, res, next) => {
    try {
        let username = req.body.username,
            password = req.body.password;

        loginSchema.parse({ username, password });

        let user;
        user = await User.findOne({ username });
        if (!user)
            return res.status(404).json({
                message: "User does not exist",
                description: "'username' not found in db",
            });

        const pwIsValid = await isValidPassword(password, user.password_hash);
        if (!pwIsValid) {
            return res.status(401).json({
                message: "Invalid credentials",
                description: "Invalid credentials",
            });
        }

        const token = createJSONToken(user.id);
        res.cookie("auth-token", token, { httpOnly: true, sameSite: "strict" });
        return res.setHeader('Host',process.env.HOSTNAME)
            .status(200)
            .json({ token, user: { id: user.id, username: user.username, email: user.email } });
    } catch (error) {
        if (error instanceof ZodError) {
            return res.status(401).json({ message: "Invalid Credentials", description: "Invalid schema" });
        }
        next(error);
    }
});

router.delete("/logout", checkAuth, (req, res, next) => {
    try {
        res.setHeader('Host',process.env.HOSTNAME).clearCookie("auth-token", { httpOnly: true, sameSite: "strict" });
        res.status(200).json({});
    } catch (err) {
        next(err);
    }
});

module.exports = router;