Skip to content

Instantly share code, notes, and snippets.

@kohendrix
kohendrix / userRepository.js
Created May 21, 2019 02:06
user repo circular ref example
const User = require('./user');
function get() {
// check the db or something
return new User('John', 'johndoe@somemail.com');
}
exports.getUser = get;
@kohendrix
kohendrix / user.js
Created May 21, 2019 02:04
user model circular reference example
const repo = require('./userRepository');
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
greet() {
return `Hello, my name is ${name}`;
@kohendrix
kohendrix / DynamoClient.ts
Created March 26, 2019 04:08
AWS DynamoDB client for Node.js
import AWS from 'aws-sdk';
import {
DocumentClient,
PutItemInput,
PutItemOutput,
GetItemInput,
QueryInput,
ScanInput,
DeleteItemInput,
DeleteItemOutput,
@kohendrix
kohendrix / stringValidator.js
Last active March 27, 2019 05:20
Validator.js wrapper for chaining
/**
* wrapper for Validator.js
* ref:
* https://www.npmjs.com/package/validator
*/
import validator from 'validator';
/**
* error messages
*/
@kohendrix
kohendrix / template.test.js
Last active March 12, 2019 02:26
Mocha test template with boilerplate chords
/**
* Mocha Test Template
* refs ================================
* Mocha: https://mochajs.org
* Chai: https://www.chaijs.com
* Chai-As-Promised: https://www.chaijs.com/plugins/chai-as-promised/
* Sinon: https://sinonjs.org
* debug: https://www.npmjs.com/package/debug
*/
import chai from 'chai';
@kohendrix
kohendrix / googleApisClient.js
Created February 28, 2019 09:56
google sign-in sample. get data from code given in the redirect url
/**
* get selected fields of userinfo
* @param { string } code
* @return { object }
*/
async function getUserInfoFromCode(code) {
const client = getOAuth2Client();
const data = await client.getToken(code);
client.setCredentials(data.tokens);
const userinfo = await client.request({ url: infoUrl });
@kohendrix
kohendrix / googleApisClient.js
Created February 28, 2019 09:48
Google sign-in url getter
import { OAuth2Client } from 'google-auth-library';
const p = console.log;
/** APIs Explorer for OAuth2 v1: https://developers.google.com/apis-explorer/#p/oauth2/v1/ **/
const infoUrl = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=JSON';
/**
* information scope needed
* reference: https://developers.google.com/identity/protocols/googlescopes
* check: Google OAuth2 API, v2
*/
@kohendrix
kohendrix / googleApisClient.js
Created February 28, 2019 09:23
Google sign-in OAuth2 sample
import { OAuth2Client } from 'google-auth-library';
const p = console.log;
/**
* this establishes the connection
* @return { OAuth2Client }
*/
function getOAuth2Client() {
return new OAuth2Client('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'REDIRECT_URL');
}
@kohendrix
kohendrix / index.ejs
Created February 28, 2019 08:45
Google Sign-in callback functions
<script>
// called by google client
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
// never log these out on a real app
console.log('id_token: ', id_token);
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
@kohendrix
kohendrix / index.ejs
Created February 28, 2019 08:33
Google Sign-in implementation on html
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="<%= clientIdUrl %>" />