Browse Source

Finished more things for settings page

KrisVos130 7 years ago
parent
commit
d336296671
2 changed files with 68 additions and 2 deletions
  1. 5 0
      frontend/app/js/views/App/index.jsx
  2. 63 2
      frontend/app/js/views/Auth/Settings.jsx

+ 5 - 0
frontend/app/js/views/App/index.jsx

@@ -55,6 +55,11 @@ class App extends Component { // eslint-disable-line react/no-multi-comp
 				dispatch(authenticate({ loggedIn, role, username, userId }));
 			});
 			socket.on("keep.event:banned", reason => dispatch(ban(reason)));
+
+			socket.on("keep.event:user.session.removed", () => {
+				location.reload();
+				// TODO Give user prompt they've been logged out and let them continue.
+			});
 		});
 
 		if (localStorage.getItem("github_redirect")) {

+ 63 - 2
frontend/app/js/views/Auth/Settings.jsx

@@ -181,6 +181,67 @@ export default class Settings extends Component {
 		}
 	};
 
+	logOutEverywhere = () => {
+		this.setState({ errors: [] });
+		io.getSocket(socket => {
+			socket.emit("users.removeSessions", this.props.user.userId, res => {
+				if (res.status === "success") {
+					alert("Success!");
+				} else {
+					this.setState({
+						errors: this.state.errors.concat([res.message]),
+					});
+				}
+			});
+		});
+	};
+
+	unlinkGitHub = () => {
+		this.setState({ errors: [] });
+		io.getSocket(socket => {
+			socket.emit("users.unlinkGitHub", res => {
+				if (res.status === "success") {
+					alert("Success!");
+				} else {
+					this.setState({
+						errors: this.state.errors.concat([res.message]),
+					});
+				}
+			});
+		});
+	};
+
+	unlinkPassword = () => {
+		this.setState({ errors: [] });
+		io.getSocket(socket => {
+			socket.emit("users.unlinkPassword", res => {
+				if (res.status === "success") {
+					alert("Success!");
+				} else {
+					this.setState({
+						errors: this.state.errors.concat([res.message]),
+					});
+				}
+			});
+		});
+	};
+
+	linkButtons = () => {
+		const linkPassword = <button>TODO</button>;
+		const linkGitHub = <a href="http://localhost:8080/auth/github/link">Link GitHub to account</a>;
+		const unlinkGitHub = (<button onClick={ this.unlinkGitHub }>
+				Remove logging in with GitHub
+			</button>);
+		const unlinkPassword = (<button onClick={ this.unlinkPassword }>
+			Remove logging in with password
+		</button>);
+		if (this.state.passwordLinked && this.state.gitHubLinked) {
+			return [unlinkGitHub, unlinkPassword];
+		} else if (!this.state.passwordLinked) {
+			return linkPassword;
+		} return linkGitHub;
+	};
+
 	validationCallback = CustomInput.validationCallback(this);
 
 	render() {
@@ -197,8 +258,8 @@ export default class Settings extends Component {
 					<h2>Security</h2>
 					<CustomInput label="New password" placeholder="New password" inputType="password" type="password" name="newPassword" value={ this.state.newPassword } customInputEvents={ { onChange: event => this.updateField("newPassword", event) } } validationCallback={ this.validationCallback } />
 					<button onClick={ this.changePassword }>Change password</button>
-					<button>Link GitHub account</button>
-					<button>Log out everywhere</button>
+					{ this.linkButtons() }
+					<button onClick={ this.logOutEverywhere }>Log out everywhere</button>
 				</div>
 			</div>
 		);