Merge pull request 'feat/logout' (#15) from feat/logout into main
All checks were successful
Sync to GitHub / sync (push) Successful in 9s
All checks were successful
Sync to GitHub / sync (push) Successful in 9s
Reviewed-on: #15
This commit is contained in:
0
src/helpers/http/jwt/decode/example.json
Normal file
0
src/helpers/http/jwt/decode/example.json
Normal file
@ -1,15 +1,14 @@
|
|||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { JWTSessionPayload } from "../../../../modules/auth/auth.types";
|
import { JWTAuthToken } from "./types";
|
||||||
import { AppError } from "../../../error/instances/app";
|
import { AppError } from "../../../error/instances/app";
|
||||||
|
|
||||||
export const jwtDecode = (payload: string) => {
|
export const jwtDecode = (payload: string) => {
|
||||||
// return payload;
|
|
||||||
if (!payload) throw new AppError(401, "Unauthorized");
|
if (!payload) throw new AppError(401, "Unauthorized");
|
||||||
const JWTKey = process.env.JWT_SECRET!;
|
const JWTKey = process.env.JWT_SECRET!;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const decodedPayload = jwt.verify(payload, JWTKey);
|
const decodedPayload = jwt.verify(payload, JWTKey);
|
||||||
return decodedPayload as JWTSessionPayload;
|
return decodedPayload as JWTAuthToken;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new AppError(401, "Invalid or expired token", error);
|
throw new AppError(401, "Invalid or expired token", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,7 @@
|
|||||||
export interface JWTAuthToken {
|
export interface JWTAuthToken {
|
||||||
id: string;
|
id: string;
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
userId: string;
|
|
||||||
deviceType: string;
|
|
||||||
deviceOs: string;
|
|
||||||
deviceIp: string;
|
|
||||||
isOnline: boolean;
|
|
||||||
lastOnline: Date;
|
|
||||||
validUntil: Date;
|
validUntil: Date;
|
||||||
deletedAt: null;
|
|
||||||
createdAt: Date;
|
|
||||||
updatedAt: Date;
|
|
||||||
user: User;
|
user: User;
|
||||||
iat: number;
|
iat: number;
|
||||||
exp: number;
|
exp: number;
|
||||||
@ -19,44 +10,21 @@ export interface JWTAuthToken {
|
|||||||
interface User {
|
interface User {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
username: string;
|
|
||||||
email: string;
|
email: string;
|
||||||
|
username: string;
|
||||||
|
avatar: string;
|
||||||
birthDate: null;
|
birthDate: null;
|
||||||
gender: null;
|
|
||||||
phoneCC: null;
|
|
||||||
phoneNumber: null;
|
|
||||||
bioProfile: null;
|
bioProfile: null;
|
||||||
profilePicture: null;
|
preference: Preference;
|
||||||
commentPicture: null;
|
|
||||||
preferenceId: null;
|
|
||||||
verifiedAt: null;
|
|
||||||
disabledAt: null;
|
|
||||||
deletedAt: null;
|
|
||||||
createdAt: Date;
|
|
||||||
updatedAt: Date;
|
|
||||||
roles: Role[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Role {
|
interface Preference {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
userId: string;
|
||||||
primaryColor: string;
|
langPreference: null;
|
||||||
secondaryColor: string;
|
adultFiltering: string;
|
||||||
pictureImage: string;
|
adultAlert: string;
|
||||||
badgeImage: null;
|
videoQuality: string;
|
||||||
isSuperadmin: boolean;
|
serviceDefaultId: null;
|
||||||
canEditMedia: boolean;
|
hideContries: any[];
|
||||||
canManageMedia: boolean;
|
|
||||||
canEditEpisodes: boolean;
|
|
||||||
canManageEpisodes: boolean;
|
|
||||||
canEditComment: boolean;
|
|
||||||
canManageComment: boolean;
|
|
||||||
canEditUser: boolean;
|
|
||||||
canManageUser: boolean;
|
|
||||||
canEditSystem: boolean;
|
|
||||||
canManageSystem: boolean;
|
|
||||||
createdBy: string;
|
|
||||||
deletedAt: null;
|
|
||||||
createdAt: Date;
|
|
||||||
updatedAt: Date;
|
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/modules/auth/controllers/logout.controller.ts
Normal file
19
src/modules/auth/controllers/logout.controller.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
import { logoutService } from "../services/http/logout.service";
|
||||||
|
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const logoutController = async (ctx: Context) => {
|
||||||
|
try {
|
||||||
|
const jwtToken = ctx.cookie.auth_token?.value;
|
||||||
|
const serviceResponse = await logoutService(jwtToken);
|
||||||
|
return returnWriteResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"Logout successful",
|
||||||
|
serviceResponse,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -6,6 +6,7 @@ import { googleCallbackController } from "./controllers/googleCallback.controlle
|
|||||||
import { getOauthProvidersController } from "./controllers/getOauthProviders.controller";
|
import { getOauthProvidersController } from "./controllers/getOauthProviders.controller";
|
||||||
import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller";
|
import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller";
|
||||||
import { tokenValidationController } from "./controllers/tokenValidation.controller";
|
import { tokenValidationController } from "./controllers/tokenValidation.controller";
|
||||||
|
import { logoutController } from "./controllers/logout.controller";
|
||||||
|
|
||||||
export const authModule = new Elysia({ prefix: "/auth" })
|
export const authModule = new Elysia({ prefix: "/auth" })
|
||||||
.post("/token/validate", tokenValidationController)
|
.post("/token/validate", tokenValidationController)
|
||||||
@ -14,4 +15,5 @@ export const authModule = new Elysia({ prefix: "/auth" })
|
|||||||
.get("/github", githubRequestController)
|
.get("/github", githubRequestController)
|
||||||
.get("/github/callback", githubCallbackController)
|
.get("/github/callback", githubCallbackController)
|
||||||
.get("/google", googleRequestController)
|
.get("/google", googleRequestController)
|
||||||
.get("/google/callback", googleCallbackController);
|
.get("/google/callback", googleCallbackController)
|
||||||
|
.post("/logout", logoutController);
|
||||||
|
|||||||
21
src/modules/auth/services/http/logout.service.ts
Normal file
21
src/modules/auth/services/http/logout.service.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||||
|
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
||||||
|
import { redis } from "../../../../utils/databases/redis/connection";
|
||||||
|
import { deleteUserSessionRepository } from "../../../userSession/repositories/deleteUserSession.repository";
|
||||||
|
|
||||||
|
export const logoutService = async (jwtToken?: any) => {
|
||||||
|
try {
|
||||||
|
if (!jwtToken) throw new AppError(403, "No auth token provided");
|
||||||
|
|
||||||
|
const jwtPayload = jwtDecode(jwtToken);
|
||||||
|
|
||||||
|
await redis.del(
|
||||||
|
`${process.env.APP_NAME}:users:${jwtPayload.user.id}:sessions:${jwtPayload.id}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return deleteUserSessionRepository(jwtPayload.id);
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const deleteUserSessionRepository = async (sessionId: string) => {
|
||||||
|
try {
|
||||||
|
return await prisma.userSession.update({
|
||||||
|
where: {
|
||||||
|
id: sessionId,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
isAuthenticated: false,
|
||||||
|
deletedAt: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Failed to delete user session", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user