Use Bearer token
This commit is contained in:
parent
55e19609e8
commit
303b3cf303
3 changed files with 38 additions and 3 deletions
28
backend/src/app/controllers/user/crud/get.ts
Normal file
28
backend/src/app/controllers/user/crud/get.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import EmailValidator from "email-validator";
|
||||
import { Request, Response } from "express";
|
||||
import Users from "../../../models/User";
|
||||
import { LOGGER } from "../../../util/Logger";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.user) {
|
||||
return res
|
||||
.status(422)
|
||||
.json({ error: "Cannot fetch the user since you are not logged in." });
|
||||
}
|
||||
|
||||
// If you're an admin it's assumed you're viewing someone else's account
|
||||
const userId = req.user.admin && req.body._id ? req.body._id : req.user._id;
|
||||
const user = await Users.findById(userId);
|
||||
if (!user) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: `User with id ${userId} not found` });
|
||||
}
|
||||
|
||||
return res.status(200).json(user);
|
||||
} catch (ex) {
|
||||
LOGGER.error(ex);
|
||||
return res.status(500).json({ error: ex });
|
||||
}
|
||||
};
|
||||
|
|
@ -1,11 +1,17 @@
|
|||
import { RouteCollection } from "..";
|
||||
import create from "./crud/create";
|
||||
import del from "./crud/delete";
|
||||
import forgot from "./auth/forgot";
|
||||
import login from "./auth/login";
|
||||
import validate from "./auth/validate";
|
||||
import create from "./crud/create";
|
||||
import del from "./crud/delete";
|
||||
import get from "./crud/get";
|
||||
|
||||
const routes: RouteCollection[] = [
|
||||
{
|
||||
httpMethod: "GET",
|
||||
path: "/api/v1/user",
|
||||
handler: get,
|
||||
},
|
||||
{
|
||||
httpMethod: "PUT",
|
||||
path: "/api/v1/user",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ import { LOGGER } from "../util/Logger";
|
|||
export default () => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const token = req.header("auth-token");
|
||||
const token = req.header("authorization").replace("Bearer ", "");
|
||||
|
||||
if (!token) {
|
||||
return next();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue