Skip to content

Commit

Permalink
refactor #14: Moved localstorage to loginPage
Browse files Browse the repository at this point in the history
  • Loading branch information
Boosmith committed Sep 2, 2019
1 parent dcfc83c commit d94ecb3
Show file tree
Hide file tree
Showing 11 changed files with 15,647 additions and 17,390 deletions.
32,928 changes: 15,554 additions & 17,374 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
"extends": "react-app"
},
"dependencies": {
"@babel/core": "^7.5.5",
"@babel/register": "^7.5.5",
"@material-ui/core": "^4.4.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react-app": "^9.0.1",
"bcrypt": "^3.0.6",
"bootstrap": "^4.3.1",
"bson": "^4.0.2",
"dotenv": "^8.1.0",
"express": "^4.17.1",
"ignore-styles": "^5.0.1",
"immer": "^2.1.5",
"jquery": "^3.4.1",
"jsonwebtoken": "^8.5.1",
"popper.js": "^1.15.0",
"prop-types": "^15.7.2",
"react": "^16.9.0",
Expand All @@ -35,7 +43,7 @@
"redux-thunk": "^2.3.0",
"remarkable": "^2.0.0",
"reselect": "^4.0.0",
"typescript": "^3.5.3"
"typescript": "^3.6.2"
},
"devDependencies": {
"cssnano": "^4.1.10",
Expand Down
8 changes: 8 additions & 0 deletions server/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require("ignore-styles");

require("@babel/register")({
ignore: [/(node_modules)/],
presets: ["@babel/preset-env", "react-app"]
});

require("./index");
31 changes: 31 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import express from "express";

// we'll talk about this in a minute:
import serverRenderer from "./middleware/renderer";

const PORT = 3000;
const path = require("path");

// initialize the application and create the routes
const app = express();
const router = express.Router();

// root (/) should always serve our server rendered page
router.use("^/$", serverRenderer);

// other static resources should just be served as they are
router.use(
express.static(path.resolve(__dirname, "..", "build"), { maxAge: "30d" })
);

// tell the app to use the above rules
app.use(router);

// start the app
app.listen(PORT, error => {
if (error) {
return console.log("something bad happened", error);
}

console.log("listening on " + PORT + "...");
});
28 changes: 28 additions & 0 deletions server/middleware/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import ReactDOMServer from "react-dom/server";

// import our main App component
import App from "../../src/components/App";

const path = require("path");
const fs = require("fs");

export default (req, res, next) => {
// point to the html file created by CRA's build tool
const filePath = path.resolve(__dirname, "..", "..", "build", "index.html");

fs.readFile(filePath, "utf8", (err, htmlData) => {
if (err) {
console.error("err", err);
return res.status(404).end();
}

// render the app as a string
const html = ReactDOMServer.renderToString(<App />);

// inject the rendered app into our html and send it
return res.send(
htmlData.replace('<div id="root"></div>', `<div id="root">${html}</div>`)
);
});
};
5 changes: 2 additions & 3 deletions src/api/authApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ function login(userName, password) {

return fetch(baseUrl + "/login", requestOptions)
.then(handleResponse)
.then(auth => {
localStorage.setItem("trelloid_token", JSON.stringify(auth.token));
return auth.user;
.then(({ token, user }) => {
return { token, user };
})
.catch(handleError);
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/auth/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class LoginPage extends Component {
super(props, context);

this.state = {
auth: {},
user: {},
errors: {},
saving: false
Expand Down Expand Up @@ -43,7 +44,9 @@ class LoginPage extends Component {
this.setState({ saving: true });
this.props.actions
.login(this.state.user)
.then(() => {
.then(auth => {
this.setState({ auth: auth });
localStorage.setItem("jwt_auth", JSON.stringify(auth));
this.props.history.push("/");
})
.catch(error => {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { renderToString } from "react-dom/server";
import { render } from "react-dom";
import { BrowserRouter as Router } from "react-router-dom";
import App from "./components/App";
import { Provider } from "react-redux";
Expand All @@ -11,7 +11,7 @@ import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
const store = configureStore();
store.dispatch(loadUsers());

renderToString(
render(
<Provider store={store}>
<Router>
<App />
Expand Down
14 changes: 7 additions & 7 deletions src/redux/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export function loginRequest(user) {
return { type: actionTypes.LOGIN_REQUEST, user: user };
}

export function loginSuccess(user) {
return { type: actionTypes.LOGIN_SUCCESS, user: user };
export function loginSuccess(auth) {
return { type: actionTypes.LOGIN_SUCCESS, auth: auth };
}

export function logoutSuccess(user) {
return { type: actionTypes.LOGOUT_SUCCESS };
}

export function login(user) {
export function login(loginDetails) {
return function(dispatch) {
dispatch(beginApiCall());
dispatch(loginRequest({ user }));
const { userName, password } = user;
dispatch(loginRequest({ loginDetails }));
const { userName, password } = loginDetails;
return authApi
.login(userName, password)
.then(() => {
dispatch(loginSuccess(user));
.then(({ token, user }) => {
dispatch(loginSuccess({ token, user }));
})
.catch(err => {
dispatch(loginFailure(err));
Expand Down
2 changes: 1 addition & 1 deletion src/redux/reducers/authReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function authentication(state = {}, action) {
case actionTypes.LOGIN_REQUEST:
return { loggingIn: true, user: action.user };
case actionTypes.LOGIN_SUCCESS:
return { loggedIn: true, user: action.user };
return { loggedIn: true, auth: action.auth };
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/apiServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server.post("/auth/login", function(req, res, next) {
const user = isAuthenticated(userName, password);
if (user) {
const tokenValue = createToken({ userName, password });
res.status(200).json({ token: tokenValue, user });
res.status(200).json({ token: tokenValue, user: user });
} else {
const status = 401;
const message = "Incorrect email or password";
Expand Down

0 comments on commit d94ecb3

Please sign in to comment.