Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import * as React from 'react';
  2. import { createContext } from '../lib/context-utils';
  3.  
  4. interface User {
  5. [key: string]: any // idk yet
  6. }
  7.  
  8. interface Auth {
  9. token: string
  10. user: User
  11. }
  12.  
  13. export const AuthContext = createContext('authContext', {
  14. auth: undefined as Auth | undefined,
  15. login: undefined as ((auth: Auth) => void) | undefined,
  16. logout: undefined as (() => void) | undefined
  17. })
  18.  
  19. interface Props {
  20. children?: React.ReactNode
  21. }
  22.  
  23. interface State {
  24. auth: Auth | undefined
  25. }
  26.  
  27. class AuthProvider extends React.PureComponent<Props, State> {
  28. readonly state: State = {
  29. auth: undefined,
  30. }
  31.  
  32. login = (auth: Auth) => {
  33. this.setState({ auth })
  34. }
  35.  
  36. logout = () => {
  37. this.setState({ auth: undefined })
  38. }
  39.  
  40. render() {
  41. return (
  42. <AuthContext.Provider value={this.authContext}>
  43. {this.props.children}
  44. </AuthContext.Provider>
  45. )
  46. }
  47.  
  48. get authContext() {
  49. return {
  50. auth: this.state.auth,
  51. login: this.login,
  52. logout: this.logout,
  53. }
  54. }
  55. }
  56.  
  57. export default AuthProvider
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement