✨ feat: create token validation endpoint
This commit is contained in:
21
src/modules/auth/controllers/tokenValidation.controller.ts
Normal file
21
src/modules/auth/controllers/tokenValidation.controller.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { tokenValidationService } from "../services/http/tokenValidation.service";
|
||||||
|
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
|
||||||
|
export const tokenValidationController = (
|
||||||
|
ctx: Context & { body: { token: string } },
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const { token } = ctx.body;
|
||||||
|
const validationResult = tokenValidationService(token);
|
||||||
|
return returnReadResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"Validation successful",
|
||||||
|
validationResult,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -5,8 +5,10 @@ import { googleRequestController } from "./controllers/googleRequest.controller"
|
|||||||
import { googleCallbackController } from "./controllers/googleCallback.controller";
|
import { googleCallbackController } from "./controllers/googleCallback.controller";
|
||||||
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";
|
||||||
|
|
||||||
export const authModule = new Elysia({ prefix: "/auth" })
|
export const authModule = new Elysia({ prefix: "/auth" })
|
||||||
|
.post("/token/validate", tokenValidationController)
|
||||||
.get("/providers", getOauthProvidersController)
|
.get("/providers", getOauthProvidersController)
|
||||||
.get("/providers/:name/callback", getCallbackProviderUrlController)
|
.get("/providers/:name/callback", getCallbackProviderUrlController)
|
||||||
.get("/github", githubRequestController)
|
.get("/github", githubRequestController)
|
||||||
|
|||||||
12
src/modules/auth/services/http/tokenValidation.service.ts
Normal file
12
src/modules/auth/services/http/tokenValidation.service.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
||||||
|
import { jwtEncode } from "../../../../helpers/http/jwt/encode";
|
||||||
|
|
||||||
|
export const tokenValidationService = (payload: string) => {
|
||||||
|
try {
|
||||||
|
const decoded = jwtDecode(payload);
|
||||||
|
return decoded;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Token validation failed", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user