在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ianstormtaylor/permit开源软件地址:https://github.com/ianstormtaylor/permit开源编程语言:JavaScript 100.0%开源软件介绍:
An unopinionated authentication library Usage • Why? • Principles • Examples • Documentation Permit makes it easy to add an authentication layer to any Node.js API. It can be used with any of the popular server frameworks (eg. Express, Koa, Hapi, Fastify) and it can be used for any type of API (eg. REST, GraphQL, etc.) due to its simple, unopinionated design. UsagePermit lets you authenticate via the two schemes most APIs need: a single secret bearer token, or a set of username and password credentials. For example, here's how to authenticate a bearer token: import { Bearer } from 'permit'
// A permit that checks for HTTP Bearer Auth, falling back to a query string.
const permit = new Bearer({
query: 'access_token',
})
async function handler({ req, res }) {
// Try to find the bearer token in the request.
const token = permit.check(req)
// No token, that means they didn't pass credentials!
if (!token) {
permit.fail(res)
throw new Error(`Authentication required!`)
}
// Authenticate the token however you'd like...
const user = await db.users.findByToken(token)
// No user, that means their credentials were invalid!
if (!user) {
permit.fail(res)
throw new Error(`Authentication invalid!`)
}
// They were authenticated, so continue with your business logic...
...
} Since Permit isn't tightly coupled to a framework or data model, it gives you complete control over how you write your authentication logic—the exact same way you'd write any other request handler. Why?Before Permit, the only real choice for authentication libraries in Node.js was Passport.js. But it has a bunch of issues that complicate your codebase...
Don't get me wrong, Passport works great for working with OAuth providers. But if you've run into any of these problems before while adding authentication to a Node.js API, you might like Permit. Which brings me to how Permit solves these issues... Principles
ExamplesPermit's API is very flexible, allowing it to be used for a variety of use cases depending on your server framework, your feelings about ORMs, your use of promises, etc. Here are a few examples of common patterns... DocumentationRead the getting started guide to familiarize yourself with how Permit works, or check out the full API reference for more detailed information... ThanksThank you to @dresende for graciously transferring the LicenseThis package is MIT-licensed. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论