Skip to content
Open

Auth #30

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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*


package-lock.json
7,402 changes: 0 additions & 7,402 deletions package-lock.json

This file was deleted.

32 changes: 29 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@mikeljames/draft-js-drag-n-drop-upload-plugin": "^2.0.1",
"dd": "^0.6.0",
"draft-js": "^0.10.5",
"draft-js-alignment-plugin": "^2.0.1",
"draft-js-buttons": "^2.0.1",
"draft-js-drag-n-drop-plugin": "^2.0.1",
"draft-js-export-html": "^1.2.0",
"draft-js-focus-plugin": "^2.0.1",
"draft-js-image-plugin": "^2.0.1",
"draft-js-plugins-editor": "^2.0.4",
"draft-js-resizeable-plugin": "^2.0.1",
"draft-js-static-toolbar-plugin": "^2.0.1",
"firebase": "^4.8.2",
"firebase-admin": "^5.8.2",
"firebase-tools": "^3.17.4",
"lodash": "^4.17.4",
"node-sass": "^4.7.2",
"node-sass-chokidar": "0.0.3",
"npm-run-all": "^4.1.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-helmet": "^5.2.0",
"react-html-parser": "^2.0.2",
"react-modal": "^3.1.10",
"react-redux": "^5.0.6",
"react-redux-firebase": "^2.0.0",
"react-router-dom": "^4.2.2",
"react-scripts": "1.0.17",
"redux": "^3.7.2",
"redux-form": "^7.2.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0"
"redux-thunk": "^2.2.0",
"token-generator": "^1.0.0",
"uid-generator": "^1.0.0",
"uuid": "^3.2.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build-js": "react-scripts build",
"build": "npm-run-all build-css build-js",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
Expand Down
21 changes: 20 additions & 1 deletion src/ActionsTYPES/TYPES.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@ export const GET_ALL_GROUPS_PENDING = 'GET_ALL_GROUPS_PENDING';
export const GET_ALL_GROUPS_SUCCESS = 'GET_ALL_GROUPS_SUCCESS';
export const GET_ALL_GROUPS_REJECTED = 'GET_ALL_GROUPS_REJECTED';
export const SAVE_POST = 'SAVE_POST';
export const FETCH_POSTS = 'FETCH_POSTS';
export const FETCH_POSTS = 'FETCH_POSTS';

export const CREATE_ACCOUNT_PENDING = 'CREATE_ACCOUNT_PENDING';
export const CREATE_ACCOUNT_SUCCESS = 'CREATE_ACCOUNT_SUCCESS';
export const CREATE_ACCOUNT_REJECTED = 'CREATE_ACCOUNT_REJECTED';
export const CREATE_ACCOUNT_TO_INITIAL = 'CREATE_ACCOUNT_TO_INITIAL';

export const LOGIN_ACCOUNT_PENDING = 'LOGIN_ACCOUNT_PENDING';
export const LOGIN_ACCOUNT_SUCCESS = 'LOGIN_ACCOUNT_SUCCESS';
export const LOGIN_ACCOUNT_REJECTED = 'LOGIN_ACCOUNT_REJECTED';

export const EMAIL_SENDED = 'EMAIL_VERIFIED_SENDED';
export const EMAIL_DISPATCH_ERROR = 'EMAIL_VERIFIED_ERROR';

export const CREATE_ACCOUNT_TO_INITIAL_ERROR = 'CREATE_ACCOUNT_TO_INITIAL_ERROR'

export const EMAIL_VERIFIED_SUCCESS = 'EMAIL_VERIFIED_SUCCESS';
export const EMAIL_VERIFIED_FAIL = 'EMAIL_VERIFIED_FAIL';

