Added /user endpoint and made changes to navbar to fetch userdata
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
fbb0f60a23
commit
93c6985c7f
@ -0,0 +1,5 @@
|
||||
export interface User {
|
||||
first_name: string
|
||||
last_name: string
|
||||
email: string
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
<template>
|
||||
<div class="Orders">
|
||||
orders
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
export default {
|
||||
name: "UserOrdersView",
|
||||
methods: {
|
||||
async fetchOrders() {
|
||||
const res = await fetch(`${import.meta.env.VITE_BASE_API_URL}/orders`, {
|
||||
credentials: import.meta.env.DEV ? "include" : undefined
|
||||
});
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
this.$router.push({ path: "/login", query: { ref: this.$route.path } });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
@ -0,0 +1,38 @@
|
||||
import express, { Router, Request, Response } from "express";
|
||||
|
||||
import { client } from "../db";
|
||||
import { DatabaseError } from "pg";
|
||||
import { UserAuth } from "../middlewares/auth";
|
||||
import { AuthedRequest } from "../interfaces/auth";
|
||||
|
||||
const router: Router = express.Router();
|
||||
|
||||
interface UserObject {
|
||||
first_name: string
|
||||
last_name: string
|
||||
email: string
|
||||
isAdmin: boolean
|
||||
isTrainer: boolean
|
||||
}
|
||||
|
||||
router.get("/user", UserAuth, async (req: AuthedRequest, res: Response) => {
|
||||
try {
|
||||
const databaseResult = await client.query(`
|
||||
SELECT first_name, last_name, email, is_admin as isAdmin, trainers.user_id IS NOT NULL as isTrainer
|
||||
FROM users
|
||||
LEFT JOIN trainers ON trainers.user_id = users.id
|
||||
WHERE users.id = $1;
|
||||
`, [
|
||||
req.user?.userId
|
||||
]);
|
||||
|
||||
const user: UserObject = databaseResult.rows[0];
|
||||
|
||||
return res.status(200).send(user);
|
||||
} catch (error: DatabaseError | Error | any) {
|
||||
console.error(error);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
})
|
||||
|
||||
export default router;
|
||||
Loading…
Reference in New Issue