-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from TeamHSE/feat/logout
Feat/logout
- Loading branch information
Showing
8 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Request, Response } from "express"; | ||
import { verifyToken } from "../middlewares/verifyToken"; | ||
|
||
export const UserControllerLogout = [ | ||
verifyToken, | ||
(req: Request, res: Response) => { | ||
try { | ||
res.clearCookie("token", { httpOnly: true, maxAge: 0 }); | ||
return res | ||
.status(200) | ||
.json({ message: "Сессия завершена" }); | ||
} catch (error) { | ||
console.error("Ошибка при выходе из системы:", error); | ||
return res | ||
.status(500) | ||
.json({ message: "Произошла ошибка сервера при выходе из системы" }); | ||
} | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request, Response } from "express"; | ||
import { verifyToken } from "../middlewares/verifyToken"; | ||
import { AppDataSource } from "../db"; | ||
import { User } from "../entity/User"; | ||
|
||
export const UserControllerGetUserInfo = [ | ||
verifyToken, | ||
async (req: Request, res: Response) => { | ||
const login = req.user?.login; | ||
if (!login) { | ||
return res.status(403).json({ message: "Не удалось идентифицировать пользователя" }); | ||
} | ||
|
||
try { | ||
const userRepository = AppDataSource.getRepository(User); | ||
const user = await userRepository.findOne({ where: { email: login } }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "Пользователь не найден" }); | ||
} | ||
|
||
const { userId, firstName, lastName, username, email, isMale, birthDate, weight, height, achievements, healthIssues } = user; | ||
const userInfo = { | ||
userId, | ||
firstName, | ||
lastName, | ||
username, | ||
email, | ||
isMale, | ||
birthDate, | ||
weight, | ||
height, | ||
achievements, | ||
healthIssues, | ||
}; | ||
|
||
return res.status(200).json(userInfo); | ||
} catch (error) { | ||
console.error("Ошибка при получении информации о пользователе:", error); | ||
return res.status(500).json({ message: "Произошла ошибка сервера при получении информации о пользователе" }); | ||
} | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { | ||
BaseEntity, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import jwt from "jsonwebtoken"; | ||
import fs from "fs"; | ||
import { config } from "../config/Config"; | ||
import { DecodedToken } from "../types/express"; | ||
|
||
const publicKeyPath = config.publicKeyPath; | ||
|
||
export function verifyToken(req: Request, res: Response, next: NextFunction) { | ||
const token = req.headers.authorization?.split(" ")[1]; | ||
|
||
if (!token) { | ||
return res | ||
.status(403) | ||
.json({ message: "Необходимо предоставить токен авторизации" }); | ||
} | ||
|
||
try { | ||
const publicKey = fs.readFileSync(publicKeyPath, "utf8"); | ||
|
||
jwt.verify(token, publicKey, { algorithms: ["RS256"] }, (err, decoded) => { | ||
if (err) { | ||
console.error("Ошибка верификации токена:", err); | ||
return res.status(403).json({ message: "Ошибка авторизации" }); | ||
} | ||
|
||
const decodedToken = decoded as DecodedToken; | ||
req.user = decodedToken; | ||
next(); | ||
}); | ||
} catch (err) { | ||
console.error("Ошибка чтения публичного ключа:", err); | ||
return res | ||
.status(500) | ||
.json({ message: "Ошибка сервера при проверке токена" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Request } from 'express'; | ||
|
||
export interface DecodedToken { | ||
login: string; | ||
iat: number; | ||
exp: number; | ||
} | ||
|
||
// Расширение интерфейса Request для добавления свойства user | ||
declare global { | ||
namespace Express { | ||
interface Request { | ||
user?: DecodedToken; | ||
} | ||
} | ||
} |