Browse Source

Added logic for logging out

howdoyoucode 8 years ago
parent
commit
5a7d31bcd6
2 changed files with 45 additions and 0 deletions
  1. 8 0
      frontend/app/js/routes.js
  2. 37 0
      frontend/app/js/views/Auth/Logout.jsx

+ 8 - 0
frontend/app/js/routes.js

@@ -27,6 +27,14 @@ export default {
 					.catch(errorLoading);
 			},
 		},
+		{
+			path: "logout",
+			getComponent(location, cb) {
+				System.import("views/Auth/Logout")
+					.then(loadRoute(cb, false))
+					.catch(errorLoading);
+			},
+		},
 		{
 			path: "register",
 			getComponent(location, cb) {

+ 37 - 0
frontend/app/js/views/Auth/Logout.jsx

@@ -0,0 +1,37 @@
+import React, { Component } from "react";
+import PropTypes from "prop-types";
+
+import io from "../../io";
+import config from "../../../../config/default";
+
+export default class Login extends Component {
+	constructor() {
+		super();
+
+		this.state = {};
+
+		this.logout = this.logout.bind(this);
+	}
+
+	logout() {
+		io.getSocket(socket => {
+			socket.emit("users.logout", res => {
+				if (res.status === "success") {
+					document.cookie = "SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
+					location.reload(); // if we could avoid this, then that would be better
+				} else {
+					// return res.message, temporarily:
+					alert(res.message);
+				}
+			});
+		});
+	}
+
+	render() {
+		return (
+			<div>
+				<button onClick={ this.logout }>Logout</button>
+			</div>
+		);
+	}
+}