add me route to get user info
This commit is contained in:
parent
659dc79c69
commit
ba0d353588
|
|
@ -1,10 +1,12 @@
|
||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import posts from './posts';
|
import posts from './posts';
|
||||||
import auth from './auth';
|
import auth from './auth';
|
||||||
|
import me from './me';
|
||||||
|
|
||||||
const api = Router();
|
const api = Router();
|
||||||
|
|
||||||
api.use('/posts', posts);
|
api.use('/posts', posts);
|
||||||
api.use('/auth', auth);
|
api.use('/auth', auth);
|
||||||
|
api.use('/me', me);
|
||||||
|
|
||||||
export default api;
|
export default api;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { verifyToken } from "@/controller/AuthController";
|
||||||
|
import { getUserById } from "@/controller/UserController";
|
||||||
|
import { NextFunction, Request, Response, Router } from "express";
|
||||||
|
|
||||||
|
const getToken = (req: Request): string | undefined => {
|
||||||
|
const token: string | undefined = req.headers.authorization?.substring(7); // remove 'Bearer ' from token
|
||||||
|
return token;
|
||||||
|
};
|
||||||
|
|
||||||
|
const checkAuth = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const token = getToken(req);
|
||||||
|
if (token === undefined) {
|
||||||
|
return res.status(401).send({ error: 'No token provided' });
|
||||||
|
}
|
||||||
|
return verifyToken(token)
|
||||||
|
.then((decodedToken: number) => {
|
||||||
|
req.userId = decodedToken;
|
||||||
|
next();
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return res.status(401).send({ error });
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const me = Router();
|
||||||
|
|
||||||
|
me.use(checkAuth);
|
||||||
|
|
||||||
|
me.get("/", async (req: Request, res: Response) => {
|
||||||
|
const user = await getUserById(req.userId);
|
||||||
|
return res.status(200).send(user);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default me;
|
||||||
|
|
@ -35,9 +35,7 @@ const genToken = (userId: number) => {
|
||||||
const verifyToken = (token: string): Promise<number> => {
|
const verifyToken = (token: string): Promise<number> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jwt.verify(token, config.JWT_SECRET, async (err?, decoded?: jwt.JwtPayload | string) => {
|
jwt.verify(token, config.JWT_SECRET, async (err?, decoded?: jwt.JwtPayload | string) => {
|
||||||
if (err) {
|
if (err || decoded === undefined || typeof decoded === 'string' || decoded.id === undefined) {
|
||||||
reject('Unkown error');
|
|
||||||
} else if (decoded === undefined || typeof decoded === 'string' || decoded.id === undefined) {
|
|
||||||
reject('Invalid token');
|
reject('Invalid token');
|
||||||
} else {
|
} else {
|
||||||
const prismaToken = await prisma.token.findUnique({
|
const prismaToken = await prisma.token.findUnique({
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,9 @@
|
||||||
import { PrismaClient, Post as PrismaPost, Like } from '@prisma/client';
|
import { PrismaClient, Post as PrismaPost, Like } from '@prisma/client';
|
||||||
import { Post } from '@/models/PostModel';
|
import { Post } from '@/models/PostModel';
|
||||||
|
import { exclude } from '@/lib/utils';
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
const exclude = <User, Key extends keyof User>(user: User, ...keys: Key[]): User => {
|
|
||||||
for (let key of keys) {
|
|
||||||
delete user[key];
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getPostById = async (id: number): Promise<PrismaPost | null> => {
|
const getPostById = async (id: number): Promise<PrismaPost | null> => {
|
||||||
const post = await prisma.post.findUnique({
|
const post = await prisma.post.findUnique({
|
||||||
where: {
|
where: {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import { User } from '@/models/UserModel';
|
import { User } from '@/models/UserModel';
|
||||||
|
import { exclude } from '@/lib/utils';
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
|
@ -13,6 +14,18 @@ const getUser = (email: string) => {
|
||||||
return user;
|
return user;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getUserById = async (id: number) => {
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
return exclude(user, 'password');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const isUserExist = (email: string) =>
|
const isUserExist = (email: string) =>
|
||||||
prisma.user
|
prisma.user
|
||||||
.findUnique({
|
.findUnique({
|
||||||
|
|
@ -48,4 +61,4 @@ const newUser = async (user: User) => {
|
||||||
return newUser;
|
return newUser;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { getUser, newUser };
|
export { getUser, newUser, isUserExist, getUserById };
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
const exclude = <PrismaObject, Key extends keyof PrismaObject>(pobj: PrismaObject, ...keys: Key[]): PrismaObject => {
|
||||||
|
for (let key of keys) {
|
||||||
|
delete pobj[key];
|
||||||
|
}
|
||||||
|
return pobj;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { exclude };
|
||||||
Loading…
Reference in New Issue