Skip to content

Instantly share code, notes, and snippets.

View blogcacanid's full-sized avatar

Rony Chandra Kudus blogcacanid

  • cacan.id
  • Jakarta
View GitHub Profile
@blogcacanid
blogcacanid / server.js
Created November 12, 2020 07:49
server.js Authentication JWT Node.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 November 12, 2020 07:48
user.routes.js Authentication JWT Node.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 November 12, 2020 07:47
auth.routes.js Authentication JWT Node.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 November 12, 2020 07:46
index.js Authentication JWT Node.js
const authJwt = require("./authJwt");
const verifyRegister = require("./verifyRegister");
module.exports = {
authJwt,
verifyRegister
};
@blogcacanid
blogcacanid / authJwt.js
Created November 12, 2020 07:46
authJwt.js Authentication JWT Node.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 November 12, 2020 07:45
verifyRegister.js Authentication JWT Node.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 November 12, 2020 07:45
user.controller.js Authentication JWT Node.js
exports.allAccess = (req, res) => {
res.status(200).send("Authentication with JSON Web Token (JWT)");
};
@blogcacanid
blogcacanid / auth.controller.js
Created November 12, 2020 07:44
auth.controller.js Authentication JWT Node.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 November 12, 2020 07:44
index.js Authentication JWT Node.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 November 12, 2020 07:43
user.model.js Authentication JWT Node.js
module.exports = (sequelize, Sequelize) => {
const User = sequelize.define("users", {
username: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING