UNPKG

authrix

Version:

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

1 lines 1.87 kB
'use strict';var chunk5ZKHY3X3_cjs=require('./chunk-5ZKHY3X3.cjs');require('./chunk-D6WM53FN.cjs');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(chunk5ZKHY3X3_cjs.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(chunk5ZKHY3X3_cjs.b.cookieName)}function f(){return u()!==null}function y(){return function(){return u()}}function A(e={}){return function(t){return function(r){return null}}}Object.defineProperty(exports,"authConfig",{enumerable:true,get:function(){return chunk5ZKHY3X3_cjs.b}});Object.defineProperty(exports,"initAuth",{enumerable:true,get:function(){return chunk5ZKHY3X3_cjs.c}});exports.createUseAuthToken=y;exports.getAuthToken=u;exports.getCurrentUserReact=a;exports.hasAuthToken=f;exports.isAuthenticatedReact=p;exports.logoutReact=d;exports.signinReact=m;exports.signupReact=h;exports.withAuthReact=A;