Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import * as RB from 'react-bootstrap';
  4. import $ from "jquery";
  5.  
  6. export class Modal extends React.Component {
  7.  
  8. constructor(props) {
  9. super(props);
  10. }
  11.  
  12. render(){
  13. return(
  14. <div>
  15. <RB.Modal show={this.props.showModal} onHide={this.props.close}>
  16. <RB.Modal.Header closeButton>
  17. <RB.Modal.Title>Enter your credentials</RB.Modal.Title>
  18. </RB.Modal.Header>
  19. <RB.Modal.Body>
  20. <RB.Row>
  21. <RB.Col sm={6}>
  22. <h4>Sign Up</h4>
  23. <RB.FormGroup>
  24. <RB.FormControl ref="username" type="text" placeholder="choose a username" />
  25. </RB.FormGroup>
  26. <RB.FormGroup>
  27. <RB.FormControl ref="password" type="password" placeholder="password" />
  28. </RB.FormGroup>
  29. <RB.FormGroup>
  30. <RB.FormControl ref="passwordVerify" type="password" placeholder="verify password" />
  31. </RB.FormGroup>
  32. <RB.FormGroup>
  33. <RB.FormControl ref="email" type="text" placeholder="email" />
  34. </RB.FormGroup>
  35. </RB.Col>
  36. <RB.Col sm={6}>
  37. <h4>Sign In</h4>
  38. <RB.Form id="signInForm" action="/loginAuth" method="get">
  39. <RB.FormGroup>
  40. <RB.FormControl type="text" placeholder="username" name="username" />
  41. </RB.FormGroup>
  42. <RB.FormGroup>
  43. <RB.FormControl type="password" placeholder="password" name="password" />
  44. </RB.FormGroup>
  45. </RB.Form>
  46. </RB.Col>
  47. </RB.Row>
  48. </RB.Modal.Body>
  49.  
  50. <RB.Modal.Footer>
  51. <RB.Col sm={6}>
  52. <RB.Button onClick={this.props.signUp}>Sign Up</RB.Button>
  53. </RB.Col>
  54. <RB.Col sm={6}>
  55. <RB.Button bsStyle="primary" type="submit" form="signInForm">Sign In</RB.Button>
  56. </RB.Col>
  57. </RB.Modal.Footer>
  58. </RB.Modal>
  59. </div>
  60. );
  61. }
  62. }
  63.  
  64.  
  65. export class Header extends React.Component {
  66.  
  67. constructor(props) {
  68. super(props);
  69. this.state = { showModal: false };
  70. }
  71.  
  72. close() {
  73. this.setState({ showModal: false });
  74. }
  75.  
  76. open() {
  77. this.setState({ showModal: true });
  78. }
  79.  
  80. signUp() {
  81. var signUpData = {
  82. username : ReactDOM.findDOMNode(this.refs.username).value,
  83. password : ReactDOM.findDOMNode(this.refs.password).value,
  84. passwordVerify : ReactDOM.findDOMNode(this.refs.passwordVerify).value,
  85. email : ReactDOM.findDOMNode(this.refs.email).value
  86. };
  87.  
  88. //Verify all sign-up data before passing it to the server
  89. if(signUpData.username == null || signUpData.username == "") {
  90. alert("Please enter a username!");
  91. }
  92. else if(signUpData.password == null || signUpData.password == "") {
  93. alert("Please enter a password!");
  94. }
  95. else if(signUpData.password != signUpData.passwordVerify) {
  96. alert("Password entries don't match!");
  97. }
  98. else if(signUpData.email == null || signUpData.email == "") {
  99. alert("Please enter your email address!");
  100. }
  101. else if (signUpData.email !== null || signUpData.email !== "") {
  102. var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  103. if(!re.test(signUpData.email)) {
  104. alert("Not a valid email address!");
  105. }
  106. else {
  107. $.post("/api/v1/newuser", signUpData, function(response){
  108. if(response == "invalid") {
  109. alert("Either this email or this username is already in use!");
  110. }
  111. else {
  112. $.get("/loginAuth", signUpData, function(){
  113. window.location.href= "./";
  114. });
  115. }
  116. });
  117. }
  118. }
  119. }
  120.  
  121. render() {
  122.  
  123. var authRender = null;
  124. if( window.user == "" ) {
  125. authRender =
  126. <div>
  127. <RB.Navbar.Form pullRight>
  128. <RB.ButtonGroup>
  129. <RB.Button onClick={this.open.bind(this)}>Sign In</RB.Button>
  130. </RB.ButtonGroup>
  131. </RB.Navbar.Form>
  132. </div>;
  133. } else {
  134. authRender =
  135. <div>
  136. <RB.Navbar.Form pullRight>
  137. <RB.ButtonGroup>
  138. <RB.Button href="/logout">Sign Out</RB.Button>
  139. </RB.ButtonGroup>
  140. </RB.Navbar.Form>
  141. <RB.Nav pullRight>
  142. <RB.NavItem eventKey={3} href="../submit">Submit</RB.NavItem>
  143. <RB.NavItem eventKey={4} href="../profile">Profile</RB.NavItem>
  144. </RB.Nav>
  145. </div>;
  146. }
  147.  
  148.  
  149. return (
  150. <div>
  151. <RB.Row>
  152. <RB.PageHeader>
  153. ReactMB <small>Built on React and Node</small>
  154. </RB.PageHeader>
  155. <RB.Navbar>
  156. <RB.Navbar.Header>
  157. <RB.Navbar.Brand>
  158. <a href="/">ReactMB</a>
  159. </RB.Navbar.Brand>
  160. </RB.Navbar.Header>
  161. <RB.Nav>
  162. <RB.NavItem eventKey={1} href="#">Popular</RB.NavItem>
  163. <RB.NavItem eventKey={2} href="#">Newest</RB.NavItem>
  164. </RB.Nav>
  165. {authRender}
  166. </RB.Navbar>
  167. </RB.Row>
  168.  
  169. <Modal show={this.state.showModal} onHide={this.close.bind(this)} onClick={this.signUp.bind(this)}/>
  170. </div>
  171. );
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement