Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User registration, login, logout code using gun db #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import './App.css';
import UserLogin from './components/userlogin';
import Home from './components/home';
import UserRegistration from './components/userregistration';
import UserLogout from './components/userlogout';

import Gun from "gun";
require('gun/sea');

class App extends Component {
render() {
Expand All @@ -24,13 +28,17 @@ class App extends Component {
<li className="nav-item">
<Link to={'/'} className="nav-link">Home</Link>
</li>
<li className="nav-item">
<Link to={'/logout'} className="nav-link">Log out</Link>
</li>
</ul>
</div>
</nav> <br/>
<h2>DGstudio App</h2> <br/>
<Switch>
<Route exact path='/newUser' component={ UserRegistration } />
<Route path='/login' component={ UserLogin } />
<Route path='/logout' component={ UserLogout }/>
<Route path='/' component={ Home } />
</Switch>
</div>
Expand Down
32 changes: 32 additions & 0 deletions src/components/UserProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { Component } from "react";
import Gun from "gun"
require('gun/sea');

var UserProfile = (function() {
var full_name = "";

var getName = function() {
return full_name; // Or pull this from cookie/localStorage
};

var setName = function(name) {
console.log('inside setname method for session')
full_name = name;
// Also set this in cookie/localStorage
};

return {
getName: getName,
setName: setName
}

})();

export function authHeader() {
var gun = Gun()
var user = gun.user()
var auth = user.auth('')
}

export default UserProfile;

5 changes: 4 additions & 1 deletion src/components/home.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React, { Component } from 'react';
import UserProfile from './UserProfile';
import Gun from "gun"
require('gun/sea');

export default class Home extends Component {
render() {
return (
<div>
<p>Welcome to Home Component!!</p>
<p>Welcome { UserProfile.getName() }to Home Component !!</p>
</div>
)
}
Expand Down
18 changes: 16 additions & 2 deletions src/components/userlogin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Gun from "gun"
require('gun/sea');

export default class UserLogin extends Component {

Expand All @@ -8,7 +10,9 @@ export default class UserLogin extends Component {
this.state ={
username:'',
email:'',
password: ''
password: '',
submitted: false,
loading: false,
};
this.onChangeUserName = this.onChangeUserName.bind(this);
this.onChangeEmail = this.onChangeEmail.bind(this);
Expand All @@ -31,8 +35,18 @@ export default class UserLogin extends Component {
})
}

onSubmit(e) {
onSubmit = e=> {
e.preventDefault();
var gun = Gun();
var user = gun.user();
var userthis = this;
user.auth(this.state.username, this.state.password, function(ack){ if(ack.err){ alert("wrong"); }

else {

userthis.props.history.push("/");
} })

console.log(`The values are ${this.state.email}, ${this.state.username}, and ${this.state.password}`)
this.setState({
email: '',
Expand Down
27 changes: 27 additions & 0 deletions src/components/userlogout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { Component } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Gun from "gun"
require('gun/sea');

export default class UserLogout extends Component {

constructor(props){
super(props);


}

componentDidMount() {

var gun = Gun();
var user = gun.user();
user.leave();
this.props.history.push("/login");
}

render(){
return true
}

}

5 changes: 5 additions & 0 deletions src/components/userregistration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { Component } from 'react';
import '../../node_modules/bootstrap/dist/css/bootstrap.min.css';

import Gun from "gun"
require('gun/sea');

export default class UserRegistration extends Component {
constructor(props){
Expand Down Expand Up @@ -45,6 +47,9 @@ export default class UserRegistration extends Component {
}

onSubmit(e) {
var gun = Gun()
var user = gun.user()
user.create(this.state.email,this.state.password)
e.preventDefault();
console.log(`The values are ${this.state.email}, ${this.state.username}, and ${this.state.password}`)
this.setState({
Expand Down