add isLoggedIn function

This commit is contained in:
Laurenz Rausche 2024-05-12 18:23:13 +00:00
parent 04e27a0db9
commit 7432031cd0
2 changed files with 19 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@auth-tools/client",
"version": "0.0.1-alpha.5",
"version": "0.0.1-alpha.6",
"description": "A structured authentication protocol for Javascript. (client)",
"main": "dist/index.js",
"repository": "https://github.com/auth-tools/auth-tools",

View File

@ -15,7 +15,7 @@ import { createLogout } from "./methods/logout";
import { createRefresh } from "./methods/refresh";
import { createCheck } from "./methods/check";
type MethodReturn<MethodName extends keyof AuthProtocol> =
export type MethodReturn<MethodName extends keyof AuthProtocol> =
| { clientError: true; res: null }
| { clientError: false; res: AuthResponse<MethodName> };
@ -88,4 +88,21 @@ export class AuthClient extends AuthBase<
check: createCheck(this._internal),
};
}
//check if user is logged in
public async isLoggedIn(): Promise<boolean> {
const checkResponse = await (
this._internal.config.connector as AuthClientConnector<"check">
)("check", {});
if (checkResponse.clientError) return false;
if (!checkResponse.res.error) return true;
const refreshResponse = await (
this._internal.config.connector as AuthClientConnector<"refresh">
)("refresh", {});
if (refreshResponse.clientError) return false;
return !refreshResponse.res.error;
}
}