Skip to content

feat(auth): add firebase auth to site #19

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ['react', 'es2015', 'stage-0'],
"plugins": ['add-module-exports']
"presets": ["react", "es2015", "stage-0"],
"plugins": ["add-module-exports"]
}
30 changes: 24 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
{
"extends": "eslint-config-airbnb",
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
"indent": [1, 4, {"SwitchCase": 1}],
"no-console": [0],
"func-names": [0],
"semi": [2, "never"],
"no-extra-semi": [2],
"space-before-function-paren": [2, "always"],
"semi": [0],
"no-extra-semi": [1],
"space-before-function-paren": [0],
"no-else-return": [0],
"space-infix-ops": [0],
"react/prefer-es6-class": [0],
"react/prefer-stateless-function": [0],
"import/no-unresolved": [0],
"global-require": [0],
"import/no-extraneous-dependencies": [0],
"no-unused-vars": [1],
"max-len": [1],
"jsx-quotes": [1],
"jsx-a11y/img-has-alt": [1],
"jsx-a11y/img-redundant-alt": [1],
"comma-dangle": [1],
"react/jsx-closing-bracket-location": [0],
"eqeqeq": [0],
"react/jsx-indent": [1],
"quotes": [1],
"react/jsx-curly-spacing": [1],
"no-use-before-define": [0]
},
"globals": {
"__PREFIX_LINKS__": true,
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"globals": {
"__PREFIX_LINKS__": true
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pages/.manifest
.idea
gatsby-starter-lumen.iml
.vscode/
public

npm-debug.log
18 changes: 0 additions & 18 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Develop",
"program": "${workspaceRoot}/node_modules/gatsby/bin/gatsby.js",
"cwd": "${workspaceRoot}",
"args": ["develop"],
"sourceMaps": true
},
{
"type": "node",
"request": "launch",
Expand All @@ -22,15 +13,6 @@
"args": ["build"],
"sourceMaps": true
},
{
"type": "node",
"request": "launch",
"name": "Serve Public",
"program": "${workspaceRoot}/node_modules/gatsby/bin/gatsby.js",
"cwd": "${workspaceRoot}",
"args": ["serve-build"],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
Expand Down
98 changes: 98 additions & 0 deletions components/Auth/firebaseHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import firebase from 'firebase';

let token;
let user;
let provider;
let credential;

let strategy = 'popup';

function gitHubRedirectLogin() {
firebase.auth()
.getRedirectResult()
.then(handleSigninSuccess)
.catch(handleSignInErr);

provider = new firebase.auth.GithubAuthProvider();
provider.addScope('repo');
firebase.auth().signInWithRedirect(provider);
}

function gitHubPopupLogin() {
provider = new firebase.auth.GithubAuthProvider();
provider.addScope('repo');
firebase.auth()
.signInWithPopup(provider)
.then(handleSigninSuccess)
.catch(handleSignInErr);
}

// function createUser(email, password) {
// return firebase.auth().createUserWithEmailAndPassword(
// email,
// password
// )
// .then(result => {
// user = result;
// return user;
// })
// .catch(handleSignInErr);
// }

// function emailLogin(email, password) {
// credential = firebase.auth.EmailAuthProvider.credential(
// email,
// password
// );
// }

function getUser() {
if (user) {
return user;
}

return false;
}

function getToken() {
if (token) {
return token;
}

return false;
}

function handleSigninSuccess(result) {
if (result.credential) {
token = result.credential.accessToken;
credential = result.credential;
}

user = result.user;
return user;
}

function handleSignInErr(err) {
const errorCode = err.code;
const errorMessage = err.message;
const email = err.email;
credential = err.credential;

if (errorCode == 'auth/account-exists-with-different-credentials') {
console.warn('You already signed up with a different ' +
'authentication provider for that email');
}
console.error(err);
}

function changeLoginStrategy(newStrategy) {
strategy = newStrategy;
}

export {
gitHubRedirectLogin,
gitHubPopupLogin,
// emailLogin,
getUser,
getToken,
};
7 changes: 7 additions & 0 deletions components/Auth/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import './initializeFirebase.js';

import * as auth from './firebaseHelper';

export {
auth,
}
12 changes: 12 additions & 0 deletions components/Auth/initializeFirebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import firebase from 'firebase';

const config = {
apiKey: 'AIzaSyDayBqMwkiAQ76WHCfGWS7ZHqJYro_Ofw0',
databaseURL: 'https://dashcommunity-blog.firebaseio.com',
authDomain: 'dashcommunity-blog.firebaseapp.com',
projectId: 'dashcommunity-blog',
storageBucket: 'dashcommunity-blog.appspot.com',
messagingSenderId: '451048266119',
};

firebase.initializeApp(config);
58 changes: 27 additions & 31 deletions components/ComposePage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import React from 'react'
import DocumentTitle from 'react-document-title'
import { RouteHandler, Link } from 'react-router'
import SiteSidebar from '../SiteSidebar'
import { config } from 'config'
import { rawDefaultEditorContent } from '../Editor/defaultEditorContent'
import { ContentState, convertToRaw } from 'draft-js'
import GitHubForm from '../Forms/GitHubForm'
import MyEditor from '../Editor'
import React from 'react';
import SiteSidebar from '../SiteSidebar';
import { rawDefaultEditorContent } from '../Editor/defaultEditorContent';
import GitHubForm from '../Forms/GitHubForm';
import MyEditor from '../Editor';

export default class ComposePage extends React.Component {
constructor () {
super()
this.state = {editorContent: rawDefaultEditorContent};
}

render () {
const passEditorContent = (editorContent) => {// https://www.youtube.com/watch?v=5Xew--ycx0o&list=PL55RiY5tL51oyA8euSROLjMFZbXaV7skS&index=4#t=165.980087
console.log('editorContent in ComposePage:', editorContent)
this.setState({editorContent: editorContent})
constructor () {
super();
this.state = { editorContent: rawDefaultEditorContent };
}
return (
<div>
<SiteSidebar {...this.props}/>
<div className='content'>
<div className='main'>
<div className='main-inner'>
<MyEditor editorContent={this.state.editorContent}/>
<GitHubForm passContent={passEditorContent.bind(this)}/>

render () {
const passEditorContent = (editorContent) => { // https://www.youtube.com/watch?v=5Xew--ycx0o&list=PL55RiY5tL51oyA8euSROLjMFZbXaV7skS&index=4#t=165.980087
console.log('editorContent in ComposePage:', editorContent);
this.setState({ editorContent });
}
return (
<div>
<SiteSidebar {...this.props} />
<div className="content">
<div className="main">
<div className="main-inner">
<MyEditor editorContent={this.state.editorContent} />
<GitHubForm passContent={passEditorContent} />
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
);
}
}
Loading