Skip to content

Instantly share code, notes, and snippets.

Avatar

Rony Chandra Kudus blogcacanid

  • cacan.id
  • Jakarta
View GitHub Profile
@blogcacanid
blogcacanid / server.js
Created Nov 12, 2020
server.js Authentication JWT Node.js
View server.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
// CORS multiple Domain
var allowlist = [
'http://localhost:3000',
'http://localhost:4200',
@blogcacanid
blogcacanid / user.routes.js
Created Nov 12, 2020
user.routes.js Authentication JWT Node.js
View user.routes.js
const { authJwt } = require("../middleware");
const controller = require("../controllers/user.controller");
module.exports = function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
@blogcacanid
blogcacanid / auth.routes.js
Created Nov 12, 2020
auth.routes.js Authentication JWT Node.js
View auth.routes.js
const { verifyRegister } = require("../middleware");
const controller = require("../controllers/auth.controller");
module.exports = function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
@blogcacanid
blogcacanid / index.js
Created Nov 12, 2020
index.js Authentication JWT Node.js
View index.js
const authJwt = require("./authJwt");
const verifyRegister = require("./verifyRegister");
module.exports = {
authJwt,
verifyRegister
};
@blogcacanid
blogcacanid / authJwt.js
Created Nov 12, 2020
authJwt.js Authentication JWT Node.js
View authJwt.js
const jwt = require("jsonwebtoken");
const config = require("../config/auth.config.js");
const db = require("../models");
const User = db.user;
verifyToken = (req, res, next) => {
let token = req.headers["x-access-token"];
if (!token) {
return res.status(403).send({
@blogcacanid
blogcacanid / verifyRegister.js
Created Nov 12, 2020
verifyRegister.js Authentication JWT Node.js
View verifyRegister.js
const db = require("../models");
const User = db.user;
checkDuplicateUsernameOrEmail = (req, res, next) => {
// Username
User.findOne({
where: {
username: req.body.username
}
}).then(user => {
@blogcacanid
blogcacanid / user.controller.js
Created Nov 12, 2020
user.controller.js Authentication JWT Node.js
View user.controller.js
exports.allAccess = (req, res) => {
res.status(200).send("Authentication with JSON Web Token (JWT)");
};
@blogcacanid
blogcacanid / auth.controller.js
Created Nov 12, 2020
auth.controller.js Authentication JWT Node.js
View auth.controller.js
const db = require("../models");
const config = require("../config/auth.config");
const User = db.user;
var jwt = require("jsonwebtoken");
var bcrypt = require("bcryptjs");
exports.register = (req, res) => {
// Save User to Database
User.create({
@blogcacanid
blogcacanid / index.js
Created Nov 12, 2020
index.js Authentication JWT Node.js
View index.js
const config = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
config.DB,
config.USER,
config.PASSWORD,
{
host: config.HOST,
@blogcacanid
blogcacanid / user.model.js
Created Nov 12, 2020
user.model.js Authentication JWT Node.js
View user.model.js
module.exports = (sequelize, Sequelize) => {
const User = sequelize.define("users", {
username: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
You can’t perform that action at this time.