Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const config = require('config');
- const express = require('express');
- const bodyParser = require('body-parser');
- const path = require('path');
- const app = express();
- const jwt = require('jsonwebtoken');
- const port = 3333;
- app.use(bodyParser.urlencoded({extended: false}));
- app.use(bodyParser.json());
- app.get('/', (req, res) => {
- res.sendFile(path.join(__dirname, 'webconfig', 'login.html'))
- });
- app.get('/config', verifyToken,(req, res)=> {
- console.log('GET /config data');
- res.sendFile(path.join(__dirname, 'webconfig', 'config.html'));
- });
- app.post('/login', (req, res) => {
- console.log('POST /login');
- const {body} = req;
- if (body.username === config.web.username && body.password === config.web.password) {
- const user = {
- id: 1,
- username: 'admin'
- };
- jwt.sign({user}, jwt_secret, {expiresIn: '3000s'}, (err, token) => {
- console.log('Token sent to client');
- res.send({
- token: token,
- });
- });
- } else {
- console.log('User pass mismatch');
- res.redirect('/')
- }
- });
- const username = document.getElementById('username');
- const password = document.getElementById('password');
- async function login() {
- axios.post('/login', {
- "username": username.value,
- "password": password.value
- })
- .then(function(res) {
- window.localStorage.setItem('token', res.data.token);
- let token = window.localStorage.getItem('token');
- if (token !== 'undefined'){
- axios.get('/login',{headers:{Authorization:'Bearer ' + token }})
- } else {
- window.location.href = '/'
- }
- })
- .catch(function(err) {
- console.log(err)
- });
- }
- }
Add Comment
Please, Sign In to add comment