Skip to content

Instantly share code, notes, and snippets.

@shark-h
shark-h / create-table.sql
Created March 4, 2019 09:12
creating tables for voting app
CREATE TABLE "public"."loved_language" (
"name" text NOT NULL,
"user_id" text NOT NULL,
CONSTRAINT loved_language_pkey PRIMARY KEY (name, user_id),
CONSTRAINT loved_language_programming_language_fky FOREIGN KEY (name) REFERENCES programming_language(name),
)
@shark-h
shark-h / getUserData.js
Last active August 3, 2020 07:48
Get user data from Firebase
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
// On sign up.
exports.processSignUp = functions.auth.user().onCreate(user => {
const customClaims = {
"https://hasura.io/jwt/claims": {
"x-hasura-default-role": "user",
"x-hasura-allowed-roles": ["user"],
@shark-h
shark-h / auth.js
Last active February 12, 2020 06:12
Auth
import firebase from "firebase/app";
import "firebase/auth";
import "firebase/database";
import React, { useState, useEffect } from "react";
import App from "./App";
const provider = new firebase.auth.GoogleAuthProvider();
// Find these options in your Firebase console
firebase.initializeApp({
@shark-h
shark-h / app.js
Last active September 15, 2022 09:39
App
import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";
import { split } from "apollo-link";
import { HttpLink } from "apollo-link-http";
import { WebSocketLink } from "apollo-link-ws";
import { getMainDefinition } from "apollo-utilities";
import gql from "graphql-tag";
import React from "react";
import { ApolloProvider, Mutation, Subscription } from "react-apollo";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const request = require("graphql-request");
const client = new request.GraphQLClient('<your-graphql-endpoint>', {
headers: {
"content-type": "application/json",
"x-hasura-admin-secret": "<your-admin-secret>"
}
})
// REGISTER USER WITH REQUIRED CUSTOM CLAIMS
exports.registerUser = functions.https.onCall(async (data, context) => {
const email = data.email;
const password = data.password;
const displayName = data.displayName;
if (email == null || password == null || displayName == null) {
throw new functions.https.HttpsError('signup-failed', 'missing information');
}
// REGISTER USER WITH REQUIRED CUSTOM CLAIMS
exports.registerUser = functions.https.onCall(async (data, context) => {
const email = data.email;
const password = data.password;
const displayName = data.displayName;
if (email == null || password == null || displayName == null) {
throw new functions.https.HttpsError('signup-failed', 'missing information');
}
// SYNC WITH HASURA ON USER CREATE
exports.processSignUp = functions.auth.user().onCreate(async user => {
const id = user.uid;
const email = user.email;
const name = user.displayName || "No Name";
const mutation = `mutation($id: String!, $email: String, $name: String) {
insert_users(objects: [{
id: $id,
// SYNC WITH HASURA ON USER DELETE
exports.processDelete = functions.auth.user().onDelete(async (user) => {
const mutation = `mutation($id: String!) {
delete_users(where: {id: {_eq: $id}}) {
affected_rows
}
}`;
const id = user.uid;
try {
const data = await client.request(mutation, {
// INCREMENT USER SCORE IF THE ANSWER IS CORRECT
exports.checkAnswer = functions.https.onRequest( async (request, response) => {
const answerID = request.body.event.data.new.answer_id;
const userID = request.body.event.data.new.user_id;
const answerQuery = `
queryAnswer($answerID: uuid!) {
question_answers(where: {id: {_eq: $answerID}}) {
is_correct
}