basic-auth的使用
basic-auth 會幫我們解析
http header
的
authorization
内的值,這個值通常是使用
base64
加密的。
導入
解析header中authorization的值
在 postman 中這樣送出就可以被解析到
手動解析authorization
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
let auth = ctx.request.header.authorization; //http header的值
auth = auth.split(' ')[1]; //有"basic "的字首,用split分割空格取值
auth = Buffer.from(auth, 'base64').toString().split(':')[0]; //解析base64,轉化為字元串,而且他有一個“:”的符号,需要分割
console.log(auth); //結果
})
app.listen(3000);