https://github.com/ariomoklo/hansip-old
Authorization library for building SSR app.
https://github.com/ariomoklo/hansip-old
astro auth authorization cookie jwt nuxt ssr
Last synced: about 2 months ago
JSON representation
Authorization library for building SSR app.
- Host: GitHub
- URL: https://github.com/ariomoklo/hansip-old
- Owner: ariomoklo
- Created: 2022-08-30T11:25:14.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-06T16:45:53.000Z (over 2 years ago)
- Last Synced: 2025-02-15T05:27:49.512Z (3 months ago)
- Topics: astro, auth, authorization, cookie, jwt, nuxt, ssr
- Language: Astro
- Homepage:
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hansip
Authorization library for building SSR app.
Hansip adalah authorisasi library. Konsep dari hansip adalah authentikasi haruslah berasal dari API dan aplikasi front-end hanya melakukan authorisasi terhadap token yang diberikan oleh API. Hansip akan mencari token dalam cookie / header / url mengikuti kebutuhan mu.
>
> this package is not stable.
>
> use with your own risk.
>## Usage
>
> install hansip as dependency
>
> `npm install hansip` | `yarn add hansip` | `pnpm add hansip`
>### createCookieSession
``` ts
import { createCookieSession } from 'hansip'// get your cookie
const cookie = request.headers.get('cookie')
const session = createCookieSession({
cookie: cookie,
tokenName: 'token', // cookie name for jwt token.
refreshName: 'refresh', // token refresh name. optional
cookieOptions: {
/**
check cookie.serialize options for detailed info
@link https://www.npmjs.com/package/cookiedomain?: string | undefined,
expires?: Date | undefined,
httpOnly?: boolean | undefined,
maxAge?: number | undefined,
path?: string | undefined,
priority?: 'low' | 'medium' | 'high' | undefined,
sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined,
secure?: boolean | undefined,
*/
}
})const { token } = session.get()
// do anything you want with token.
if (!token) {
// redirect on token not found or validation false
return redirect()
}response.headers.set('Set-Cookie', session.serialize.token())
response.headers.set('Set-Cookie', session.serialize.refresh())
// send response with token in cookie```
### detectURL
``` ts
import { detectURL, createCookieSession } from 'hansip'const session = createCookieSession({ tokenName: 'token' })
const url = new URL(request.url, 'http://localhost')const found = detectURL(url, { tokenName: 'access_token', refreshName: 'refresh_token' })
if (!found.token) {
// redirect on token undefined
}// do anything you want with token and refresh token
const validatedToken = found.token
if (!validatedToken) {
// if token not valid redirect
}session.set(validatedToken, validatedToken)
response.headers.set('Set-Cookie', session.serialize.token())
response.headers.set('Set-Cookie', session.serialize.refresh())
// send response with token in cookie
```## Target
- [ ] JWT utility / helper
- [ ] ... ?