Start on fixing up the login manager.
This commit is contained in:
parent
21eae48bf7
commit
089a9f9840
|
@ -33,6 +33,12 @@ export function fetchArticle(articleId='landing') {
|
|||
console.info(`Loading... ${articleId}`);
|
||||
return fetch(`/app/pages/${articleId}`)
|
||||
.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();
|
||||
})
|
||||
.then((json) => {
|
||||
|
@ -54,6 +60,12 @@ export function fetchNavigationMenu() {
|
|||
return (dispatch) => {
|
||||
return fetch("/app/menu")
|
||||
.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();
|
||||
})
|
||||
.then((json) => {
|
||||
|
@ -66,6 +78,14 @@ export function fetchAppSiteInfo() {
|
|||
return (dispatch) => {
|
||||
return fetch("/app/config")
|
||||
.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();
|
||||
})
|
||||
.then((json) => {
|
||||
|
@ -125,13 +145,6 @@ export function loadNavigation(menu=[]) {
|
|||
};
|
||||
}
|
||||
|
||||
export function notifyUserLogin(user) {
|
||||
return {
|
||||
type: LOGIN_USER_ACTION,
|
||||
user: user
|
||||
};
|
||||
}
|
||||
|
||||
export function switchThemes() {
|
||||
let switchedThemes = ThemeManager.switchTheme();
|
||||
return {
|
||||
|
|
|
@ -9,8 +9,8 @@ Rookeries client app
|
|||
import React from "react";
|
||||
import {connect} from "react-redux";
|
||||
|
||||
// import {UserLoginView} from "../views/user_login_modal";
|
||||
import {ThemeSwitchView, ConnectedThemeSwitchView} from "../views/theme_switcher_button";
|
||||
import {UserLoginView} from "../views/user_login_modal";
|
||||
import {ConnectedThemeSwitchView} from "../views/theme_switcher_button";
|
||||
|
||||
import {SiteHeader} from "../views/site_header";
|
||||
import {SiteFooter} from "../views/site_footer";
|
||||
|
@ -33,7 +33,7 @@ class App extends React.Component {
|
|||
{/*TODO Extract as header container.*/}
|
||||
<div className="row">
|
||||
<div className="col-lg-offset-1 col-lg-3">
|
||||
{/*<UserLoginView />*/}
|
||||
<UserLoginView />
|
||||
</div>
|
||||
<div className="col-lg-offset-5 col-lg-2">
|
||||
<ConnectedThemeSwitchView />
|
||||
|
|
|
@ -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 };
|
|
@ -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 };
|
|
@ -1,60 +1,54 @@
|
|||
/*
|
||||
User login store.
|
||||
Managing the user login store.
|
||||
|
||||
@copyright (c) Copyright 2013-2016 Dorian Pula
|
||||
@license AGPL v3
|
||||
@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
|
||||
*/
|
||||
|
||||
import Reflux from "reflux";
|
||||
import request from "superagent";
|
||||
|
||||
import { Actions } from "../actions.js";
|
||||
import { UserInfo } from "../user_info.js";
|
||||
import { UserInfo } from "./user_info";
|
||||
|
||||
|
||||
export const UserLoginStore = Reflux.createStore({
|
||||
init: function() {
|
||||
this.listenTo(Actions.loginUserAttempt, this.attemptUserLogin);
|
||||
this.listenTo(Actions.logoutUser, this.logoutUser);
|
||||
},
|
||||
export class UserLoginManager {
|
||||
|
||||
isUserLoggedIn: function() {
|
||||
static isUserLoggedIn() {
|
||||
return UserInfo.getUserInfo("auth_token") != "";
|
||||
},
|
||||
}
|
||||
|
||||
getUserAuthToken: function() {
|
||||
static getUserAuthToken() {
|
||||
return UserInfo.getUserInfo("auth_token");
|
||||
},
|
||||
}
|
||||
|
||||
getCurrentUserFullName: function() {
|
||||
static getCurrentUserFullName() {
|
||||
return UserInfo.getUserInfo("full_name");
|
||||
},
|
||||
}
|
||||
|
||||
attemptUserLogin: function(username, password) {
|
||||
static attemptUserLogin(username, password) {
|
||||
request.post("/auth")
|
||||
.send({username: username, password: password})
|
||||
.accept('json')
|
||||
.end((err, res) => {
|
||||
if (res.ok) {
|
||||
// TODO Include the user full name in response as well.
|
||||
UserLoginStore.updateUserLogin(res.body.token, "admin");
|
||||
UserLoginManager.updateUserLogin(res.body.token, "admin");
|
||||
Actions.loginUserSuccess();
|
||||
} else {
|
||||
console.error(`Unable to login - ${res.status} - ${res.text} - Actual ${err}`);
|
||||
Actions.loginUserFailure("Login failed. Try again!");
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
logoutUser: function() {
|
||||
static logoutUser() {
|
||||
UserInfo.removeUserInfo("auth_token");
|
||||
UserInfo.removeUserInfo("full_name");
|
||||
Actions.logoutUserFinalize();
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
updateUserLogin: function(token, fullName) {
|
||||
static updateUserLogin(token, fullName) {
|
||||
if (token !== undefined && token !== "") {
|
||||
UserInfo.setUserInfo("auth_token", token);
|
||||
UserInfo.setUserInfo("full_name", fullName);
|
||||
|
@ -63,4 +57,4 @@ export const UserLoginStore = Reflux.createStore({
|
|||
Actions.loginUserFailure(`Received invalid token: ${token}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
|
@ -12,14 +12,16 @@ import LinkedStateMixin from "react-addons-linked-state-mixin";
|
|||
import {Modal, Input, Button, Alert} from "react-bootstrap";
|
||||
import FontAwesome from "react-fontawesome";
|
||||
|
||||
import {UserLoginManager} from "../user_login_manager";
|
||||
|
||||
|
||||
const UserLoginView = React.createClass({
|
||||
mixins: [LinkedStateMixin],
|
||||
|
||||
getInitialState: function() {
|
||||
return {
|
||||
isUserLoggedIn: UserLoginStore.isUserLoggedIn(),
|
||||
fullName: UserLoginStore.getCurrentUserFullName(),
|
||||
isUserLoggedIn: UserLoginManager.isUserLoggedIn(),
|
||||
fullName: UserLoginManager.getCurrentUserFullName(),
|
||||
username: "",
|
||||
password: "",
|
||||
errorMessage: "",
|
||||
|
@ -34,7 +36,7 @@ const UserLoginView = React.createClass({
|
|||
}
|
||||
|
||||
console.log("Attempting login...");
|
||||
Actions.loginUserAttempt(this.state.username, this.state.password);
|
||||
UserLoginManager.attemptUserLogin(this.state.username, this.state.password);
|
||||
},
|
||||
|
||||
loginFailure: function(errorMessage = "") {
|
||||
|
@ -43,7 +45,7 @@ const UserLoginView = React.createClass({
|
|||
|
||||
loginSuccess: function() {
|
||||
this.hideLoginModal();
|
||||
this.setState({fullName: UserLoginStore.getCurrentUserFullName(), isUserLoggedIn: true});
|
||||
this.setState({fullName: UserLoginManager.getCurrentUserFullName(), isUserLoggedIn: true});
|
||||
},
|
||||
|
||||
showLoginModal: function() {
|
||||
|
@ -60,7 +62,7 @@ const UserLoginView = React.createClass({
|
|||
},
|
||||
|
||||
logoutUserRefresh: function() {
|
||||
this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginStore.getCurrentUserFullName()});
|
||||
this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginManager.getCurrentUserFullName()});
|
||||
},
|
||||
|
||||
render: function() {
|
||||
|
|
Loading…
Reference in New Issue