Skip to content

Instantly share code, notes, and snippets.

View AndrejGajdos's full-sized avatar

Andrej Gajdos AndrejGajdos

View GitHub Profile
@AndrejGajdos
AndrejGajdos / scrollTo.js
Created August 21, 2017 13:40 — forked from joshcanhelp/scrollTo.js
Animated scrollTo for specific element or top of page
//
// Smooth scroll-to inspired by:
// http://stackoverflow.com/a/24559613/728480
//
module.exports = function (scrollTo, scrollDuration) {
//
// Set a default for where we're scrolling to
//
@AndrejGajdos
AndrejGajdos / package.json
Last active June 24, 2018 07:35
Project dependencies for implementing user authentication in Node.js, Passport, React, Redux. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/package.json
{
"dependencies": {
"@koa/cors": "^2.2.1",
"axios": "^0.18.0",
"bcrypt": "^2.0.0",
"bootstrap": "^4.1.0",
"classnames": "^2.2.5",
"dotenv": "^6.0.0",
"find-config": "^1.0.0",
"jquery": "^3.3.1",
@AndrejGajdos
AndrejGajdos / server.js
Last active June 24, 2018 07:36
Adding required modules and creating new koa application for user authentication project. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/server.js
require('dotenv').config({ path: require('find-config')('.env') });
const Koa = require('koa');
const Router = require('koa-router');
const koaLogger = require('koa-logger');
const cors = require('@koa/cors');
const bodyParser = require('koa-bodyparser');
const serve = require('koa-static');
const send = require('koa-send');
const path = require('path');
const session = require('koa-session');
@AndrejGajdos
AndrejGajdos / server.js
Last active June 24, 2018 07:37
Initializing Redis and adding one user into mock database. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/server.js
// create mock database with one user
const db = redis.createClient();
db.on('error', (err) => {
console.log(`Redis Error ${err}`);
});
db.set('usersMockDatabase', JSON.stringify([
{
id: 1,
email: 'chouomam@chouman.com',
// "test" -- generated by bcrypt calculator
@AndrejGajdos
AndrejGajdos / server.js
Last active June 24, 2018 07:37
koa router with routes for authentication project. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/server.js
const auth = require('./controllers/auth');
const router = new Router();
router
/* Handle Login POST */
.post('/login', ctx => passport.authenticate('local', (err, user) => {
if (!user) {
ctx.throw(401, err);
} else {
ctx.body = user;
@AndrejGajdos
AndrejGajdos / auth.js
Last active June 24, 2018 07:38
Adding required libraries and modules for user authentication, adding serializing and de-serializing the user information to the session. https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/controllers/auth.js
const bcrypt = require('bcrypt');
const passport = require('koa-passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const config = require('../serverConfig');
const { db } = require('../server');
const { promisify } = require('util');
const getAsync = promisify(db.get).bind(db);
passport.use(
new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
},
async (email, password, done) => {
let user = null;
await getAsync('usersMockDatabase').then((users) => {
const currUsers = JSON.parse(users);
passport.use(
new FacebookStrategy(
{
clientID: config.facebookAuth.clientID,
clientSecret: config.facebookAuth.clientSecret,
callbackURL: config.facebookAuth.callbackURL,
profileFields: [
'id',
'displayName',
'picture.width(200).height(200)',
exports.getLoggedUser = async (ctx) => {
if (ctx.isAuthenticated()) {
const reqUserId = ctx.req.user.id;
let user = null;
await getAsync('usersMockDatabase').then((users) => {
user = JSON.parse(users).find(currUser => currUser.id === reqUserId);
});
if (user) {
delete user.password;
ctx.response.body = user;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AUTH FLOW</title>
<meta name="description" content="">
<!-- Mobile-friendly viewport -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>