Skip to content

Commit

Permalink
Add autoLogin props to PolyfireProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-btc committed Jan 16, 2024
1 parent f9ed9bc commit 115dc84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
39 changes: 24 additions & 15 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,36 @@ export async function logout(co: MutablePromise<Partial<ClientOptions>>): Promis
export async function init(
co: MutablePromise<Partial<ClientOptions>>,
projectOptions: { project: string; endpoint: string },
autoLogin = true,
): Promise<boolean> {
if (typeof window === "undefined") {
try {
if (!autoLogin) {
throw new PolyfireError("Auto login disabled");
}
await signInAnon(undefined, co, projectOptions);
return true;
} catch (e) {
if (typeof window === "undefined") {
co.throw(new PolyfireError("You need to be authenticated to use this function"));
return false;
}

const session = await getSession(projectOptions.project, projectOptions).catch(() => {
clearSessionStorage();
return {} as { token?: string; email?: string };
});
if (session.token) {
co.set({ token: session.token, endpoint: projectOptions.endpoint });
return true;
}

co.throw(new PolyfireError("You need to be authenticated to use this function"));
return false;
}

const session = await getSession(projectOptions.project, projectOptions).catch(() => {
clearSessionStorage();
return {} as { token?: string; email?: string };
});
if (session.token) {
co.set({ token: session.token, endpoint: projectOptions.endpoint });
return true;
}

co.throw(new PolyfireError("You need to be authenticated to use this function"));
return false;
}

export type AuthClient = {
init: () => Promise<boolean>;
init: (autoLogin?: boolean) => Promise<boolean>;
login: (input: LoginFunctionInput) => Promise<void>;
logout: () => Promise<void>;
};
Expand All @@ -228,7 +237,7 @@ export default function authClient(
projectOptions: { project: string; endpoint: string },
): AuthClient {
return {
init: () => init(co, projectOptions),
init: (autoLogin) => init(co, projectOptions, autoLogin),
login: (input: LoginFunctionInput) => login(input, projectOptions, co),
logout: () => logout(co),
};
Expand Down
4 changes: 3 additions & 1 deletion lib/hooks/usePolyfire.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export function PolyfireProvider({
children,
project,
endpoint = "https://api.polyfire.com",
autoLogin,
}: {
children: ReactNode;
project: string;
endpoint?: string;
autoLogin?: boolean;
}): JSX.Element {
const [status, setStatus] = useState<AuthStatus>("loading");
const [polyfire] = useState<Client>(() => PolyfireClientBuilder({ project, endpoint }));

useEffect(() => {
polyfire.auth.init().then((isAuthenticated) => {
polyfire.auth.init(autoLogin).then((isAuthenticated) => {
setStatus(isAuthenticated ? "authenticated" : "unauthenticated");
});
}, []);
Expand Down

0 comments on commit 115dc84

Please sign in to comment.