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
public class Student | |
{ | |
public string Name {get; set;} | |
} |
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
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`); |
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
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 |
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
import { Request, Response } from "express"; | |
class UserController { | |
static check = async(req:Request,res:Response) => { | |
res.send("You're authorized!"); | |
} | |
} | |
export default UserController; |
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
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); |
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
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; |
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
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; |
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
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; |
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
export default { | |
jwtSecret: "ThisIsMySecret", | |
jwtExpirationSeconds: 300 | |
}; |
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
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; | |
}; |
OlderNewer