Skip to content

Commit

Permalink
Working user auth
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneclaes committed Oct 6, 2020
1 parent 2777e97 commit f7ce9e4
Show file tree
Hide file tree
Showing 40 changed files with 761 additions and 939 deletions.
2 changes: 0 additions & 2 deletions .makerverse.default
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"watchDirectory": "",
"accessTokenLifetime": "30d",
"allowRemoteAccess": false,
"workspaces": []
}
2 changes: 0 additions & 2 deletions .makerverse.docker
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"watchDirectory": "/home/node/gcode",
"accessTokenLifetime": "30d",
"allowRemoteAccess": true,
"workspaces": [],
"mountPoints": [
{
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@
"expand-tilde": "~2.0.2",
"expr-eval": "~1.2.2",
"express": "~4.16.4",
"express-jwt": "~5.3.1",
"express-session": "~1.16.1",
"final-form": "~4.12.0",
"font-awesome": "~4.7.0",
Expand All @@ -202,7 +201,6 @@
"is-electron": "~2.2.0",
"jimp": "~0.6.1",
"js-polyfills": "~0.1.42",
"jsonwebtoken": "~8.5.1",
"jsuri": "~1.3.1",
"keycode": "~2.2.0",
"lodash": "~4.17.11",
Expand Down Expand Up @@ -259,7 +257,6 @@
"shortid": "~2.2.14",
"socket.io": "~2.2.0",
"socket.io-client": "~2.2.0",
"socketio-jwt": "~4.5.0",
"sortablejs": "~1.9.0",
"spawn-default-shell": "~2.0.0",
"styled-components": "~3.4.9",
Expand Down
37 changes: 0 additions & 37 deletions src/app/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,10 @@ const noCache = (request) => {
}
};

export const apirequest = superagentUse(superagent);
apirequest.use(noCache);

export const authrequest = superagentUse(superagent);
authrequest.use(bearer);
authrequest.use(noCache);

//
// Authentication
//
const signin = (options) => new Promise((resolve, reject) => {
// const { token, username, password } = { ...options };

// const requestedUrl = new URLSearchParams(window.location.search).get('ReturnUrl');
// args.returnUrl = requestedUrl || (await ).url;
// const args = { username, password };

// fetch(`${owsApi}/auth/login`, {
// method: 'POST',
// credentials: 'include',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(args),
// });

// authrequest
// .post('/api/signin')
// .send({ token, name, password })
// .end((err, res) => {
// if (err) {
// reject(res);
// } else {
// resolve(res);
// }
// });
});

//
// Latest Version
//
Expand Down Expand Up @@ -710,9 +676,6 @@ export default {
fetchGCode,
downloadGCode,

// Authentication
signin,

// Controllers
controllers,

Expand Down
18 changes: 3 additions & 15 deletions src/app/components/ProtectedRoute/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
// import PropTypes from 'prop-types';
import auth from 'app/lib/auth';
import { connect } from 'react-redux';
import { Route, Redirect, withRouter } from 'react-router-dom';
import log from 'app/lib/log';
Expand All @@ -8,8 +9,7 @@ const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => {
if (props.user) {
log.debug('User is authenticated.', props.user);
if (auth.isAuthenticated()) {
return Component ? <Component {...rest} /> : null;
}

Expand Down Expand Up @@ -39,16 +39,4 @@ ProtectedRoute.propTypes = {
...withRouter.propTypes,
};

function mapStateToProps(state) {
return {
user: state.oidc.user
};
}

function mapDispatchToProps(dispatch) {
return {
dispatch
};
}

export default connect(mapStateToProps, mapDispatchToProps)(ProtectedRoute);
export default connect()(ProtectedRoute);
16 changes: 5 additions & 11 deletions src/app/containers/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import Space from 'app/components/Space';
import combokeys from 'app/lib/combokeys';
import i18n from 'app/lib/i18n';
import log from 'app/lib/log';
import * as user from 'app/lib/user';
import store from 'app/store';
import auth from 'app/lib/auth';
import Workspaces from 'app/lib/workspaces';
import settings from 'app/config/settings';
import styles from './index.styl';
Expand Down Expand Up @@ -244,9 +243,7 @@ class Header extends PureComponent {
render() {
const { history, location } = this.props;
const { pushPermission, commands, runningTasks, updateAvailable, latestVersion, lastUpdate } = this.state;
const sessionEnabled = store.get('session.enabled');
const signedInName = store.get('session.name');
const hideUserDropdown = !sessionEnabled;
const signedInName = auth.user ? auth.user.username : '?';
const showCommands = commands.length > 0;
const workspace = Workspaces.findByPath(location.pathname);
const updateMsg = i18n._('A new version of {{name}} is available', { name: settings.productName }) + '. ' +
Expand Down Expand Up @@ -301,9 +298,6 @@ class Header extends PureComponent {
<Navbar.Collapse>
<Nav pullRight>
<NavDropdown
className={classNames(
{ 'hidden': hideUserDropdown }
)}
id="nav-dropdown-user"
title={(
<div title={i18n._('My Account')}>
Expand All @@ -317,19 +311,19 @@ class Header extends PureComponent {
</MenuItem>
<MenuItem divider />
<MenuItem
href="#/settings/user-accounts"
href="https://openwork.shop/account/manage"
>
<i className="fa fa-fw fa-user" />
<Space width="8" />
{i18n._('Account')}
</MenuItem>
<MenuItem
onClick={() => {
if (user.isAuthenticated()) {
if (auth.isAuthenticated()) {
log.debug('Destroy and cleanup the WebSocket connection');
Workspaces.disconnect();

user.signout();
auth.signout();

// Remember current location
history.replace(location.pathname);
Expand Down
2 changes: 1 addition & 1 deletion src/app/containers/Home/CreateWorkspace/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class CreateWorkspace extends PureComponent {

componentDidMount() {
this._mounting = true;
this.controller.connect(auth.host, auth.options, () => {
this.controller.connect(auth.host, auth.socket, () => {
this.addControllerEvents();
this.refreshPorts();
this._mounting = false;
Expand Down
62 changes: 62 additions & 0 deletions src/app/containers/Login/Callback.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React, { PureComponent } from 'react';
import { CallbackComponent } from 'redux-oidc';
import { Redirect } from 'react-router-dom';
import auth from 'app/lib/auth';
import { connect } from 'react-redux';
import styles from './index.styl';

class Callback extends PureComponent {
state = { error: null, success: false, ready: false };

handleSuccess(oidc) {
this.setState({ success: true });

auth.signin(oidc).then((authorized) => {
if (authorized) {
this.setState({ ready: true });
} else {
this.setState({ error: { message: 'Failed to authenticate.' } });
}
});
}

handleError(err) {
this.setState({ error: err });
}

renderInner() {
if (this.state.error) {
const msg = this.state.error.message ?? this.state.error;
return <Redirect to={`/login?error=${msg}`} />;
}

if (this.state.success) {
if (!this.state.ready) {
return <div>Loading Profile...</div>;
}
return <Redirect to="/" />;
}

return (
<CallbackComponent
userManager={auth.manager}
successCallback={(s) => this.handleSuccess(s)}
errorCallback={(e) => this.handleError(e)}
>
<div>Authenticating...</div>
</CallbackComponent>
);
}

render() {
return (
<div className={styles.container}>
<div className={styles.login} style={{ padding: '20px' }}>
{this.renderInner()}
</div>
</div>
);
}
}

export default connect()(Callback);

0 comments on commit f7ce9e4

Please sign in to comment.