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
public class Student
{
public string Name {get; set;}
}
@saman-ghm
saman-ghm / server.ts
Created October 11, 2019 19:23
Refresh token using Node
import express from "express";
const app = express();
app.get('/', (req, res) => {
res.send("Hello world!");
});
app.listen(3000, () => {
console.log(`Server is running in http://localhost:3000`);
import { Request, Response } from "express";
import * as jwt from "jsonwebtoken";
import { random } from "../utils/index";
import config from "../config/index";
class AuthController {
public static refTokens = new Array<{ username: string, refreshToken: string }>();
private static issueToken = async (username: string) => {
const userToken = {
username
import { Request, Response } from "express";
class UserController {
static check = async(req:Request,res:Response) => {
res.send("You're authorized!");
}
}
export default UserController;
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);
@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 { 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;
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;
export default {
jwtSecret: "ThisIsMySecret",
jwtExpirationSeconds: 300
};
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;
};