Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- var bodyParser = require('body-parser');
- var jwt = require('jsonwebtoken');
- var passport = require('passport');
- var passportJWT = require('passport-jwt');
- var ExtractJwt = passportJWT.ExtractJwt;
- var JwtStrategy = passportJWT.Strategy;
- var jwtOptions = {};
- jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
- jwtOptions.secretOrKey = 'wowwow';
- // lets create our strategy for web token
- var strategy = new JwtStrategy(jwtOptions, function(jwt_payload, next) {
- console.log('payload received', jwt_payload);
- var user = getUser({ id: jwt_payload.id });
- if (user) {
- next(null, user);
- } else {
- next(null, false);
- }
- });
- // use the strategy
- passport.use(strategy);
- const app = express();
- // initialize passport with express
- app.use(passport.initialize());
- // parse application/json
- app.use(bodyParser.json());
- //parse application/x-www-form-urlencoded
- app.use(bodyParser.urlencoded({ extended: true }));
- const Sequelize = require('sequelize');
- // initialze an instance of Sequelize
- const sequelize = new Sequelize({
- database: 'test_sequelize',
- username: 'root',
- password: '',
- dialect: 'mysql',
- });
- // check the databse connection
- sequelize
- .authenticate()
- .then(() => console.log('Connection has been established successfully.'))
- .catch(err => console.error('Unable to connect to the database:', err));
- // create user model
- const User = sequelize.define('user', {
- name: {
- type: Sequelize.STRING,
- },
- password: {
- type: Sequelize.STRING,
- },
- });
- // create table with user model
- User.sync()
- .then(() => console.log('User table created successfully'))
- .catch(err => console.log('oooh, did you enter wrong database credentials?'));
- // create some helper functions to work on the database
- const createUser = async ({ name, password }) => {
- return await User.create({ name, password });
- };
- const getAllUsers = async () => {
- return await User.findAll();
- };
- const getUser = async obj => {
- return await User.findOne({
- where: obj,
- });
- };
- // set some basic routes
- app.get('/', function(req, res) {
- res.json({ message: 'Express is up!' });
- });
- // get all users
- app.get('/users', function(req, res) {
- getAllUsers().then(user => res.json(user));
- });
- // register route
- app.post('/register', function(req, res, next) {
- const { name, password } = req.body;
- createUser({ name, password }).then(user =>
- res.json({ user, msg: 'account created successfully' })
- );
- });
- //login route
- app.post('/login', async function(req, res, next) {
- const { name, password } = req.body;
- if (name && password) {
- var user = await getUser({ name: name });
- if (!user) {
- res.status(401).json({ message: 'No such user found' });
- }
- if (user.password === password) {
- // from now on we'll identify the user by the id and the id is the
- // only personalized value that goes into our token
- var payload = { id: user.id };
- var token = jwt.sign(payload, jwtOptions.secretOrKey);
- res.json({ msg: 'ok', token: token });
- } else {
- res.status(401).json({ msg: 'Password is incorrect' });
- }
- }
- });
- // protected route
- app.get('/protected', passport.authenticate('jwt', { session: false }), function(req, res) {
- res.json('Success! You can now see this without a token.');
- });
- // start app
- app.listen(3000, function() {
- console.log('Express is running on port 3000');
- });
Add Comment
Please, Sign In to add comment