Skip to content

Instantly share code, notes, and snippets.

View yvki's full-sized avatar
:bowtie:
#今天珉天再加叶班#

v yvki

:bowtie:
#今天珉天再加叶班#
View GitHub Profile
@yvki
yvki / JWTVerifier.js
Created January 4, 2024 09:34
JavaScript implementation for verification of JSON Web Token (JWT) ✅
// 1. create a backend file with middleware function to check if user is logged in within 3 steps, note that the following snippet is a sample function asserting that a given token not only follows a proper JWT format, but also it's signature matches during the encoding and decoding process, so as to grant actions entitled to a privileged user
// 1.1 import modules
const jwt = require("jsonwebtoken");
const JWT_SECRET = process.env.JWT_SECRET;
// 1.2 declare token verification function
function verifyToken(req, res, next) {
const authHeader = req.headers.authorization;
// 1.2.1 check if the header contains the authorization information
if (authHeader === null || authHeader === undefined || !authHeader.startsWith("Bearer ")) {
@yvki
yvki / index.js
Created January 2, 2024 07:21
The Ultimate Quiz 📄on Chemistry 🧪, Computing ⚙️, Geography 🗺️, and Physics 🪂 for Command-Line Interfaces (CLIs) 🖥️
//create a class to modal MCQ which takes in question, choice, answer and category as its parameter
class MCQ {
constructor(question, choice, answer, category) {
this.question = question;
this.choice = choice;
this.answer = answer;
this.category = category;
this.userAnswer = "";
}
//write a method to display question and choices format