export const AUTO_LOGIN_USER = 'AUTO_LOGIN_USER';
50 changes: 23 additions & 27 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
.App {
text-align: center;
}
@import url(https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,600,600i,700,700i,900|Open+Sans:300,300i,400,700);
body {
font-family: "Open Sans", sans-serif;
margin: 0; }

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
nav {
display: flex;
font-family: "Montserrat", sans-serif;
justify-content: space-between;
padding: 5px 30px;
-webkit-box-shadow: 0 1px 1px #555;
-moz-box-shadow: 0 1px 1px #555;
-ms-box-shadow: 0 1px 1px #555;
box-shadow: 0 1px 1px #555; }
nav ul li {
display: inline-block;
margin: 0 5px; }
nav ul li a {
text-decoration: none; }
nav .logo {
margin: 16px 0;
text-transform: uppercase;
font-weight: bold;
font-size: 18px; }
14 changes: 4 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { Component } from 'react';
import './App.css';
import { NavLink } from 'react-router-dom';
import Routs from './routs';

import {Helmet} from "react-helmet";
import Header from './components/Header/Header'
import { Helmet } from "react-helmet";

class App extends Component {
render() {
Expand All @@ -14,17 +13,12 @@ class App extends Component {
<title>MASA REVIEW APPLICATION</title>
<link rel="canonical" href="https://stark-atoll-57647.herokuapp.com/" />
</Helmet>
<nav>
<ul>
<li><NavLink to="/" activeClassName="selected">Home</NavLink></li>
<li><NavLink to="/programs" activeClassName="selected">Programs</NavLink></li>
<li><NavLink to="/chat" activeClassName="selected">Chat</NavLink></li>
</ul>
</nav>
<Header/>
<Routs />
</div>
);
}
}


export default App;
30 changes: 30 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@import "variables.scss";

body {
font-family: $font-open-sans;
margin: 0;
}

nav {
display: flex;
font-family: $font-monserrat;
justify-content: space-between;
padding: 5px 30px;
@include box-shadow-size(0, 1px, 1px, #555);
ul {
li {
display: inline-block;
margin: 0 5px;
a {
text-decoration: none;
}
}
}

.logo {
margin: 16px 0;
text-transform: uppercase;
font-weight: bold;
font-size: $heading;
}
}
12 changes: 12 additions & 0 deletions src/_variables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import url(https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,600,600i,700,700i,900|Open+Sans:300,300i,400,700);

$font-monserrat: 'Montserrat', sans-serif;
$font-open-sans: 'Open Sans', sans-serif;
$heading: 18px;

@mixin box-shadow-size($x, $y, $radius, $color) {
-webkit-box-shadow: $x $y $radius $color;
-moz-box-shadow: $x $y $radius $color;
-ms-box-shadow: $x $y $radius $color;
box-shadow: $x $y $radius $color;
}
128 changes: 128 additions & 0 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import {
CREATE_ACCOUNT_PENDING,
CREATE_ACCOUNT_SUCCESS,
CREATE_ACCOUNT_REJECTED,
CREATE_ACCOUNT_TO_INITIAL,
CREATE_ACCOUNT_TO_INITIAL_ERROR,
LOGIN_ACCOUNT_PENDING,
LOGIN_ACCOUNT_REJECTED,
LOGIN_ACCOUNT_SUCCESS,
EMAIL_VERIFIED_SUCCESS,
EMAIL_VERIFIED_FAIL,
AUTO_LOGIN_USER

} from '../ActionsTYPES/TYPES';
import { createFirebaseConnect } from 'react-redux-firebase';
import uuidv1 from 'uuid/v1';

export const createAccount = (email, password) =>
(dispatch, getState, getFirebase) => {
const firebase = getFirebase();

dispatch({ type: CREATE_ACCOUNT_PENDING });

firebase.auth().createUserWithEmailAndPassword(email, password)
.then((userData) => {

console.log(userData)
//localStorage.setItem("user", JSON.stringify({ email, "registration": true, "login": false }))
dispatch({ type: CREATE_ACCOUNT_SUCCESS, payload: userData });
})
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
dispatch({ type: CREATE_ACCOUNT_REJECTED, payload: errorMessage });
});
}

