From 2109f52b78f5dc76308eaa7a5dd5ec902e468712 Mon Sep 17 00:00:00 2001 From: majormjr Date: Sat, 30 Apr 2016 14:35:10 -0400 Subject: [PATCH] added handlers for login, logout, checking user status, added checks in ui to redirect to login page --- .gitignore | 3 +- conf.json => conf.json.example | 0 docker/Dockerfile | 6 +- docker/init.sh | 1 - docker/nginx.conf | 4 +- handlers.go | 63 +++++++++++++++++++- routes.go | 10 ++++ ui/App/App.jsx | 90 +++++++++++++++++++---------- ui/App/components/HiddenSidebar.jsx | 11 ++-- ui/App/components/LoginContent.jsx | 9 ++- ui/index.js | 6 +- 11 files changed, 154 insertions(+), 49 deletions(-) rename conf.json => conf.json.example (100%) diff --git a/.gitignore b/.gitignore index 4be4ca2..7f91512 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules/ bundle.js factorio-server-manager -auth.level-db/ +auth.level-db/* +conf.json *.exe diff --git a/conf.json b/conf.json.example similarity index 100% rename from conf.json rename to conf.json.example diff --git a/docker/Dockerfile b/docker/Dockerfile index 6740d63..0392941 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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"] diff --git a/docker/init.sh b/docker/init.sh index 1852193..9835c94 100755 --- a/docker/init.sh +++ b/docker/init.sh @@ -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 diff --git a/docker/nginx.conf b/docker/nginx.conf index 1776f88..a6928f8 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -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; } } diff --git a/handlers.go b/handlers.go index 8318819..078b7b6 100644 --- a/handlers.go +++ b/handlers.go @@ -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, "/") } diff --git a/routes.go b/routes.go index afd1b52..806867b 100644 --- a/routes.go +++ b/routes.go @@ -170,5 +170,15 @@ var apiRoutes = Routes{ "POST", "/login", LoginUser, + }, { + "LogoutUser", + "GET", + "/logout", + LogoutUser, + }, { + "UserStatus", + "GET", + "/user/status", + GetCurrentLogin, }, } diff --git a/ui/App/App.jsx b/ui/App/App.jsx index 5d1a3df..11a984a 100644 --- a/ui/App/App.jsx +++ b/ui/App/App.jsx @@ -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 = +
+
+ + + + {React.cloneElement( + this.props.children, + {message: "", + facServStatus: this.facServStatus, + serverStatus: this.state.serverStatus, + getStatus: this.getStatus, + saves: this.state.saves, + getSaves: this.getSaves} + )} + +
+ + ; +
+ } else { + var resp =

Not Logged in

; + console.log(resp); + } + return( -
- -
- - - - {React.cloneElement( - this.props.children, - {message: "", - facServStatus: this.facServStatus, - serverStatus: this.state.serverStatus, - getStatus: this.getStatus, - saves: this.state.saves, - getSaves: this.getSaves} - )} - -