当前位置:网站首页>Generating access keys using JSON webtoken
Generating access keys using JSON webtoken
2022-04-23 17:15:00 【MiMenge】
Back end
const {
response } = require('express');
const express = require('express');
const fs = require('fs');
const jwt = require('jsonwebtoken');
const app = express();
app.use(express.urlencoded({
extended: false }));
app.use(express.json());
app.use('*', (request, response, next) => {
// // Gets the of the header token
// let token = request.headers.authorization;
// // check token
// jwt.verify(token, 'MiMenge001018', (err, data) => {
// if (err && err.message === 'invalid token') {
// return response.json({ message: ' Invalid token', code: 0 });
// } else if (err && err.message === 'jwt expired') {
// return response.json({ message: ' invalid token', code: 0 })
// }
// });
next();
});
app.get('/', (request, response) => {
fs.readFile('./public/index.html', (err, data) => {
if (err) {
response.json({
message: ' Page resource not found ', code: 0 });
} else {
response.send(data.toString());
}
});
});
app.get('/demo', (request, response) => {
fs.readFile('./public/demo.html', (err, data) => {
if (err) {
response.json({
message: ' Page resource not found ', code: 0 });
} else {
response.send(data.toString());
}
});
});
app.post('/login', (request, response) => {
let {
uname, password } = request.body;
if (uname === 'Tom' && password === '1234') {
let token = jwt.sign({
uname: 'MiMenge',
iSRoot: true
}, 'MiMenge001018', {
expiresIn: 60
});
response.json({
message: 'OK', code: 0, token, });
}
});
app.post('/data', (request, response) => {
let token = request.headers.authorization;
jwt.verify(token, 'MiMenge001018', (err, data) => {
if (err) {
if (err.message === 'invalid token') {
return response.json({
message: ' Invalid token', code: 0 });
} else {
return response.json({
message: ' invalid token', code: 0 })
}
} else {
response.json({
message: ' success ', code: 1 });
}
})
});
app.listen(3300, err => {
if (err) {
console.error(err);
return;
}
console.log('success');
});
front end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href=""> obtain </a>
<script> let a = document.querySelector('a'); a.addEventListener('click', (e) => {
e.preventDefault(); let token = localStorage.getItem('token'); let headers = new Headers({
'Authorization': token }); fetch('http://localhost:3300/data', {
method: 'post', headers }).then(data => data.text()).then(value => console.log(value)); // // Create request object // let xml = new XMLHttpRequest(); // // Initialization request // xml.open('post', 'http://localhost:3300/data', true); // // Set request header // xml.setRequestHeader('Content-Type', 'application/json;charset=utf8'); // // console.log(JSON.stringify(new AddO(uname, pwd))); // // Send request data // xml.send(JSON.stringify({ 'token': token })); // // Set request delay // xml.timeout = 3000; // xml.addEventListener('timeout', () => {
// xml.abort(); // }); // xml.addEventListener('error', () => {
// alert(' Wrong request ') // }); // xml.addEventListener('readystatechange', () => {
// if (xml.readyState === 4) {
// if (xml.status >= 200 && xml.status < 300 || xml.status === 304) {
// console.log(xml.responseText); // // console.log(xml.responseURL); // // console.log(xml.status); // // console.log(xml.statusText); // } // } // }); }); </script>
</body>
</html>
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553027505.html
边栏推荐
- Collection of common SQL statements
- Read a blog, re understand closures and tidy up
- Promise (III)
- Solution architect's small bag - 5 types of architecture diagrams
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- 基于51单片机红外无线通讯仿真
- El cascade and El select click elsewhere to make the drop-down box disappear
- Grpc gateway based on Ocelot
- Shell - introduction, variables, and basic syntax
- 手写事件发布订阅框架
猜你喜欢
随机推荐
Detailed explanation of Milvus 2.0 quality assurance system
groutine
Website_ Collection
ClickHouse-表引擎
websocket
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Baidu Map 3D rotation and tilt angle adjustment
Abnormal resolution of Xiaomi camera
Further study of data visualization
Interface document yaml
Shell-sed命令的使用
freeCodeCamp----shape_ Calculator exercise
Further optimize Baidu map data visualization
Preliminary understanding of promse
Tencent resolves the address according to the IP address
Grpc gateway based on Ocelot
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Model problems of stock in and stock out and inventory system
01-初识sketch-sketch优势
Solution of Navicat connecting Oracle library is not loaded








