Skip to content

Instantly share code, notes, and snippets.

View saman-ghm's full-sized avatar
☺️
Relaxed!

Saman Gholami saman-ghm

☺️
Relaxed!
View GitHub Profile
import { Router, Response, Request, NextFunction } from "express";
import express from "express";
import bodyParser from "body-parser";
import { validate, Matches, IsDefined } from "class-validator";
import { plainToClass, Expose } from "class-transformer";
class UserDto {
@IsDefined()
@Expose()
username: String;
import { Router, Response, Request } from "express";
import express from "express";
import bodyParser from "body-parser";
import { validate, Matches, IsDefined } from "class-validator";
import { plainToClass, Expose } from "class-transformer";
class UserDto {
@IsDefined()
@Expose()
username: String;
import { Router, Response, Request } from "express";
import express from "express";
import bodyParser from "body-parser";
import { validate, Matches, IsDefined } from "class-validator";
import { plainToClass, Expose } from "class-transformer";
class UserDto {
@IsDefined()
@Expose()
username: String;
@saman-ghm
saman-ghm / validation-index.ts
Last active January 30, 2020 19:11
validation entry
import { Router, Response, Request } from "express";
import express from "express";
import bodyParser from "body-parser";
class UserDto {
username: String;
password: String;
email: String;
}
export const random = (length: number): string => {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
export default {
jwtSecret: "ThisIsMySecret",
jwtExpirationSeconds: 300
};
import { Router } from "express";
import auth from "./auth";
import user from "./user";
const routes = Router();
routes.use("/auth", auth);
routes.use("/user", user);
export default routes;
import { Router } from "express";
import { checkAuthToken } from "../middlewares/checkAuthToken";
import UserController from "../controllers/UserController"
const router = Router();
router.get("/check", [ checkAuthToken ], UserController.check);
export default router;
@saman-ghm
saman-ghm / auth.ts
Last active October 21, 2019 20:25
import { Router } from "express";
import AuthController from "../controllers/AuthController";
const router = Router();
router.post("/refreshToken", AuthController.refreshToken);
router.post("/login", AuthController.login);
export default router;
import { Request, Response, NextFunction } from "express";
import * as jwt from "jsonwebtoken";
import config from "../config";
export const checkAuthToken = function(req: Request, res: Response, next: NextFunction) {
const token = <string>req.headers.authorization;
let jwtPayload;
try {
// check if access token is valid
jwtPayload = <any>jwt.verify(token, config.jwtSecret);