Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harshmangalam/elysia-prisma-jwt-auth
JWT authentication using Elysia framework
https://github.com/harshmangalam/elysia-prisma-jwt-auth
authentication authorization bun elysia javascript jwt-authentication postgresql prisma refresh-token typescript
Last synced: 2 months ago
JSON representation
JWT authentication using Elysia framework
- Host: GitHub
- URL: https://github.com/harshmangalam/elysia-prisma-jwt-auth
- Owner: harshmangalam
- Created: 2024-06-12T05:57:55.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-22T04:06:49.000Z (3 months ago)
- Last Synced: 2024-11-30T12:33:36.308Z (2 months ago)
- Topics: authentication, authorization, bun, elysia, javascript, jwt-authentication, postgresql, prisma, refresh-token, typescript
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JWT Authentication with Bun(Elysia)
## Tech Stack
- Bun
- Elysia
- Prisma
- Postgresql
- Typescript## Setup
Install dependencies
```
bun i
```Copy `.env.example` to `.env`
```
cp .env.example .env
```Sync prisma schema with db
```
bun run prisma:push
```## Route
- POST `/api/auth/sign-up` - Create new account
- POST `/api/auth/sign-in` - Sign in to existing account
- GET `/api/auth/me` - Fetch current user
- POST `/api/auth/logout` - Logout current user
- POST `/api/auth/refresh` - Create new pair of access & refresh token from existing refresh token## Authentication work flow
- Sign in
- Verify user email & password
- Create pair of access token and refresj token
- Save refresh token in db for further uses
- Set access token and refresh token in response cookies- Protected route `/me`
- verify jwt access token in plugin
- If access token is missing raise 401 status code error
- If access token is available but incorrect/expire raise 403 status code error
- In case of 403 error client can request for `/refresh` to generate new pair of access/refresh token
- In success case find the user from db and set using `derive` function
- Now `/me` can get user and return as a response