text: "use strict";function Auth(api=null,localDB=null,options={}){if(options&&"object"==typeof options||(options={}),!api)throw"Starbase Channels API Client is missing.";if(!localDB)throw"Starbase Channels Database is missing.";let db=localDB,request=(method,credentials)=>api.path("/").request({method:method,data:credentials}),tokenPath=(options.parentChannel||"/auth/token").toString(),stateHandler=null,authToken=null,user=null,stateChange=async token=>{if(token||(token=null),authToken=token,db){await db.path(tokenPath).put({token:authToken})}authToken&&authToken.user?(user=authToken.user,auth.user=user):(user=null,auth.user=user),stateHandler&&"function"==typeof stateHandler&&stateHandler(authToken)},auth={user:null};return auth.getToken=(()=>(async()=>new Promise(async(resolve,reject)=>{if(!authToken&&db){let storedToken=await db.path(tokenPath).get().then(result=>result.data).catch(err=>null);storedToken&&storedToken.token&&(authToken=storedToken.token,stateChange(authToken))}return authToken?authToken.accessExpires<Date.now()?authToken.refreshExpires>Date.now()?void request("refreshToken",authToken).then(result=>(stateChange(result),resolve(result.accessToken))).catch(err=>(err.code&&err.code<499&&stateChange(null),reject(err))):(stateChange(null),reject({code:400,message:"Invalid or expired token."})):resolve(authToken.accessToken):resolve(null)}))()),auth.onStateChange=(cb=>{stateHandler=cb,auth.getToken().catch(err=>{})}),auth.createUser=((username,password)=>new Promise((resolve,reject)=>{request("createUser",{username:username,password:password}).then(resolve).catch(reject)})),auth.deleteUser=((username,password)=>new Promise((resolve,reject)=>{request("deleteUser",{username:username,password:password}).then(resolve).catch(reject)})),auth.changePassword=((username,password,newPassword)=>new Promise((resolve,reject)=>{request("changePassword",{username:username,password:password,newPassword:newPassword}).then(resolve).catch(reject)})),auth.signIn=((username,password)=>new Promise((resolve,reject)=>{request("signIn",{username:username,password:password}).then(token=>{stateChange(token),resolve(token)}).catch(err=>{reject(err)})})),auth.signOut=(()=>new Promise((resolve,reject)=>{stateChange(null),resolve({message:"Signed out."})})),auth}