Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import React, { useState, useEffect, useContext } from 'react'
  2. import createAuth0Client from '@auth0/auth0-spa-js'
  3.  
  4. const DEFAULT_REDIRECT_CALLBACK = () =>
  5. window.history.replaceState({}, document.title, window.location.pathname)
  6.  
  7. export const Auth0Context = React.createContext()
  8. export const useAuth0 = () => useContext(Auth0Context)
  9.  
  10. let _initOptions
  11.  
  12. const getAuth0Client = () => {
  13. return new Promise(async (resolve, reject) => {
  14. let client
  15. if (!client) {
  16. try {
  17. client = await createAuth0Client(_initOptions)
  18. resolve(client)
  19. } catch (e) {
  20. reject(new Error('getAuth0Client Error', e))
  21. }
  22. }
  23. })
  24. }
  25.  
  26. export const getTokenSilently = async (...p) => {
  27. const client = await getAuth0Client()
  28. return await client.getTokenSilently(...p)
  29. }
  30. export const Auth0Provider = ({
  31. children,
  32. onRedirectCallback = DEFAULT_REDIRECT_CALLBACK,
  33. ...initOptions
  34. }) => {
  35. const [isAuthenticated, setIsAuthenticated] = useState()
  36. const [user, setUser] = useState()
  37. const [auth0Client, setAuth0] = useState()
  38. const [loading, setLoading] = useState(true)
  39. const [popupOpen, setPopupOpen] = useState(false)
  40.  
  41. useEffect(() => {
  42. const initAuth0 = async () => {
  43. _initOptions = initOptions
  44. const client = await getAuth0Client(initOptions)
  45. setAuth0(client)
  46. if (window.location.search.includes('code=')) {
  47. const {
  48. appState
  49. } = await client.handleRedirectCallback()
  50. onRedirectCallback(appState)
  51. }
  52. const isAuthenticated = await client.isAuthenticated()
  53. setIsAuthenticated(isAuthenticated)
  54.  
  55. if (isAuthenticated) {
  56. const user = await client.getUser()
  57. setUser(user)
  58. }
  59.  
  60. setLoading(false)
  61. }
  62. initAuth0()
  63. // eslint-disable-next-line
  64. }, [])
  65.  
  66. const loginWithPopup = async (params = {}) => {
  67. setPopupOpen(true)
  68. try {
  69. await auth0Client.loginWithPopup(params)
  70. } catch (error) {
  71. console.error(error)
  72. } finally {
  73. setPopupOpen(false)
  74. }
  75. const user = await auth0Client.getUser()
  76. setUser(user)
  77. setIsAuthenticated(true)
  78. }
  79.  
  80. const handleRedirectCallback = async () => {
  81. setLoading(true)
  82. await auth0Client.handleRedirectCallback()
  83. const user = await auth0Client.getUser()
  84. setLoading(false)
  85. setIsAuthenticated(true)
  86. setUser(user)
  87. }
  88. return (
  89. <Auth0Context.Provider
  90. value={{
  91. isAuthenticated,
  92. user,
  93. loading,
  94. popupOpen,
  95. loginWithPopup,
  96. handleRedirectCallback,
  97. getIdTokenClaims: (...p) => auth0Client.getIdTokenClaims(...p),
  98. loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),
  99. getTokenWithPopup: (...p) => auth0Client.getTokenWithPopup(...p),
  100. logout: (...p) => auth0Client.logout(...p)
  101. }}
  102. >
  103. {children}
  104. </Auth0Context.Provider>
  105. )
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement