Skip to content

Commit

Permalink
move logout mutation to go (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed Jul 29, 2020
1 parent f819e85 commit 8d59daa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion kotsadm/pkg/apiserver/server.go
Expand Up @@ -107,7 +107,7 @@ func Start() {
r.Path("/api/v1/app/{appSlug}/cluster/{clusterId}/dashboard").Methods("OPTIONS", "GET").HandlerFunc(handlers.GetAppDashboard)

r.HandleFunc("/api/v1/login", handlers.Login)
r.HandleFunc("/api/v1/logout", handlers.NotImplemented)
r.HandleFunc("/api/v1/logout", handlers.Logout)

// Installation
r.Path("/api/v1/license").Methods("OPTIONS", "POST").HandlerFunc(handlers.UploadNewLicense)
Expand Down
40 changes: 40 additions & 0 deletions kotsadm/pkg/handlers/logout.go
@@ -0,0 +1,40 @@
package handlers

import (
"net/http"

"github.com/replicatedhq/kots/kotsadm/pkg/logger"
"github.com/replicatedhq/kots/kotsadm/pkg/session"
"github.com/replicatedhq/kots/kotsadm/pkg/user"
)

func Logout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "content-type, origin, accept, authorization")

if r.Method == "OPTIONS" {
w.WriteHeader(200)
return
}

sess, err := session.Parse(r.Header.Get("Authorization"))
if err != nil {
logger.Error(err)
w.WriteHeader(401)
return
}

// we don't currently have roles, all valid tokens are valid sessions
if sess == nil || sess.ID == "" {
w.WriteHeader(401)
return
}

if err := user.LogOut(sess.ID); err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}

JSON(w, 204, "")
}
13 changes: 13 additions & 0 deletions kotsadm/pkg/user/user.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/persistence"
"golang.org/x/crypto/bcrypt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -48,3 +49,15 @@ func LogIn(password string) (*User, error) {
ID: "000000",
}, nil
}

func LogOut(id string) error {
db := persistence.MustGetPGSession()
query := `delete from session where id = $1`

_, err := db.Exec(query, id)
if err != nil {
return errors.Wrap(err, "failed to exec")
}

return nil
}
7 changes: 1 addition & 6 deletions kotsadm/web/src/components/apps/AppDetailPage.jsx
Expand Up @@ -9,7 +9,7 @@ import has from "lodash/has";
import withTheme from "@src/components/context/withTheme";
import { getKotsApp, listDownstreamsForApp } from "@src/queries/AppsQueries";
import { isVeleroInstalled } from "@src/queries/SnapshotQueries";
import { createKotsDownstream, deleteKotsDownstream } from "../../mutations/AppsMutations";
import { createKotsDownstream } from "../../mutations/AppsMutations";
import { KotsSidebarItem } from "@src/components/watches/WatchSidebarItem";
import { HelmChartSidebarItem } from "@src/components/watches/WatchSidebarItem";
import NotFound from "../static/NotFound";
Expand Down Expand Up @@ -433,9 +433,4 @@ export default compose(
createKotsDownstream: (appId, clusterId) => mutate({ variables: { appId, clusterId } })
})
}),
graphql(deleteKotsDownstream, {
props: ({ mutate }) => ({
deleteKotsDownstream: (slug, clusterId) => mutate({ variables: { slug, clusterId } })
})
}),
)(AppDetailPage);
31 changes: 15 additions & 16 deletions kotsadm/web/src/components/shared/NavBar.jsx
Expand Up @@ -2,11 +2,9 @@ import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { Link, withRouter } from "react-router-dom";
import { compose, withApollo, graphql } from "react-apollo";

import { Utilities } from "@src/utilities/utilities";
import { listClusters } from "@src/queries/ClusterQueries";
import { logout } from "@src/mutations/GitHubMutations";
import Avatar from "../shared/Avatar";

import "@src/scss/components/shared/NavBar.scss";
Expand All @@ -27,11 +25,20 @@ export class NavBar extends PureComponent {

handleLogOut = async (e) => {
e.preventDefault();
await this.props.logout()
.catch((err) => {
console.log(err);
})
Utilities.logoutUser();
try {
const res = await fetch(`${window.env.API_ENDPOINT}/logout`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
});
if (res.ok && res.status === 204) {
Utilities.logoutUser();
}
} catch(err) {
console.log(err);
}
}

componentDidUpdate(lastProps) {
Expand Down Expand Up @@ -194,12 +201,4 @@ export class NavBar extends PureComponent {
}
}

export default compose(
withRouter,
withApollo,
graphql(logout, {
props: ({ mutate }) => ({
logout: () => mutate()
})
}),
)(NavBar);
export default withRouter(NavBar);

0 comments on commit 8d59daa

Please sign in to comment.