Skip to content

Commit

Permalink
feat(auth): add auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Oct 15, 2021
1 parent 2689181 commit 12ab9ca
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/test-results/

# modules
/auth/
/firestore/
/storage/
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
},
"./auth": {
"require": "./lib/cjs/auth/index.js",
"default": "./lib/esm/auth/index.js"
},
"./firestore": {
"require": "./lib/cjs/firestore/index.js",
"default": "./lib/esm/firestore/index.js"
Expand All @@ -21,6 +25,7 @@
},
"files": [
"lib",
"auth",
"firestore",
"storage"
],
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require("fs");

const modules = ["firestore", "storage"];
const modules = ["auth", "firestore", "storage"];
for (const module of modules) {
const packageJson = {
main: `../lib/cjs/${module}/index.js`,
Expand Down
1 change: 1 addition & 0 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useAuthState";
20 changes: 20 additions & 0 deletions src/auth/useAuthState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Auth, AuthError, onAuthStateChanged, User } from "firebase/auth";
import { useEffect, useMemo } from "react";
import { ValueHookResult } from "../common";
import { useLoadingValue } from "../util/useLoadingValue";

export type UseAuthStateResult = ValueHookResult<User | null, AuthError>;

export function useAuthState(auth: Auth): UseAuthStateResult {
const { error, loading, setError, setValue, value } = useLoadingValue<User | null, AuthError>(auth.currentUser);

useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, setValue, (e) => setError(e as AuthError));

return () => {
unsubscribe();
};
}, [auth]);

return useMemo(() => [value, loading, error], [value, loading, error]);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./auth";
export * from "./common";
export * from "./firestore";
export * from "./storage";
Expand Down

0 comments on commit 12ab9ca

Please sign in to comment.