export const loginViaFirebase = (email, password) =>
(dispatch, getState, getFirebase) => {
const firebase = getFirebase();

dispatch({ type: LOGIN_ACCOUNT_PENDING });

firebase.auth().signInWithEmailAndPassword(email, password)

.then((userData)=>{

// add this user to local storage

//localStorage.setItem("user", JSON.stringify({ email, "registration": true, "login": false }))
dispatch({ type: LOGIN_ACCOUNT_SUCCESS, payload: userData})

})

.then(() => {
if (firebase.auth().currentUser.emailVerified) {

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function (idToken) {
const userRoute = email.split('@')[0]
firebase.database().ref(`users/${userRoute}`)
.set({email, token: idToken})
.then(()=>{
localStorage.setItem("userToken", JSON.stringify({ token: idToken, userRoute }))
})
.then(()=>{
dispatch({type: EMAIL_VERIFIED_SUCCESS, payload: email});
})
})
} else {


dispatch({type: EMAIL_VERIFIED_FAIL});

}
})
.catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
dispatch({ type: LOGIN_ACCOUNT_REJECTED, payload: errorMessage })
});
}


export const createAccountToInitial = () =>
(dispatch, getState, getFirebase) => {
const firebase = getFirebase();

firebase.auth().signOut()

.then(function() {
localStorage.removeItem("userToken")
dispatch({ type: CREATE_ACCOUNT_TO_INITIAL });
})
.catch(function(error) {
dispatch({ type: CREATE_ACCOUNT_TO_INITIAL_ERROR, payload: error});
});
}

export const verifyLogin = (userToken) =>
(dispatch, getState, getFirebase) => {
const firebase = getFirebase();

const users = firebase.database().ref('users/')
users.once('value', function(snapshot) {
if(userToken){
const userData = snapshot.val()[userToken.userRoute];
if (userToken.token === userData.token){
dispatch({ type: AUTO_LOGIN_USER, payload: userData.email})

const email = userData.email;
const userRoute = email.split('@')[0]
const token = uuidv1();

firebase.database().ref(`users/${userRoute}`)
.set({ email, token })
localStorage.setItem("userToken", JSON.stringify({ token, userRoute }))

}
}
});
}

export default {
createAccount,
createAccountToInitial
}
29 changes: 22 additions & 7 deletions src/actions/events.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SAVE_POST, FETCH_POSTS } from '../ActionsTYPES/TYPES'
import { SAVE_POST, FETCH_POSTS, EMAIL_SENDED, EMAIL_DISPATCH_ERROR } from '../ActionsTYPES/TYPES'


export const savePost = (post) =>
(dispatch, getState, getFirebase) => {
const firebase = getFirebase()
firebase.database().ref(`groups/${post.groupTitle}/posts/${post.postIndex}`)
.set(post)
firebase.database().ref(`groups/${post.groupTitle}/posts`)
.push(post)
.then(() => {
dispatch({ type: SAVE_POST, payload: 'Success' })
})
Expand All @@ -14,12 +14,27 @@ export const savePost = (post) =>
})
}

export const fetchPosts = () => (dispatch, getState, getFirebase) => {
export const fetchPosts = (groupTitle) => (dispatch, getState, getFirebase) => {
const firebase = getFirebase()
const posts = firebase.database().ref('groups/')
const posts = firebase.database().ref(`groups/${groupTitle}/posts`)

posts.on('value', function (snapshot) {
console.log(snapshot.val())
dispatch({ type: FETCH_POSTS, payload: snapshot.val() || [] })
});
};
};

export const authListener = () => (dispatch, getState, getFirebase) => {
const firebase = getFirebase()
firebase.auth().onAuthStateChanged(function (user) {
if(user && !user.emailVerified){
user.sendEmailVerification()
.then(function () {
dispatch({ type:EMAIL_SENDED });
}).catch(function (error) {
dispatch({ type:EMAIL_DISPATCH_ERROR, payload: error });
});
}
});
}


Loading