Start on fixing up the login manager.

This commit is contained in:
Dorian 2016-08-08 09:03:28 -04:00
parent 21eae48bf7
commit 089a9f9840
6 changed files with 46 additions and 105 deletions

View File

@ -33,6 +33,12 @@ export function fetchArticle(articleId='landing') {
console.info(`Loading... ${articleId}`); console.info(`Loading... ${articleId}`);
return fetch(`/app/pages/${articleId}`) return fetch(`/app/pages/${articleId}`)
.then((response) => { .then((response) => {
// TODO: Add better error handling.
// if (response.ok) {
// return response.json();
// } else {
// console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
// }
return response.json(); return response.json();
}) })
.then((json) => { .then((json) => {
@ -54,6 +60,12 @@ export function fetchNavigationMenu() {
return (dispatch) => { return (dispatch) => {
return fetch("/app/menu") return fetch("/app/menu")
.then((response) => { .then((response) => {
// TODO: Add better error handling.
// if (response.ok) {
// return response.json();
// } else {
// console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
// }
return response.json(); return response.json();
}) })
.then((json) => { .then((json) => {
@ -66,6 +78,14 @@ export function fetchAppSiteInfo() {
return (dispatch) => { return (dispatch) => {
return fetch("/app/config") return fetch("/app/config")
.then((response) => { .then((response) => {
// TODO: Add better error handling.
// if (response.ok) {
// return response.json();
// } else {
// console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
// }
return response.json(); return response.json();
}) })
.then((json) => { .then((json) => {
@ -125,13 +145,6 @@ export function loadNavigation(menu=[]) {
}; };
} }
export function notifyUserLogin(user) {
return {
type: LOGIN_USER_ACTION,
user: user
};
}
export function switchThemes() { export function switchThemes() {
let switchedThemes = ThemeManager.switchTheme(); let switchedThemes = ThemeManager.switchTheme();
return { return {

View File

@ -9,8 +9,8 @@ Rookeries client app
import React from "react"; import React from "react";
import {connect} from "react-redux"; import {connect} from "react-redux";
// import {UserLoginView} from "../views/user_login_modal"; import {UserLoginView} from "../views/user_login_modal";
import {ThemeSwitchView, ConnectedThemeSwitchView} from "../views/theme_switcher_button"; import {ConnectedThemeSwitchView} from "../views/theme_switcher_button";
import {SiteHeader} from "../views/site_header"; import {SiteHeader} from "../views/site_header";
import {SiteFooter} from "../views/site_footer"; import {SiteFooter} from "../views/site_footer";
@ -33,7 +33,7 @@ class App extends React.Component {
{/*TODO Extract as header container.*/} {/*TODO Extract as header container.*/}
<div className="row"> <div className="row">
<div className="col-lg-offset-1 col-lg-3"> <div className="col-lg-offset-1 col-lg-3">
{/*<UserLoginView />*/} <UserLoginView />
</div> </div>
<div className="col-lg-offset-5 col-lg-2"> <div className="col-lg-offset-5 col-lg-2">
<ConnectedThemeSwitchView /> <ConnectedThemeSwitchView />

View File

@ -1,34 +0,0 @@
/*
Store handling app site information.
@copyright (c) Copyright 2013-2016 Dorian Pula
@license AGPL-1.0
@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
*/
import Reflux from "reflux";
import request from "superagent";
import {Actions} from "../actions.js";
const AppSiteInfoStore = Reflux.createStore({
init: function() {
this.listenTo(Actions.fetchAppSiteInfo, this.fetchAppSiteInfo);
},
fetchAppSiteInfo: function() {
request.get("/app/config")
.accept('json')
.end(
(err, res) => {
if (res.ok) {
Actions.renderAppSiteInfo(res.body);
} else {
console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
}
});
}
});
export { AppSiteInfoStore };

View File

@ -1,34 +0,0 @@
/*
Store handling navigation menu.
@copyright (c) Copyright 2013-2016 Dorian Pula
@license AGPL-1.0
@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
*/
import Reflux from "reflux";
import request from "superagent";
import {Actions} from "../actions.js";
const NavigationMenuStore = Reflux.createStore({
init: function() {
this.listenTo(Actions.fetchNavigationMenu, this.fetchNavigationDetails);
},
fetchNavigationDetails: function() {
request.get("/app/menu")
.accept('json')
.end(
(err, res) => {
if (res.ok) {
Actions.renderNavigationMenu({menu: res.body.menu});
} else {
console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
}
});
}
});
export { NavigationMenuStore };

View File

@ -1,60 +1,54 @@
/* /*
User login store. Managing the user login store.
@copyright (c) Copyright 2013-2016 Dorian Pula @copyright (c) Copyright 2013-2016 Dorian Pula
@license AGPL v3 @license AGPL v3
@author Dorian Pula [dorian.pula@amber-penguin-software.ca] @author Dorian Pula [dorian.pula@amber-penguin-software.ca]
*/ */
import Reflux from "reflux";
import request from "superagent"; import request from "superagent";
import { Actions } from "../actions.js"; import { UserInfo } from "./user_info";
import { UserInfo } from "../user_info.js";
export const UserLoginStore = Reflux.createStore({ export class UserLoginManager {
init: function() {
this.listenTo(Actions.loginUserAttempt, this.attemptUserLogin);
this.listenTo(Actions.logoutUser, this.logoutUser);
},
isUserLoggedIn: function() { static isUserLoggedIn() {
return UserInfo.getUserInfo("auth_token") != ""; return UserInfo.getUserInfo("auth_token") != "";
}, }
getUserAuthToken: function() { static getUserAuthToken() {
return UserInfo.getUserInfo("auth_token"); return UserInfo.getUserInfo("auth_token");
}, }
getCurrentUserFullName: function() { static getCurrentUserFullName() {
return UserInfo.getUserInfo("full_name"); return UserInfo.getUserInfo("full_name");
}, }
attemptUserLogin: function(username, password) { static attemptUserLogin(username, password) {
request.post("/auth") request.post("/auth")
.send({username: username, password: password}) .send({username: username, password: password})
.accept('json') .accept('json')
.end((err, res) => { .end((err, res) => {
if (res.ok) { if (res.ok) {
// TODO Include the user full name in response as well. // TODO Include the user full name in response as well.
UserLoginStore.updateUserLogin(res.body.token, "admin"); UserLoginManager.updateUserLogin(res.body.token, "admin");
Actions.loginUserSuccess(); Actions.loginUserSuccess();
} else { } else {
console.error(`Unable to login - ${res.status} - ${res.text} - Actual ${err}`); console.error(`Unable to login - ${res.status} - ${res.text} - Actual ${err}`);
Actions.loginUserFailure("Login failed. Try again!"); Actions.loginUserFailure("Login failed. Try again!");
} }
}); });
}, }
logoutUser: function() { static logoutUser() {
UserInfo.removeUserInfo("auth_token"); UserInfo.removeUserInfo("auth_token");
UserInfo.removeUserInfo("full_name"); UserInfo.removeUserInfo("full_name");
Actions.logoutUserFinalize(); Actions.logoutUserFinalize();
}, }
updateUserLogin: function(token, fullName) { static updateUserLogin(token, fullName) {
if (token !== undefined && token !== "") { if (token !== undefined && token !== "") {
UserInfo.setUserInfo("auth_token", token); UserInfo.setUserInfo("auth_token", token);
UserInfo.setUserInfo("full_name", fullName); UserInfo.setUserInfo("full_name", fullName);
@ -63,4 +57,4 @@ export const UserLoginStore = Reflux.createStore({
Actions.loginUserFailure(`Received invalid token: ${token}`); Actions.loginUserFailure(`Received invalid token: ${token}`);
} }
} }
}); }

View File

@ -12,14 +12,16 @@ import LinkedStateMixin from "react-addons-linked-state-mixin";
import {Modal, Input, Button, Alert} from "react-bootstrap"; import {Modal, Input, Button, Alert} from "react-bootstrap";
import FontAwesome from "react-fontawesome"; import FontAwesome from "react-fontawesome";
import {UserLoginManager} from "../user_login_manager";
const UserLoginView = React.createClass({ const UserLoginView = React.createClass({
mixins: [LinkedStateMixin], mixins: [LinkedStateMixin],
getInitialState: function() { getInitialState: function() {
return { return {
isUserLoggedIn: UserLoginStore.isUserLoggedIn(), isUserLoggedIn: UserLoginManager.isUserLoggedIn(),
fullName: UserLoginStore.getCurrentUserFullName(), fullName: UserLoginManager.getCurrentUserFullName(),
username: "", username: "",
password: "", password: "",
errorMessage: "", errorMessage: "",
@ -34,7 +36,7 @@ const UserLoginView = React.createClass({
} }
console.log("Attempting login..."); console.log("Attempting login...");
Actions.loginUserAttempt(this.state.username, this.state.password); UserLoginManager.attemptUserLogin(this.state.username, this.state.password);
}, },
loginFailure: function(errorMessage = "") { loginFailure: function(errorMessage = "") {
@ -43,7 +45,7 @@ const UserLoginView = React.createClass({
loginSuccess: function() { loginSuccess: function() {
this.hideLoginModal(); this.hideLoginModal();
this.setState({fullName: UserLoginStore.getCurrentUserFullName(), isUserLoggedIn: true}); this.setState({fullName: UserLoginManager.getCurrentUserFullName(), isUserLoggedIn: true});
}, },
showLoginModal: function() { showLoginModal: function() {
@ -60,7 +62,7 @@ const UserLoginView = React.createClass({
}, },
logoutUserRefresh: function() { logoutUserRefresh: function() {
this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginStore.getCurrentUserFullName()}); this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginManager.getCurrentUserFullName()});
}, },
render: function() { render: function() {