<p align="center"> <a href="https://www.propelauth.com?ref=github" target="_blank" align="center"> <img src="https://www.propelauth.com/imgs/lockup.svg" width="200"> </a> </p>
npm install @propelauth/nodeA Javascript library for managing authentication, backed by PropelAuth.
PropelAuth makes it easy to add authentication and authorization to your B2B/multi-tenant application.
Your frontend gets a beautiful, safe, and customizable login screen. Your backend gets easy authorization with just a few lines of code. You get an easy-to-use dashboard to config and manage everything.
- Full reference this library is here
- Getting started guides for PropelAuth are here
``shell`
npm install @propelauth/node
initBaseAuth performs a one-time initialization of the library.apiKey
It will verify your is correct and fetch the metadata needed to verify access tokens in validateAccessTokenAndGetUserClass.
`typescript
import { initBaseAuth } from '@propelauth/node';
const {
validateAccessTokenAndGetUserClass,
fetchUserMetadataByUserId,
// ...
} = initBaseAuth({
authUrl: "REPLACE_ME",
apiKey: "REPLACE_ME",
});
`
After initializing auth, you can verify access tokens by passing it in the Authorization header (formatted Bearer TOKEN) to validateAccessTokenAndGetUserClass.
You can see more information about the User Class here.
`tsGot request from user ${user.userId}
const authorizationHeader = // Get the Authorization header from an HTTP request
try {
const user = await validateAccessTokenAndGetUserClass(authorizationHeader)
console.log();Unauthorized request ${err}
} catch (err) {
// You can return a 401, or continue the request knowing it wasn't sent from a logged-in user
console.log();`
}
You can also verify which organizations the user is in, and which roles and permissions they have in each organization all through the User Class
Verify that the request was made by a valid user and that the user is a member of the specified organization.
`tsGot request from user ${user.userId} for org ${org.orgName}
const authorizationHeader = // Get the Authorization header from an HTTP request
const orgId = // get the orgId from somewhere, such as the request URL
try {
const user = await validateAccessTokenAndGetUserClass(authorizationHeader)
const org = user.getOrg(orgId)
if (!org) {
// return a 403
}
console.log();Unauthorized request ${err}
} catch (err) {
// You can return a 401, or continue the request knowing it wasn't sent from a logged-in user
console.log();`
}
Similar to checking org membership, but will also verify that the user has a specific Role in the organization.
A user has a Role within an organization. By default, the available roles are Owner, Admin, or Member, but these can be configured. These roles are also hierarchical, so Owner > Admin > Member.
`tsGot request from Admin user ${user.userId} for org ${org.orgName}
const authorizationHeader = // Get the Authorization header from an HTTP request
const orgId = // get the orgId from somewhere, such as the request URL
try {
const user = await validateAccessTokenAndGetUserClass(authorizationHeader)
const org = user.getOrg(orgId)
if (!org || !org.isRole("Admin")) {
// return a 403
}
console.log();Unauthorized request ${err}
} catch (err) {
// You can return a 401, or continue the request knowing it wasn't sent from a logged-in user
console.log();`
}
Similar to checking org membership, but will also verify that the user has the specified permission in the organization.
Permissions are arbitrary strings associated with a role. For example, can_view_billing, ProductA::CanCreate, and ReadOnly are all valid permissions. You can create these permissions in the PropelAuth dashboard.
`tsUser ${user.userId} has 'can_view_billing' permissions for org ${org.orgName}
const authorizationHeader = // Get the Authorization header from an HTTP request
const orgId = // get the orgId from somewhere, such as the request URL
try {
const user = await validateAccessTokenAndGetUserClass(authorizationHeader)
const org = user.getOrg(orgId)
if (!org || !org.hasPermission("can_view_billing")) {
// return a 403
}
console.log();Unauthorized request ${err}
} catch (err) {
// You can return a 401, or continue the request knowing it wasn't sent from a logged-in user
console.log();`
}
---
You can also use the library to call the PropelAuth APIs directly, allowing you to fetch users, create orgs, and a lot more.
`ts
const auth = initAuth({
authUrl: 'REPLACE_ME',
apiKey: 'REPLACE_ME',
})
const magicLink = await auth.createMagicLink({
email: 'user@customer.com',
})
``
---
Feel free to reach out at support@propelauth.com