Guest User

Untitled

a guest
Nov 10th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. import React, { Component, Fragment } from 'react'
  2. import {
  3. NavLink,
  4. Link,
  5. BrowserRouter as Router,
  6. Route,
  7. Switch,
  8. Redirect,
  9. } from 'react-router-dom'
  10. import FeedPage from './FeedPage'
  11. import DraftsPage from './DraftsPage'
  12. import CreatePage from './CreatePage'
  13. import DetailPage from './DetailPage'
  14. import LoginPage from './LoginPage'
  15. import SignupPage from './SignupPage'
  16. import PageNotFound from './PageNotFound'
  17. import LogoutPage from './LogoutPage'
  18. import { AUTH_TOKEN } from '../constant'
  19. import { isTokenExpired } from '../helper/jwtHelper'
  20. import { graphql } from 'react-apollo'
  21. import { gql } from 'apollo-boost'
  22.  
  23. const ProtectedRoute = ({ component: Component, token, ...rest }) => {
  24. return token ? (
  25. <Route {...rest} render={matchProps => <Component {...matchProps} />} />
  26. ) : (
  27. <Redirect to="/login" />
  28. )
  29. }
  30.  
  31. class RootContainer extends Component {
  32. constructor(props) {
  33. super(props)
  34. this.refreshTokenFn = this.refreshTokenFn.bind(this)
  35.  
  36. this.state = {
  37. token: props.token,
  38. }
  39. }
  40.  
  41. refreshTokenFn(data = {}) {
  42. const token = data.AUTH_TOKEN
  43.  
  44. if (token) {
  45. localStorage.setItem(AUTH_TOKEN, token)
  46. } else {
  47. localStorage.removeItem(AUTH_TOKEN)
  48. }
  49.  
  50. this.setState({
  51. token: data.AUTH_TOKEN,
  52. })
  53. }
  54.  
  55. bootStrapData() {
  56. try {
  57. const token = localStorage.getItem(AUTH_TOKEN)
  58. if (token !== null && token !== undefined) {
  59. const expired = isTokenExpired(token)
  60. if (!expired) {
  61. this.setState({ token: token })
  62. } else {
  63. localStorage.removeItem(AUTH_TOKEN)
  64. this.setState({ token: null })
  65. }
  66. }
  67. } catch (e) {
  68. console.log('')
  69. }
  70. }
  71.  
  72. //verify localStorage check
  73. componentDidMount() {
  74. this.bootStrapData()
  75. }
  76.  
  77. render() {
  78. return (
  79. <Router>
  80. <Fragment>
  81. {this.renderNavBar()}
  82. {this.renderRoute()}
  83. </Fragment>
  84. </Router>
  85. )
  86. }
  87.  
  88. renderNavBar() {
  89. return (
  90. <nav className="pa3 pa4-ns">
  91. <div className="stateText">
  92. STATE:&nbsp;
  93.  
  94. <strong>
  95. {this.state.token ? (
  96. <div>
  97. Logged <em>IN</em>!!!
  98. </div>
  99. ) : (
  100. <div>
  101. Logged <em>OUT</em>!!!
  102. </div>
  103. )}
  104. </strong>
  105. </div>&nbsp;
  106. ======>
  107. {this.state.token ? (
  108. <div
  109. onClick={() => {
  110. this.refreshTokenFn &&
  111. this.refreshTokenFn({
  112. [AUTH_TOKEN]: null,
  113. })
  114. window.location.href = '/'
  115. }}
  116. className="f6 link dim br1 ba ph3 pv2 fr mb2 dib black">
  117. Logout
  118. </div>
  119. ) : (
  120. <Link
  121. to="/login"
  122. className="f6 link dim br1 ba ph3 pv2 fr mb2 dib black"
  123. >
  124. Login
  125. </Link>
  126. )}
  127. </nav>
  128. )
  129. }
  130.  
  131. renderRoute() {
  132. return (
  133. <div className="fl w-100 pl4 pr4">
  134. <Switch>
  135. <Route exact path="/" component={FeedPage} />
  136. <Route
  137. token={this.state.token}
  138. path="/login"
  139. render={props => <LoginPage refreshTokenFn={this.refreshTokenFn} />}
  140. />
  141. <Route path="/logout" component={LogoutPage} />
  142. <Route component={PageNotFound} />
  143. </Switch>
  144. </div>
  145. )
  146. }
  147. }
  148.  
  149. export default RootContainer
  150.  
  151.  
  152.  
  153. // ==================== The subsequent LoginPage is below ============================>
  154.  
  155.  
  156.  
  157.  
  158. import React, { Component } from 'react'
  159. import { withRouter } from 'react-router-dom'
  160. import { graphql } from 'react-apollo'
  161. import { gql } from 'apollo-boost'
  162. import { AUTH_TOKEN } from '../constant'
  163.  
  164. class LoginPage extends Component {
  165. state = {
  166. username: '',
  167. password: '',
  168. }
  169.  
  170. render() {
  171. return (
  172. <div className="pa4 flex justify-center bg-white">
  173. <div>
  174. Username: bobjones
  175. <br />
  176. Password: abc12345678
  177. <input
  178. autoFocus
  179. className="w-100 pa2 mv2 br2 b--black-20 bw1"
  180. placeholder="Email"
  181. type="text"
  182. onChange={e => this.setState({ username: e.target.value })}
  183. value={this.state.username}
  184. />
  185. <input
  186. autoFocus
  187. className="w-100 pa2 mv2 br2 b--black-20 bw1"
  188. placeholder="Password"
  189. type="password"
  190. onChange={e => this.setState({ password: e.target.value })}
  191. value={this.state.password}
  192. />
  193.  
  194. <button
  195. className="pa3 bg-black-10 bn dim ttu pointer"
  196. onClick={this._login}>
  197. GO
  198. </button>
  199.  
  200. </div>
  201. </div>
  202. )
  203. }
  204.  
  205. _login = async e => {
  206. const { username, password } = this.state
  207. this.props
  208. .loginMutation({
  209. variables: {
  210. username,
  211. password,
  212. },
  213. })
  214. .then(result => {
  215. const token = result.data.login.authToken
  216.  
  217. this.props.refreshTokenFn &&
  218. this.props.refreshTokenFn({
  219. [AUTH_TOKEN]: token,
  220. })
  221. alert('Auth Successful')
  222. {/*this.props.history.replace('/')
  223. window.location.reload()*/}
  224. })
  225. .catch(err => {
  226. alert('Incorrect credentials')
  227. })
  228. }
  229. }
  230.  
  231. const LOGIN_USER_MUTATION = gql`
  232. mutation loginMutation($username: String!, $password: String!) {
  233. login (input: {
  234. username: $username
  235. password: $password
  236. clientMutationId: ""
  237. }) {
  238. authToken
  239. }
  240. }
  241. `
  242.  
  243. export default graphql(LOGIN_USER_MUTATION, { name: 'loginMutation' })(
  244. withRouter(LoginPage),
  245. )
Add Comment
Please, Sign In to add comment