https://github.com/reenphygeorge/smol-auth
A smol authentication package with RBAC 🔐
https://github.com/reenphygeorge/smol-auth
jwt kysely npm postgresql typescript
Last synced: over 1 year ago
JSON representation
A smol authentication package with RBAC 🔐
- Host: GitHub
- URL: https://github.com/reenphygeorge/smol-auth
- Owner: reenphygeorge
- License: gpl-3.0
- Created: 2023-10-22T05:14:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-06T15:36:36.000Z (over 2 years ago)
- Last Synced: 2025-03-11T19:48:19.879Z (over 1 year ago)
- Topics: jwt, kysely, npm, postgresql, typescript
- Language: TypeScript
- Homepage:
- Size: 254 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A smol authentication package with RBAC 🔐
## Guide to smol
#### Integrate smol-auth with your project
##### For Backend
1. Install the packages
npm i smol-auth-express
2. Setup an env file with accessToken & RefreshToken secrets which are long strings. You can generate it by running the following commands:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'));"
3. Initialize smol service
import { validateUser, smol } from 'smol-auth-express';
import express from 'express';
const app = express();
const smolConfig = {
connectionUrl: process.env.DB_URL,
accessTokenSecret: process.env.ACCESS_TOKEN_SECRET,
refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET,
clientDomain: process.env.WEBSITE_DOMAIN
}
smol()
.init(app, smolConfig)
`Only Express & postgreSQL (auth db) is supported`
###### RBAC
1. Basic Role: To grant broad permissions to a role, use the `*` wildcard. For example:
smol()
.addRoles({
admin: '*',
}, { defaultRole: 'admin' })
.init(app, smolConfig)
2. Multiple & Specific Roles: To specify permissions for a role on a particular route and method, use an array. For example:
smol()
.addRoles({
admin: '*',
user: [{ route: '/posts', method: '*' }],
viewer: [{ route: '/posts', method: ['GET'] }]
}, { defaultRole: 'viewer' })
.init(app, smolConfig)
`Default Role is required for now`
##### For Frontend
1. Install the packages
npm i smol-auth-client
2. Initialize smolClient
import { smolClient } from 'smol-auth-client'
smolClient(process.env.API_DOMAIN)
3. Use the corresponding functions
import { signin, signup, getAuthId, signout } from 'smol-auth-client'
const signupData = await signup(email, password)
const signinData = await signin(email, password)
const authId = await getAuthId()
await signout()
#### Try with our example app
1. Clone the repo
git clone https://github.com/reenphygeorge/smol-auth
2. Install all required packages
npm run install:all
3. Build smol-packages
npm run build
4. Start Example API Service
npm run start-example:api
5. Start Example Client Service
npm run start-example:client
6. Connect [NocoDB](https://docs.nocodb.com/data-sources/connect-to-data-source/) to the auth db for dashboard
http://localhost:8080
7. To stop docker containers after exit
npm run docker:stop