added handlers for login, logout, checking user status, added checks in ui to redirect to login page

This commit is contained in:
majormjr 2016-04-30 14:35:10 -04:00
parent cd78f35a77
commit 2109f52b78
11 changed files with 154 additions and 49 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules/
bundle.js
factorio-server-manager
auth.level-db/
auth.level-db/*
conf.json
*.exe

View File

@ -20,7 +20,11 @@ RUN curl -s -L -S -k https://www.factorio.com/get-download/$FACTORIO_VERSION/hea
rm /tmp/factorio_$FACTORIO_VERSION.tar.gz && \
curl -s -L -S -k https://github.com/MajorMJR/factorio-server-manager/releases/download/$MANAGER_VERSION/factorio-server-manager-linux-x64.zip --cacert /opt/github.pem -o /tmp/factorio-server-manager-linux-x64_$MANAGER_VERSION.zip && \
unzip -qq /tmp/factorio-server-manager-linux-x64_$MANAGER_VERSION.zip && \
rm /tmp/factorio-server-manager-linux-x64_$MANAGER_VERSION.zip
rm /tmp/factorio-server-manager-linux-x64_$MANAGER_VERSION.zip && \
rm -r /var/lib/nginx && \
mkdir -p /var/lib/nginx/tmp && \
chown nginx:root /var/lib/nginx
EXPOSE 80/tcp 443/tcp 34190-34200/udp
ENTRYPOINT ["/opt/init.sh"]

View File

@ -28,6 +28,5 @@ if [ ! -f /security/passwords.conf ]; then
openssl passwd -apr1 $ADMIN_PASSWORD >> /security/passwords.conf
fi
nohup nginx &
/opt/factorio-server/factorio-server-manager -dir /opt/factorio

View File

@ -16,13 +16,12 @@ events {
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 20m;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 100m;
#gzip on;
server {
@ -59,6 +58,7 @@ http {
location / {
root /opt/factorio-server/app;
try_files $uri /index.html;
}
}

View File

@ -593,9 +593,68 @@ func LoginUser(w http.ResponseWriter, r *http.Request) {
return
}
fmt.Println(user)
err = Auth.aaa.Login(w, r, user.Username, user.Password, "/")
if err != nil {
log.Printf("Error logging in user: %s, error: %s", user.Username, err)
resp.Data = fmt.Sprintf("Error logging in user: %s", user.Username)
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error listing mods: %s", err)
}
return
}
log.Printf("User: %s, logged in successfully", user.Username)
resp.Data = fmt.Sprintf("User: %s, logged in successfully", user.Username)
resp.Success = true
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error listing mods: %s", err)
}
}
}
func LogoutUser(w http.ResponseWriter, r *http.Request) {
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
if err := Auth.aaa.Logout(w, r); err != nil {
log.Printf("Error logging out current user")
return
}
resp.Success = true
resp.Data = "User logged out successfully."
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error logging out: %s", err)
}
}
func GetCurrentLogin(w http.ResponseWriter, r *http.Request) {
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
user, err := Auth.aaa.CurrentUser(w, r)
if err != nil {
log.Printf("Error getting current user status: %s", err)
resp.Data = fmt.Sprintf("Error getting user status: %s", user.Username)
resp.Success = false
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error listing mods: %s", err)
}
return
}
fmt.Println(user, err)
resp.Success = true
resp.Data = user
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error getting user status: %s", err)
}
//if err := Auth.aaa.Login(w, r, username, password, "/")
}

View File

@ -170,5 +170,15 @@ var apiRoutes = Routes{
"POST",
"/login",
LoginUser,
}, {
"LogoutUser",
"GET",
"/logout",
LogoutUser,
}, {
"UserStatus",
"GET",
"/user/status",
GetCurrentLogin,
},
}

View File

@ -9,6 +9,7 @@ import HiddenSidebar from './components/HiddenSidebar.jsx';
class App extends React.Component {
constructor(props) {
super(props);
this.checkLogin = this.checkLogin.bind(this);
this.facServStatus = this.facServStatus.bind(this);
this.getSaves = this.getSaves.bind(this);
this.getStatus = this.getStatus.bind(this);
@ -16,15 +17,31 @@ class App extends React.Component {
serverRunning: "stopped",
serverStatus: {},
saves: [],
loggedIn: true,
loggedIn: false,
}
}
// Check if state.loggedIn is true, if so continue, else redirect to /login page.
componentWillMount() {
if (!this.state.loggedIn) {
browserHistory.push("/login");
}
componentDidMount() {
this.checkLogin();
setTimeout(() => {
if (!this.state.loggedIn) {
browserHistory.push("/login");
}
}, 1000);
}
checkLogin() {
console.log(this.state);
$.ajax({
url: "/api/user/status",
dataType: "json",
success: (data) => {
console.log(data.success);
if (data.success === true) {
this.setState({loggedIn: true})
}
}
})
}
facServStatus() {
@ -64,32 +81,43 @@ class App extends React.Component {
}
render() {
// render main application, if not logged in show Not logged in message
// if logged in show application
var resp;
if (this.state.loggedIn) {
var resp =
<div>
<Header />
<Sidebar
serverStatus={this.facServStatus}
serverRunning={this.state.serverRunning}
/>
{React.cloneElement(
this.props.children,
{message: "",
facServStatus: this.facServStatus,
serverStatus: this.state.serverStatus,
getStatus: this.getStatus,
saves: this.state.saves,
getSaves: this.getSaves}
)}
<Footer />
<HiddenSidebar
serverStatus={this.state.serverStatus}
/>;
</div>
} else {
var resp = <div><p>Not Logged in</p></div>;
console.log(resp);
}
return(
<div className="wrapper" style={{height: "100%"}}>
<Header />
<Sidebar
serverStatus={this.facServStatus}
serverRunning={this.state.serverRunning}
/>
{React.cloneElement(
this.props.children,
{message: "",
facServStatus: this.facServStatus,
serverStatus: this.state.serverStatus,
getStatus: this.getStatus,
saves: this.state.saves,
getSaves: this.getSaves}
)}
<Footer />
<HiddenSidebar
serverStatus={this.state.serverStatus}
/>
<div className="wrapper">
{resp}
</div>
)
}

View File

@ -17,12 +17,15 @@ class HiddenSidebar extends React.Component {
<li>
<Link to="/login" activeClassName="active">
<i className="menu-icon fa fa-lock bg-green"></i>
<div className="menu-info">
<i classNameName="fa fa-lock fa-fw"></i>Login
</div>
Login
</Link>
</li>
<li>
<a href="/api/logout">
<i className="menu-icon fa fa-lock bg-red"></i>
Login
</a>
</li>
</ul>
<div className="table-responsive">
<table className="table table-border">

View File

@ -1,5 +1,5 @@
import React from 'react';
import {IndexLink} from 'react-router';
import {IndexLink, browserHistory} from 'react-router';
class LoginContent extends React.Component {
constructor(props) {
@ -8,7 +8,7 @@ class LoginContent extends React.Component {
}
componentDidMount() {
console.log(this.state);
console.log(this.props);
}
loginUser(e) {
@ -25,7 +25,10 @@ class LoginContent extends React.Component {
dataType: "json",
data: JSON.stringify(user),
success: (resp) => {
console.log(resp)
console.log(resp);
if (resp.success === true) {
browserHistory.push("/")
}
}
})
}

View File

@ -9,12 +9,10 @@ import ConfigContent from './App/components/ConfigContent.jsx';
import LoginContent from './App/components/LoginContent.jsx';
import Index from './App/components/Index.jsx';
var loggedIn = false
ReactDOM.render((
<Router history={browserHistory}>
<Route path="/login" component={LoginContent} loggedIn={loggedIn}/>
<Route path="/" component={App} loggedIn={loggedIn}>
<Route path="/login" component={LoginContent}/>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="/server" component={Index}/>
<Route path="/mods" component={ModsContent}/>