Skip to content

Instantly share code, notes, and snippets.

View gragland's full-sized avatar

Gabe Ragland gragland

View GitHub Profile
// Usage
function ProfilePage({ uid }) {
// Subscribe to Firestore document
const { data, status, error } = useFirestoreQuery(
firestore.collection("profiles").doc(uid)
);
if (status === "loading"){
return "Loading...";
}
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const requireAuth = require("./_require-auth.js");
const { updateUser } = require("./_db.js");
export default requireAuth((req, res) => {
const user = req.user;
// If user already has a stripeCustomerId then return it in success response
if (user.stripeCustomerId) {
return res.send({
// Usage
export function useUser(uid) {
return useQuery(uid && firestore.collection("users").doc(uid));
}
// Custom useQuery hook for Firestore
function useQuery(query) {
const [queryState, setQueryState] = useState({
status: "loading",
data: undefined,
@gragland
gragland / firestore-rules
Last active September 20, 2021 06:15
Some Firestore security rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if isUser(uid);
}
match /items/{id} {
allow read: if true;
//allow read: if isOwner(); // Would restrict reads to owner
allow delete: if isOwner();
@gragland
gragland / use-memo-compare.jsx
Last active March 12, 2022 03:23
React Hook recipe from https://usehooks.com
import React, { useState, useEffect, useRef } from 'react';
// Usage
function MyComponent({ obj }) {
const [state, setState] = useState();
// Use the previous obj value if the "id" property hasn't changed
const objFinal = useMemoCompare(obj, (prev, next) => {
return prev && prev.id === next.id;
});
const signinAnonymously = () => {
return firebase.auth().signInAnonymously()
.then(response => {
setUser(response.user);
return response.user;
});
};
etc...
@gragland
gragland / use-async.jsx
Last active February 17, 2024 16:42
React Hook recipe from https://usehooks.com
import React, { useState, useEffect, useCallback } from 'react';
// Usage
function App() {
const { execute, status, value, error } = useAsync(myFunction, false);
return (
<div>
{status === 'idle' && <div>Start your journey by clicking a button</div>}
{status === 'success' && <div>{value}</div>}
@gragland
gragland / use-require-auth.js
Last active January 27, 2024 16:36
React Hook recipe from https://usehooks.com
import Dashboard from "./Dashboard.js";
import Loading from "./Loading.js";
import { useRequireAuth } from "./use-require-auth.js";
function DashboardPage(props) {
const auth = useRequireAuth();
// If auth is null (still fetching data)
// or false (logged out, above hook will redirect)
// then show loading indicator.
@gragland
gragland / use-router.jsx
Last active December 27, 2022 07:36
React Hook recipe from https://usehooks.com
import { useMemo } from "react";
import { useParams, useLocation, useHistory, useRouteMatch } from 'react-router-dom';
import queryString from 'query-string';
// Usage
function MyComponent(){
// Get the router object
const router = useRouter();
// Get value from query string (?postId=123) or route param (/:postId)
@gragland
gragland / auth.js
Last active June 23, 2021 11:00
Auth0 React Hook (exported by divjoy.com)
import React, { useState, useEffect, useContext, createContext } from "react";
import Auth0 from "auth0-js";
/*
SETUP INSTRUCTIONS:
- Create a new Auth0 application and choose type "Single Page Web Applications"
- Go to your news app's setting and add the domain and clientId to the code below
where it says "Replace me".
- In your new apps settings scroll to the bottom, click "Show Advanced Settings",
go to the "Grant Types" tab, check the "Password" option, then click save.