Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
Sign up mutation (#24)
Browse files Browse the repository at this point in the history
* Adds working signup mutation

* Correctly handles sign in and out page refresh
  • Loading branch information
ncrmro authored Jan 24, 2017
1 parent 32f28b4 commit 04e63c2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
19 changes: 19 additions & 0 deletions bend/src/users/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,24 @@ def mutate_and_get_payload(cls, input, context, info):
return LogInUser(viewer, jwt_token)


class CreateUser(relay.ClientIDMutation):
class Input:
username = String(required=True)
password = String(required=True)

viewer = Field(UserNode)
jwt_token = String()

@classmethod
def mutate_and_get_payload(cls, input, context, info):
print("Logging user in", input, context, info)
username = input.get('username')
password = input.get('password')
viewer = User.objects.create_user(username=username, password=password)
jwt_token = loginUser(username, password)
return CreateUser(viewer, jwt_token)


class UserMutations(AbstractType):
login_user = LogInUser.Field()
create_user = CreateUser.Field()
2 changes: 1 addition & 1 deletion fend/src/client/components/App/AppComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class App extends React.Component {

render() {
const viewer = this.props.viewer;
const userLoggedIn = viewer.username && viewer.email ? true : false;
const userLoggedIn = viewer.username ? true : false;

return (
<div className={styles.root}>
Expand Down
6 changes: 4 additions & 2 deletions fend/src/client/components/Navbar/NavbarComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ export default class Navbar extends React.Component {
handleSignOut() {
localStorage.removeItem('jwtToken');
// Causes crash
//this.props.router.push('/');
this.props.router.push('/');
window.location.reload()

}

renderLoggedIn() {
return (
<Navigation>
<Link to="/" onClick={this.handleSignOut()}>Sign out</Link>
<Link to="/" onClick={this.handleSignOut}>Sign out</Link>
<Link to='/dashboard'>Dashboard</Link>

</Navigation>
Expand Down
20 changes: 17 additions & 3 deletions fend/src/client/components/Signup/SignupComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import Relay from "react-relay";
import {Grid, Cell, Textfield, Button} from "react-mdl";
import Page from "../Page/PageComponent";
//import SignupUserMutation from "./SignupUserMutation";
import SignupUserMutation from "./SignupUserMutation";


export default class Signup extends React.Component {
Expand All @@ -19,8 +19,22 @@ export default class Signup extends React.Component {
form.preventDefault();
const username = this.state.username;
const password = this.state.password;
const signupUserMutation = new SignupUserMutation({});
Relay.Store.commitUpdate(signupUserMutation);
const signupUserMutation = new SignupUserMutation({
username,
password
});
var onFailure = (transaction) => {
console.log("failure", transaction)
};

var onSuccess = (response) => {
console.log("Success", response);
const jwtToken = response.createUser.jwtToken;
localStorage.setItem('jwtToken', jwtToken);
this.props.router.push('/dashboard');
window.location.reload()
};
Relay.Store.commitUpdate(signupUserMutation, {onSuccess, onFailure});
};

render() {
Expand Down
43 changes: 23 additions & 20 deletions fend/src/client/components/Signup/SignupUserMutation.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import Relay from "react-relay";

class AddFeatureMutation extends Relay.Mutation {
class SignupUserMutation extends Relay.Mutation {

getMutation() {
return Relay.QL`
mutation { addFeature }
mutation { createUser }
`;
}

getVariables() {
return {
name: this.props.name,
description: this.props.description,
url: this.props.url
username: this.props.username,
password: this.props.password
};
}

getFatQuery() {
return Relay.QL`
fragment on AddFeaturePayload {
featureEdge,
viewer { features }
fragment on CreateUserPayload {
viewer { username, firstName, email }
}
`;
}

getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'viewer',
parentID: this.props.viewerId,
connectionName: 'features',
edgeName: 'featureEdge',
rangeBehaviors: {
'': 'append',
},
}];
}
return [{
type: 'REQUIRED_CHILDREN',
// Forces these fragments to be included in the query
children: [Relay.QL`
fragment on CreateUserPayload {
viewer {
id,
username,
email,
dateJoined,
},
jwtToken
}
`],
}];
}
}

export default AddFeatureMutation;
export default SignupUserMutation;

0 comments on commit 04e63c2

Please sign in to comment.