mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 18:28:47 +00:00
Still working on Auth State
This commit is contained in:
parent
dcd03d7888
commit
5df28e0e77
@ -1,30 +1,34 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from "react";
|
||||||
|
|
||||||
import './App.css';
|
import "./App.css";
|
||||||
|
|
||||||
import { BrowserRouter as Router } from 'react-router-dom';
|
import { BrowserRouter as Router } from "react-router-dom";
|
||||||
import Route from 'react-router-dom/Route';
|
import Route from "react-router-dom/Route";
|
||||||
import Navbar from './components/layout/NavBar';
|
import Navbar from "./components/layout/NavBar";
|
||||||
import jwtDecode from 'jwt-decode';
|
import jwtDecode from "jwt-decode";
|
||||||
|
|
||||||
|
// Redux
|
||||||
|
import { Provider } from "react-redux";
|
||||||
|
import store from "./redux/store";
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import AuthRoute from './util/AuthRoute';
|
import AuthRoute from "./util/AuthRoute";
|
||||||
|
|
||||||
import home from './pages/Home';
|
import home from "./pages/Home";
|
||||||
import register from './pages/Register';
|
import register from "./pages/Register";
|
||||||
import login from './pages/Login';
|
import login from "./pages/Login";
|
||||||
import user from './pages/user';
|
import user from "./pages/user";
|
||||||
import writeMicroblog from './Writing_Microblogs.js';
|
import writeMicroblog from "./Writing_Microblogs.js";
|
||||||
import edit from './pages/edit.js';
|
import edit from "./pages/edit.js";
|
||||||
import userLine from './Userline.js';
|
import userLine from "./Userline.js";
|
||||||
|
|
||||||
let authenticated;
|
let authenticated;
|
||||||
const token = localStorage.FBIdToken;
|
const token = localStorage.FBIdToken;
|
||||||
if (token) {
|
if (token) {
|
||||||
const decodedToken = jwtDecode(token);
|
const decodedToken = jwtDecode(token);
|
||||||
if (decodedToken.exp * 1000 < Date.now()) {
|
if (decodedToken.exp * 1000 < Date.now()) {
|
||||||
window.location.href = '/login';
|
window.location.href = "/login";
|
||||||
authenticated = false;
|
authenticated = false;
|
||||||
} else {
|
} else {
|
||||||
authenticated = true;
|
authenticated = true;
|
||||||
@ -34,21 +38,37 @@ if (token) {
|
|||||||
class App extends Component {
|
class App extends Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
|
<Provider store={store}>
|
||||||
<Router>
|
<Router>
|
||||||
<div className='container' >
|
<div className="container">
|
||||||
<Navbar />
|
<Navbar />
|
||||||
</div>
|
</div>
|
||||||
<div className="app">
|
<div className="app">
|
||||||
<AuthRoute exact path="/" component={home} authenticated={authenticated}/>
|
<AuthRoute
|
||||||
<AuthRoute exact path="/register" component={register} authenticated={authenticated}/>
|
exact
|
||||||
<AuthRoute exact path="/login" component={login} authenticated={authenticated}/>
|
path="/"
|
||||||
<Route exact path="/user" component={user}/>
|
component={home}
|
||||||
<Route exact path="/home" component={writeMicroblog}/>
|
authenticated={authenticated}
|
||||||
<Route exact path="/edit" component={edit}/>
|
/>
|
||||||
<Route exact path="/userline" component={userLine}/>
|
<AuthRoute
|
||||||
|
exact
|
||||||
|
path="/register"
|
||||||
|
component={register}
|
||||||
|
authenticated={authenticated}
|
||||||
|
/>
|
||||||
|
<AuthRoute
|
||||||
|
exact
|
||||||
|
path="/login"
|
||||||
|
component={login}
|
||||||
|
authenticated={authenticated}
|
||||||
|
/>
|
||||||
|
<Route exact path="/user" component={user} />
|
||||||
|
<Route exact path="/home" component={writeMicroblog} />
|
||||||
|
<Route exact path="/edit" component={edit} />
|
||||||
|
<Route exact path="/userline" component={userLine} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</Router>
|
</Router>
|
||||||
|
</Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
|
import * as serviceWorker from './serviceWorker';
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<App />,
|
<App />,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
serviceWorker.unregister();
|
||||||
@ -11,6 +11,10 @@ import TextField from "@material-ui/core/TextField";
|
|||||||
import Typography from "@material-ui/core/Typography";
|
import Typography from "@material-ui/core/Typography";
|
||||||
import withStyles from "@material-ui/core/styles/withStyles";
|
import withStyles from "@material-ui/core/styles/withStyles";
|
||||||
|
|
||||||
|
// Redux stuff
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
import { loginUser } from '../redux/actions/userActions';
|
||||||
|
|
||||||
const styles = {
|
const styles = {
|
||||||
form: {
|
form: {
|
||||||
textAlign: "center"
|
textAlign: "center"
|
||||||
@ -55,40 +59,26 @@ export class Login extends Component {
|
|||||||
this.state = {
|
this.state = {
|
||||||
email: "",
|
email: "",
|
||||||
password:"",
|
password:"",
|
||||||
loading: false,
|
|
||||||
errors: {}
|
errors: {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentWillReceiveProps(nextProps) {
|
||||||
|
if (nextProps.UI.errors) {
|
||||||
|
this.setState({ errors: nextProps.UI.errors });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Runs whenever the submit button is clicked.
|
// Runs whenever the submit button is clicked.
|
||||||
// Updates the database entry of the signed in user with the
|
// Updates the database entry of the signed in user with the
|
||||||
// data stored in the state.
|
// data stored in the state.
|
||||||
handleSubmit = (event) => {
|
handleSubmit = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this.setState({
|
|
||||||
loading: true
|
|
||||||
});
|
|
||||||
const loginData = {
|
const loginData = {
|
||||||
email: this.state.email,
|
email: this.state.email,
|
||||||
password: this.state.password,
|
password: this.state.password,
|
||||||
};
|
};
|
||||||
axios
|
this.props.loginUser(loginData, this.props.history);
|
||||||
.post("/login", loginData)
|
|
||||||
.then((res) => {
|
|
||||||
// Save the login token
|
|
||||||
localStorage.setItem('FBIdToken', `Bearer ${res.data.token}`);
|
|
||||||
this.setState({
|
|
||||||
loading: false
|
|
||||||
});
|
|
||||||
// Redirects to home page
|
|
||||||
this.props.history.push('/home');
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
this.setState({
|
|
||||||
errors: err.response.data,
|
|
||||||
loading: false
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Updates the state whenever one of the textboxes changes.
|
// Updates the state whenever one of the textboxes changes.
|
||||||
@ -104,8 +94,8 @@ export class Login extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { classes } = this.props;
|
const { classes, UI: { loading } } = this.props;
|
||||||
const { errors, loading } = this.state;
|
const { errors } = this.state;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid container className={classes.form}>
|
<Grid container className={classes.form}>
|
||||||
@ -165,7 +155,19 @@ export class Login extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Login.propTypes = {
|
Login.propTypes = {
|
||||||
classes: PropTypes.object.isRequired
|
classes: PropTypes.object.isRequired,
|
||||||
|
loginUser: PropTypes.func.isRequired,
|
||||||
|
user: PropTypes.object.isRequired,
|
||||||
|
UI: PropTypes.object.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
export default withStyles(styles)(Login);
|
const mapStateToProps = (state) => ({
|
||||||
|
user: state.user,
|
||||||
|
UI: state.UI,
|
||||||
|
});
|
||||||
|
|
||||||
|
const mapActionsToProps = {
|
||||||
|
loginUser
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect(mapStateToProps, mapActionsToProps)(withStyles(styles)(Login));
|
||||||
|
|||||||
0
twistter-frontend/src/redux/actions/dataActions.js
Normal file
0
twistter-frontend/src/redux/actions/dataActions.js
Normal file
35
twistter-frontend/src/redux/actions/userActions.js
Normal file
35
twistter-frontend/src/redux/actions/userActions.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI} from '../types';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
export const loginUser = (loginData, history) => (dispatch) => {
|
||||||
|
dispatch({ type: LOADING_UI });
|
||||||
|
axios
|
||||||
|
.post("/login", loginData)
|
||||||
|
.then((res) => {
|
||||||
|
// Save the login token
|
||||||
|
const FBIdToken = `Bearer ${res.data.token}`;
|
||||||
|
localStorage.setItem('FBIdToken', FBIdToken);
|
||||||
|
axios.defaults.headers.common['Authorization'] = FBIdToken;
|
||||||
|
dispatch(getProfileInfo());
|
||||||
|
dispatch({ type: CLEAR_ERRORS })
|
||||||
|
// Redirects to home page
|
||||||
|
history.push('/home');
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
dispatch ({
|
||||||
|
type: SET_ERRORS,
|
||||||
|
payload: err.response.data,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getProfileInfo = () => (dispatch) => {
|
||||||
|
axios.get('/getProfileInfo')
|
||||||
|
.then((res) => {
|
||||||
|
dispatch({
|
||||||
|
type: SET_USER,
|
||||||
|
payload: res.data,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch((err) => console.error(err));
|
||||||
|
}
|
||||||
0
twistter-frontend/src/redux/reducers/dataReducer.js
Normal file
0
twistter-frontend/src/redux/reducers/dataReducer.js
Normal file
30
twistter-frontend/src/redux/reducers/uiReducer.js
Normal file
30
twistter-frontend/src/redux/reducers/uiReducer.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { SET_ERRORS, CLEAR_ERRORS, LOADING_UI } from '../types';
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
loading: false,
|
||||||
|
errors: null
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function(state = initialState, action) {
|
||||||
|
switch(action.type) {
|
||||||
|
case SET_ERRORS:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
loading: false,
|
||||||
|
errors: action.payload
|
||||||
|
};
|
||||||
|
case CLEAR_ERRORS:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
loading: false,
|
||||||
|
errors: null
|
||||||
|
};
|
||||||
|
case LOADING_UI:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
loading: true
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
twistter-frontend/src/redux/reducers/userReducer.js
Normal file
28
twistter-frontend/src/redux/reducers/userReducer.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from '../types';
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
authenticated: false,
|
||||||
|
credentials: {},
|
||||||
|
likes: [],
|
||||||
|
notifications: []
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function(state = initialState, action) {
|
||||||
|
switch(action.type) {
|
||||||
|
case SET_AUTHENTICATED:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
authenticated: true,
|
||||||
|
|
||||||
|
};
|
||||||
|
case SET_UNAUTHENTICATED:
|
||||||
|
return initialState;
|
||||||
|
case SET_USER:
|
||||||
|
return {
|
||||||
|
authenticated: true,
|
||||||
|
...action.payload,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
twistter-frontend/src/redux/store.js
Normal file
27
twistter-frontend/src/redux/store.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
|
||||||
|
import thunk from "redux-thunk";
|
||||||
|
|
||||||
|
import userReducer from "./reducers/userReducer";
|
||||||
|
import dataReducer from "./reducers/dataReducer";
|
||||||
|
import uiReducer from "./reducers/uiReducer";
|
||||||
|
|
||||||
|
const initialState = {};
|
||||||
|
|
||||||
|
const middleWare = [thunk];
|
||||||
|
|
||||||
|
const reducers = combineReducers({
|
||||||
|
user: userReducer,
|
||||||
|
data: dataReducer,
|
||||||
|
UI: uiReducer
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = createStore(
|
||||||
|
reducers,
|
||||||
|
initialState,
|
||||||
|
compose(
|
||||||
|
applyMiddleware(...middleWare),
|
||||||
|
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
export default store;
|
||||||
12
twistter-frontend/src/redux/types.js
Normal file
12
twistter-frontend/src/redux/types.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// User reducer types
|
||||||
|
export const SET_AUTHENTICATED = 'SET_AUTHENTICATED';
|
||||||
|
export const SET_UNAUTHENTICATED = 'SET_UNAUTHENTICATED';
|
||||||
|
export const SET_USER = 'SET_USER';
|
||||||
|
export const LOADING_USER = 'LOADING_USER';
|
||||||
|
|
||||||
|
// UI reducer types
|
||||||
|
export const SET_ERRORS = 'SET_ERRORS';
|
||||||
|
export const LOADING_UI = 'LOADING_UI';
|
||||||
|
export const CLEAR_ERRORS = 'CLEAR_ERRORS';
|
||||||
|
|
||||||
|
// Data reducer types
|
||||||
135
twistter-frontend/src/serviceWorker.js
Normal file
135
twistter-frontend/src/serviceWorker.js
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
// This optional code is used to register a service worker.
|
||||||
|
// register() is not called by default.
|
||||||
|
|
||||||
|
// This lets the app load faster on subsequent visits in production, and gives
|
||||||
|
// it offline capabilities. However, it also means that developers (and users)
|
||||||
|
// will only see deployed updates on subsequent visits to a page, after all the
|
||||||
|
// existing tabs open on the page have been closed, since previously cached
|
||||||
|
// resources are updated in the background.
|
||||||
|
|
||||||
|
// To learn more about the benefits of this model and instructions on how to
|
||||||
|
// opt-in, read https://bit.ly/CRA-PWA
|
||||||
|
|
||||||
|
const isLocalhost = Boolean(
|
||||||
|
window.location.hostname === 'localhost' ||
|
||||||
|
// [::1] is the IPv6 localhost address.
|
||||||
|
window.location.hostname === '[::1]' ||
|
||||||
|
// 127.0.0.1/8 is considered localhost for IPv4.
|
||||||
|
window.location.hostname.match(
|
||||||
|
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
export function register(config) {
|
||||||
|
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||||
|
// The URL constructor is available in all browsers that support SW.
|
||||||
|
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
|
||||||
|
if (publicUrl.origin !== window.location.origin) {
|
||||||
|
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||||
|
// from what our page is served on. This might happen if a CDN is used to
|
||||||
|
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('load', () => {
|
||||||
|
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||||
|
|
||||||
|
if (isLocalhost) {
|
||||||
|
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||||
|
checkValidServiceWorker(swUrl, config);
|
||||||
|
|
||||||
|
// Add some additional logging to localhost, pointing developers to the
|
||||||
|
// service worker/PWA documentation.
|
||||||
|
navigator.serviceWorker.ready.then(() => {
|
||||||
|
console.log(
|
||||||
|
'This web app is being served cache-first by a service ' +
|
||||||
|
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Is not localhost. Just register service worker
|
||||||
|
registerValidSW(swUrl, config);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerValidSW(swUrl, config) {
|
||||||
|
navigator.serviceWorker
|
||||||
|
.register(swUrl)
|
||||||
|
.then(registration => {
|
||||||
|
registration.onupdatefound = () => {
|
||||||
|
const installingWorker = registration.installing;
|
||||||
|
if (installingWorker == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
installingWorker.onstatechange = () => {
|
||||||
|
if (installingWorker.state === 'installed') {
|
||||||
|
if (navigator.serviceWorker.controller) {
|
||||||
|
// At this point, the updated precached content has been fetched,
|
||||||
|
// but the previous service worker will still serve the older
|
||||||
|
// content until all client tabs are closed.
|
||||||
|
console.log(
|
||||||
|
'New content is available and will be used when all ' +
|
||||||
|
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Execute callback
|
||||||
|
if (config && config.onUpdate) {
|
||||||
|
config.onUpdate(registration);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// At this point, everything has been precached.
|
||||||
|
// It's the perfect time to display a
|
||||||
|
// "Content is cached for offline use." message.
|
||||||
|
console.log('Content is cached for offline use.');
|
||||||
|
|
||||||
|
// Execute callback
|
||||||
|
if (config && config.onSuccess) {
|
||||||
|
config.onSuccess(registration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error during service worker registration:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkValidServiceWorker(swUrl, config) {
|
||||||
|
// Check if the service worker can be found. If it can't reload the page.
|
||||||
|
fetch(swUrl)
|
||||||
|
.then(response => {
|
||||||
|
// Ensure service worker exists, and that we really are getting a JS file.
|
||||||
|
const contentType = response.headers.get('content-type');
|
||||||
|
if (
|
||||||
|
response.status === 404 ||
|
||||||
|
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||||
|
) {
|
||||||
|
// No service worker found. Probably a different app. Reload the page.
|
||||||
|
navigator.serviceWorker.ready.then(registration => {
|
||||||
|
registration.unregister().then(() => {
|
||||||
|
window.location.reload();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Service worker found. Proceed as normal.
|
||||||
|
registerValidSW(swUrl, config);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
console.log(
|
||||||
|
'No internet connection found. App is running in offline mode.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unregister() {
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.ready.then(registration => {
|
||||||
|
registration.unregister();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,11 +1,22 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { Route, Redirect} from 'react-router-dom';
|
import { Route, Redirect} from 'react-router-dom';
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
const AuthRoute = ({ component: Component, authenticated, ...rest}) => (
|
const AuthRoute = ({ component: Component, authenticated, ...rest }) => (
|
||||||
<Route
|
<Route
|
||||||
{...rest}
|
{...rest}
|
||||||
render = {(props) => authenticated === true ? <Redirect to='/home'/> : <Component {...props} />}
|
render={(props) =>
|
||||||
|
authenticated === true ? <Redirect to='/home'/> : <Component {...props} />}
|
||||||
/>
|
/>
|
||||||
)
|
);
|
||||||
|
|
||||||
export default AuthRoute;
|
const mapStateToProps = (state) => ({
|
||||||
|
authenticated: state.user.authenticated
|
||||||
|
});
|
||||||
|
|
||||||
|
AuthRoute.propTypes = {
|
||||||
|
user: PropTypes.object
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect(mapStateToProps)(AuthRoute);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user