UNPKG

authrix

Version:

Lightweight, flexible authentication library for Node.js and TypeScript.

1 lines 1.62 kB
import {b}from'./chunk-GACMQPPZ.mjs';export{b as authConfig,c as initAuth}from'./chunk-GACMQPPZ.mjs';import'./chunk-3SAEGOMQ.mjs';async function o(e,t={}){let n=await fetch(e,{credentials:"include",headers:{"Content-Type":"application/json",...t.headers||{}},...t}),r=null;try{r=await n.json();}catch{}if(!n.ok){let s=r?.error?.message||r?.message||`${n.status} ${n.statusText}`;throw new Error(s)}return r}function g(e){if(typeof document>"u")return null;let n=`; ${document.cookie}`.split(`; ${e}=`);return n.length===2&&n.pop()?.split(";").shift()||null}function l(e,t="/"){typeof document>"u"||(document.cookie=`${e}=; Path=${t}; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`);}async function h(e,t,n="/api/auth/signup"){return o(n,{method:"POST",body:JSON.stringify({email:e,password:t})})}async function m(e,t,n="/api/auth/signin"){return o(n,{method:"POST",body:JSON.stringify({email:e,password:t})})}async function d(e="/api/auth/logout"){let t=await o(e,{method:"POST"});return l(b.cookieName),t}async function a(e="/api/auth/me"){try{return (await o(e,{method:"GET"})).user||null}catch(t){return t.message?.toLowerCase().includes("unauthorized")||t.message==="401 Unauthorized",null}}async function p(e="/api/auth/me"){return await a(e)!==null}function u(){return g(b.cookieName)}function f(){return u()!==null}function y(){return function(){return u()}}function A(e={}){return function(t){return function(r){return null}}}export{y as createUseAuthToken,u as getAuthToken,a as getCurrentUserReact,f as hasAuthToken,p as isAuthenticatedReact,d as logoutReact,m as signinReact,h as signupReact,A as withAuthReact};