├── .gitignore ├── client ├── public │ ├── favicon.ico │ ├── manifest.json │ └── index.html ├── src │ ├── actions │ │ ├── types.js │ │ └── index.js │ ├── setupProxy.js │ ├── reducers │ │ ├── index.js │ │ ├── authReducer.js │ │ └── channelsReducer.js │ ├── components │ │ ├── Landing.js │ │ ├── ChannelList.js │ │ ├── Header.js │ │ └── App.js │ ├── index.js │ └── serviceWorker.js ├── .gitignore ├── package.json └── README.md ├── config ├── keys.js └── prod.js ├── models └── User.js ├── routes ├── youtubeRoutes.js └── authRoutes.js ├── index.js ├── package.json └── services └── passport.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dev.js 3 | -------------------------------------------------------------------------------- /client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StephenGrider/GoogleAuth2020/master/client/public/favicon.ico -------------------------------------------------------------------------------- /client/src/actions/types.js: -------------------------------------------------------------------------------- 1 | export const FETCH_USER = 'fetch_user'; 2 | export const FETCH_CHANNELS = 'fetch_channels'; 3 | -------------------------------------------------------------------------------- /config/keys.js: -------------------------------------------------------------------------------- 1 | if (process.env.NODE_ENV === 'production') { 2 | module.exports = require('./prod'); 3 | } else { 4 | module.exports = require('./dev'); 5 | } 6 | -------------------------------------------------------------------------------- /config/prod.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | googleClientID: process.env.GOOGLE_CLIENT_ID, 3 | googleClientSecret: process.env.GOOGLE_CLIENT_SECRET, 4 | mongoURI: process.env.MONGO_URI, 5 | cookieKey: process.env.COOKIE_KEY 6 | }; 7 | -------------------------------------------------------------------------------- /models/User.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const { Schema } = mongoose; 3 | 4 | const userSchema = new Schema({ 5 | googleId: String, 6 | googleAccessToken: String 7 | }); 8 | 9 | mongoose.model('users', userSchema); 10 | -------------------------------------------------------------------------------- /client/src/setupProxy.js: -------------------------------------------------------------------------------- 1 | const proxy = require('http-proxy-middleware'); 2 | 3 | module.exports = function(app) { 4 | app.use(proxy('/auth/google', { target: 'http://localhost:5000' })); 5 | app.use(proxy('/api', { target: 'http://localhost:5000' })); 6 | }; 7 | -------------------------------------------------------------------------------- /client/src/reducers/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | import authReducer from './authReducer'; 3 | import channelsReducer from './channelsReducer'; 4 | 5 | export default combineReducers({ 6 | auth: authReducer, 7 | channels: channelsReducer 8 | }); 9 | -------------------------------------------------------------------------------- /client/src/reducers/authReducer.js: -------------------------------------------------------------------------------- 1 | import { FETCH_USER } from '../actions/types'; 2 | 3 | export default function(state = null, action) { 4 | switch (action.type) { 5 | case FETCH_USER: 6 | return action.payload || false; 7 | default: 8 | return state; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /client/src/reducers/channelsReducer.js: -------------------------------------------------------------------------------- 1 | import { FETCH_CHANNELS } from '../actions/types'; 2 | 3 | export default function(state = [], action) { 4 | switch (action.type) { 5 | case FETCH_CHANNELS: 6 | return action.payload; 7 | default: 8 | return state; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /client/src/components/Landing.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | const Landing = () => { 4 | return ( 5 |
6 |

7 | Emaily! 8 |

9 | Collect feedback form your users 10 |
11 | ); 12 | }; 13 | 14 | export default Landing; 15 | -------------------------------------------------------------------------------- /client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /client/src/actions/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import { FETCH_USER, FETCH_CHANNELS } from './types'; 3 | 4 | export const fetchUser = () => async dispatch => { 5 | const res = await axios.get('/api/current_user'); 6 | 7 | dispatch({ type: FETCH_USER, payload: res.data }); 8 | }; 9 | 10 | export const fetchChannels = () => async dispatch => { 11 | const res = await axios.get('/api/channels'); 12 | 13 | dispatch({ type: FETCH_CHANNELS, payload: res.data.items }); 14 | }; 15 | -------------------------------------------------------------------------------- /client/src/index.js: -------------------------------------------------------------------------------- 1 | import 'materialize-css/dist/css/materialize.min.css'; 2 | import React from 'react'; 3 | import ReactDOM from 'react-dom'; 4 | import { Provider } from 'react-redux'; 5 | import { createStore, applyMiddleware } from 'redux'; 6 | import reduxThunk from 'redux-thunk'; 7 | 8 | import App from './components/App'; 9 | import reducers from './reducers'; 10 | 11 | const store = createStore(reducers, {}, applyMiddleware(reduxThunk)); 12 | 13 | ReactDOM.render( 14 | , 15 | document.querySelector('#root') 16 | ); 17 | -------------------------------------------------------------------------------- /routes/youtubeRoutes.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | module.exports = app => { 4 | app.get('/api/channels', async (req, res) => { 5 | if (!req.user) { 6 | res.status(403).send({ error: 'You must be logged in to do that.' }); 7 | } 8 | 9 | try { 10 | const ytRes = await axios.get( 11 | `https://www.googleapis.com/youtube/v3/channels?access_token=${ 12 | req.user.googleAccessToken 13 | }&part=snippet&mine=true` 14 | ); 15 | res.send(ytRes.data); 16 | } catch (e) { 17 | console.log(e); 18 | console.log(e.message); 19 | } 20 | }); 21 | }; 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const mongoose = require('mongoose'); 3 | const cookieSession = require('cookie-session'); 4 | const passport = require('passport'); 5 | const keys = require('./config/keys'); 6 | require('./models/User'); 7 | require('./services/passport'); 8 | 9 | mongoose.connect(keys.mongoURI); 10 | 11 | const app = express(); 12 | 13 | app.use( 14 | cookieSession({ 15 | maxAge: 30 * 24 * 60 * 60 * 1000, 16 | keys: [keys.cookieKey] 17 | }) 18 | ); 19 | app.use(passport.initialize()); 20 | app.use(passport.session()); 21 | 22 | require('./routes/authRoutes')(app); 23 | require('./routes/youtubeRoutes')(app); 24 | 25 | const PORT = process.env.PORT || 5000; 26 | app.listen(PORT); 27 | -------------------------------------------------------------------------------- /client/src/components/ChannelList.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | import { fetchChannels } from '../actions'; 4 | 5 | class ChannelList extends React.Component { 6 | componentDidMount() { 7 | this.props.fetchChannels(); 8 | } 9 | 10 | render() { 11 | return ( 12 | 19 | ); 20 | } 21 | } 22 | 23 | const mapStateToProps = state => { 24 | return { channels: state.channels }; 25 | }; 26 | 27 | export default connect( 28 | mapStateToProps, 29 | { fetchChannels } 30 | )(ChannelList); 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "engines": { 7 | "node": "10.13.0", 8 | "npm": "6.4.1" 9 | }, 10 | "scripts": { 11 | "start": "node index.js", 12 | "server": "nodemon index.js", 13 | "client": "npm run start --prefix client", 14 | "dev": "concurrently \"npm run server\" \"npm run client\"" 15 | }, 16 | "author": "", 17 | "license": "ISC", 18 | "dependencies": { 19 | "axios": "^0.18.0", 20 | "concurrently": "^4.1.0", 21 | "cookie-session": "^2.0.0-beta.3", 22 | "express": "^4.16.4", 23 | "mongoose": "^5.3.13", 24 | "nodemon": "^1.18.7", 25 | "passport": "^0.4.0", 26 | "passport-google-oauth20": "^1.0.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /routes/authRoutes.js: -------------------------------------------------------------------------------- 1 | const passport = require('passport'); 2 | 3 | module.exports = app => { 4 | app.get( 5 | '/auth/google', 6 | passport.authenticate('google', { 7 | scope: ['profile', 'email', 'https://www.googleapis.com/auth/youtube'], 8 | prompt: 'select_account' 9 | }) 10 | ); 11 | 12 | app.get('/api/delete/me', async (req, res) => { 13 | await req.user.delete(); 14 | req.logout(); 15 | res.send('deleted user'); 16 | }); 17 | 18 | app.get( 19 | '/auth/google/callback', 20 | passport.authenticate('google'), 21 | (req, res) => { 22 | res.redirect('/surveys'); 23 | } 24 | ); 25 | 26 | app.get('/api/logout', (req, res) => { 27 | req.logout(); 28 | res.redirect('/'); 29 | }); 30 | 31 | app.get('/api/current_user', (req, res) => { 32 | res.send(req.user); 33 | }); 34 | }; 35 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "/api/*": { 6 | "target": "http://localhost:5000" 7 | }, 8 | "dependencies": { 9 | "axios": "^0.18.0", 10 | "http-proxy-middleware": "^0.19.1", 11 | "materialize-css": "^1.0.0", 12 | "react": "^16.6.3", 13 | "react-dom": "^16.6.3", 14 | "react-redux": "^6.0.0", 15 | "react-router-dom": "^4.3.1", 16 | "react-scripts": "2.1.1", 17 | "redux": "^4.0.1", 18 | "redux-thunk": "^2.3.0" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test", 24 | "eject": "react-scripts eject" 25 | }, 26 | "eslintConfig": { 27 | "extends": "react-app" 28 | }, 29 | "browserslist": [ 30 | ">0.2%", 31 | "not dead", 32 | "not ie <= 11", 33 | "not op_mini all" 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /client/src/components/Header.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | import { Link } from 'react-router-dom'; 4 | 5 | class Header extends Component { 6 | renderContent() { 7 | switch (this.props.auth) { 8 | case null: 9 | return; 10 | case false: 11 | return
  • Login With Google
  • ; 12 | default: 13 | return
  • Logout
  • ; 14 | } 15 | } 16 | 17 | render() { 18 | return ( 19 | 32 | ); 33 | } 34 | } 35 | 36 | function mapStateToProps({ auth }) { 37 | return { auth }; 38 | } 39 | 40 | export default connect(mapStateToProps)(Header); 41 | -------------------------------------------------------------------------------- /services/passport.js: -------------------------------------------------------------------------------- 1 | const passport = require('passport'); 2 | const GoogleStrategy = require('passport-google-oauth20').Strategy; 3 | const mongoose = require('mongoose'); 4 | const keys = require('../config/keys'); 5 | 6 | const User = mongoose.model('users'); 7 | 8 | passport.serializeUser((user, done) => { 9 | done(null, user.id); 10 | }); 11 | 12 | passport.deserializeUser((id, done) => { 13 | User.findById(id).then(user => { 14 | done(null, user); 15 | }); 16 | }); 17 | 18 | passport.use( 19 | new GoogleStrategy( 20 | { 21 | clientID: keys.googleClientID, 22 | clientSecret: keys.googleClientSecret, 23 | callbackURL: '/auth/google/callback', 24 | proxy: true 25 | }, 26 | async (accessToken, refreshToken, profile, done) => { 27 | const existingUser = await User.findOne({ googleId: profile.id }); 28 | 29 | if (existingUser) { 30 | return done(null, existingUser); 31 | } 32 | 33 | const user = await new User({ 34 | googleId: profile.id, 35 | googleAccessToken: accessToken 36 | }).save(); 37 | done(null, user); 38 | } 39 | ) 40 | ); 41 | -------------------------------------------------------------------------------- /client/src/components/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { BrowserRouter, Route, Link } from 'react-router-dom'; 3 | import { connect } from 'react-redux'; 4 | import * as actions from '../actions'; 5 | import ChannelList from './ChannelList'; 6 | 7 | import Header from './Header'; 8 | import Landing from './Landing'; 9 | const Dashboard = () => ( 10 |
    11 |

    Dashboard

    12 | Go to Channels 13 |
    14 | ); 15 | const SurveyNew = () =>

    SurveyNew

    ; 16 | 17 | class App extends Component { 18 | componentDidMount() { 19 | this.props.fetchUser(); 20 | } 21 | 22 | render() { 23 | return ( 24 |
    25 | 26 |
    27 |
    28 | 29 | 30 | 31 | 32 |
    33 |
    34 |
    35 | ); 36 | } 37 | } 38 | 39 | export default connect( 40 | null, 41 | actions 42 | )(App); 43 | -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
    29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
    10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
    13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
    18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
    23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
    26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /client/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read http://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit http://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See http://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | --------------------------------------------------------------------------------