Skip to content

Instantly share code, notes, and snippets.

David Pennington Xeoncross

Block or report user

Report or block Xeoncross

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Xeoncross
Xeoncross / size_of_slice.go
Created Sep 18, 2019
How much memory does a slice of random strings actually take?
View size_of_slice.go
package main
import (
"fmt"
"math/rand"
"unsafe"
)
// Thanks to Kale, Andrei, and Jaden on gophers slack
@Xeoncross
Xeoncross / styles.less
Created Sep 16, 2019
Remove extra colors from Atom IDE solorized theme
View styles.less
# Select Solarized Light Syntax theme
# open styles.less and add the following
.syntax--variable,
.syntax--storage,
.syntax--entity,
.syntax--name,
.syntax--type {
color: #657b83 !important;
}
View javascript_encryption.md
@Xeoncross
Xeoncross / mock_http_client.go
Last active Aug 14, 2019
Simple wrapper to use for testing when you need a custom http.Client for faking network requests
View mock_http_client.go
// https://golang.org/pkg/net/http/#Client
type MockClient struct {
Body interface{}
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
if m.DoFunc != nil {
View use-local-storage-example.js
import { useState } from 'react';
// Usage
function App() {
// Similar to useState but first arg is key to the value in local storage.
const [name, setName] = useLocalStorage('name', 'Bob');
return (
<div>
<input
View ApiProvider.tsx
import * as React from 'react';
import Axios, { AxiosInstance } from 'axios';
import { createContext, consume } from '../lib/context-utils';
import { AuthContext } from './AuthProvider';
export const ApiContext = createContext('apiContext', {
api: undefined as AxiosInstance | undefined,
auth: undefined as React.ContextType<typeof AuthContext> | undefined,
})
@Xeoncross
Xeoncross / use-auth.js
Created Aug 5, 2019 — forked from timc1/use-auth.js
React Context + Hooks + Firebase Authentication
View use-auth.js
import React from 'react'
import firebaseConfig from '../path/to/firebase-config'
import firebase from 'firebase/app'
import 'firebase/auth'
import FullPageLoading from '../path/to/full-page-loading'
AuthProvider.actions = {
setUser: 'SET_USER',
toggleLoading: 'TOGGLE_LOADING',
}
@Xeoncross
Xeoncross / config.js
Created Aug 4, 2019 — forked from mccahill/config.js
Example of a node OAuth (Twitter) and OAuth2 (Google Calendar) client that works with the version 3 Express framework. This assumes you have a config.js file holding the keys and secrets
View config.js
module.exports = {
'HOSTPATH': 'http://your.host.here',
'PORT': 80,
'EXPRESS_SESSION_SECRET': '123456',
'TWITTER_CONSUMER_KEY': 'your-consumer-key-here',
'TWITTER_CONSUMER_SECRET': 'your-secret-here',
'GOOGLE_APP_ID': 'your-app-id-here',
'GOOGLE_CONSUMER_SECRET': 'your-consumer-secret-here',
};
@Xeoncross
Xeoncross / react_oauth_flow.md
Last active Aug 5, 2019
Breakdown of the different ways to handle OAuth flow for single-page apps like reactjs using backends on the same (or different) origin
View react_oauth_flow.md

I'm trying to handle OAuth from a react app. Rather than using an external service like Firebase or AuthO, I would like to handle OAuth login to facebook, google, twitter myself. (Regardless of the backend, OAuth libraries that can verify and trade the token for user info abound).

Here is the basic flow:

  1. React SPA opens seperate [popup/iframe/browser tab] to our server
  2. Our server creates OAuth URL payload and issues redirect to fb/google/twitter
  3. User login on fb/google/twitter redirect back to our server
  4. Our server communicates with React SPA 4.1. If same origin 4.1.1. Using localStorage
@Xeoncross
Xeoncross / fetchjson.js
Created Jul 27, 2019
Basica fetch() wrapper to make sure we always get a JSON response even if there is an error, or the server returns some other content type.
View fetchjson.js
// Look into wrapping fetch so it always returns JSON
export default function fetchjson(url, data, options) {
const defaults = {
// credentials: 'same-origin',
credentials: 'omit',
method: data ? 'post' : 'get',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': data ? 'application/json' : 'text/plain; charset=utf-8',
You can’t perform that action at this time.