Skip to content

Instantly share code, notes, and snippets.

View Spyna's full-sized avatar
💭
🚀

Lorenzo Spinelli Spyna

💭
🚀
View GitHub Profile
@Spyna
Spyna / googleAuth.js
Last active February 1, 2018 11:30
google-auth-library token_id check
const credentials = require( './credentials.json' )
const GOOGLE_CLIENT_ID = credentials.google.client_id;
const { OAuth2Client } = require( 'google-auth-library' );
var client = new OAuth2Client( GOOGLE_CLIENT_ID, '', '' );
//return a promise with user informations
module.exports.getGoogleUser = ( code ) => {
//verify the token using google client
return client.verifyIdToken( { idToken: code, audience: GOOGLE_CLIENT_ID } )
.then( login => {
var express = require( 'express' );
var bodyParser = require( 'body-parser' );
var googleAuth = require( './googleAuth.js' );
var app = express();
var jwt = require('./jwt.js')
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( { extended: false } ) );
var jwt = require( 'jsonwebtoken' );
const secret = require( './credentials.json' ).jwt.secret
var createToken = ( user ) => {
return "thisisthetokenof:" + user.id
}
module.exports.verify = ( token ) => {
try {
const credentials = require( './credentials.json' ).facebook
const { client_id, client_secret } = credentials;
var fetch = require( 'node-fetch' );
module.exports.getUser = ( code ) => {
let appToken;
let url = 'https://graph.facebook.com/oauth/access_token?client_id=' + client_id + '&client_secret=' + client_secret + '&grant_type=client_credentials';
var jwt = require('./jwt');
const checkToken = ( req ) => {
//get the http header 'authorization'
let authorization = req.get( 'authorization' );
if ( !authorization ) {
throw new Error( 401 )
}
//check the token signature with 'jwt.js' library
let token = authorization.replace( 'Bearer ', '' );
import React from 'react';
import LoadingOverlay from './Loader'
export default (loader, props) => (
class AsyncComponent extends React.Component {
constructor(props) {
super(props);
this.Component = null;
this.state = { Component: AsyncComponent.Component };
}
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
@Spyna
Spyna / push-notifications-base.js
Last active August 26, 2019 13:16
How to show a web push notification
const img = "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg";
const text = "Take a look at this brand new t-shirt!";
const title = "New Product Available";
const options = {
body: text,
icon: "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg",
vibrate: [200, 100, 200],
tag: "new-product",
image: img,
badge: "https://spyna.it/icons/android-icon-192x192.png",
@Spyna
Spyna / push-notifications.js
Created August 26, 2019 13:27
How to display web push notificaitons
/**
* checks if Push notification and service workers are supported by your browser
*/
function isPushNotificationSupported() {
return "serviceWorker" in navigator && "PushManager" in window;
}
/**
* asks user consent to receive push notifications and returns the response of the user, one of granted, default, denied
*/
@Spyna
Spyna / create-push-notification-subscription.js
Last active August 26, 2019 14:35
How to create a web push notification subscription
const pushServerPublicKey = "<A PUSH SERVER PUBLIC KEY GOES HERE>";
/**
*
* using the registered service worker creates a push notification subscription and returns it
*
*/
function createNotificationSubscription() {
//wait for service worker installation to be ready, and then
return navigator.serviceWorker.ready.then(function(serviceWorker) {