Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facebook OAuth example? #90

Closed
vladinator1000 opened this issue Aug 9, 2016 · 2 comments
Closed

Facebook OAuth example? #90

vladinator1000 opened this issue Aug 9, 2016 · 2 comments

Comments

@vladinator1000
Copy link

vladinator1000 commented Aug 9, 2016

Hello guys, noob here, I'm trying to figure out how to do this right. I'm getting authenticated by Facebook without a problem, but when I got to the part of using the actual data from Facebook I got stuck... I've been copying from here and there. How would you go about using the info received from the OAuth thing?

Here's my code, I've been referencing GitHunt and Jonas' auth article mainly.

import express from 'express';
import path from 'path';
import uuid from 'node-uuid';

// GraphQL
import { apolloServer } from 'apollo-server';
import schema from './postgres/schema';
import resolvers from './postgres/resolvers';

// Webpack
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from '../webpack.config.dev.js';

// Authentication
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import passport from 'passport';
import { Strategy as FacebookStrategy } from 'passport-facebook';

import facebookStrategyOptions from './auth/config';


// Knex Session
import knex from './postgres/connectors';
const KnexSessionStore = require('connect-session-knex')(session);
const store = new KnexSessionStore({ knex });

import { User } from './postgres/models/User';
const userModel = new User();


// Routes
import routes from './routes';
import facebook from './routes/auth/facebook';
import local from './routes/auth/local';


const GRAPHQL_PORT = 3000;

const app = express();

const compiler = webpack(config);

app.use(webpackMiddleware(compiler, {
    hot: true,
    noInfo: true,
    publicPath: config.output.publicPath,
    stats: {
        colors: true,
        timings: true,
        modules: false
    }
}));

app.use(webpackHotMiddleware(compiler));

// React front end
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../client/index.html'));
});

app.get('/logout', (req, res) => {
    req.logout();
    res.redirect('/');
});

app.use('/graphql', apolloServer({
    graphiql: true, 
    pretty: true,
    schema,
    resolvers
}));


// Authentication
app.use(session({
    saveUninitialized: true,
    resave: true,
    genid: req => {
         return uuid.v4();
    },
    secret: 'Z3]GJW!?9uP”/Kpe',
    store
}));

app.use(passport.initialize());
app.use(passport.session());

app.get('/login/facebook',
    passport.authenticate('facebook', {
        scope: ['email']
    }));

app.get('/login/facebook/callback',
    passport.authenticate('facebook', {
        failureRedirect: '/bleh',
        successRedirect: '/'
    }),
    (req, res) => res.redirect('/')
);

// Parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

passport.use(new FacebookStrategy(facebookStrategyOptions, (accessToken, refreshToken, profile, cb) => {
    console.log(profile); // The info I need is there, but I can't figure out how to use it
    userModel.findFacebook(profile)
        .then(user => {
            if(user) {
                return done(null, user)
            }

            else {
                userModel.createFacebook(profile)
                .then(newUser => {
                    return done(null, newUser)
                })
            }
        })
        .catch(error => console.log('Oh no...',error));

    cb(null, profile);
}));

passport.serializeUser((user, cb) => cb(null, user));
passport.deserializeUser((obj, cb) => cb(null, obj));

app.listen(GRAPHQL_PORT, () => console.log(
    `GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`
));
@helfer
Copy link
Contributor

helfer commented Aug 11, 2016

Can you be a bit more specific on where you're stuck? Also, you might have more luck asking on the Apollo slack channel, because technically this isn't an apollo-server issue.

@vladinator1000
Copy link
Author

Good point @helfer, I'll close this and pop in the slack channel.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants