Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
order-management-react
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ascend
order-management-react
Commits
f638aae1
Commit
f638aae1
authored
May 11, 2021
by
Alex Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-111]
♻
️ Refactor custom hook 'useGoogleAuth' with types & service call (
@asegers
)
parent
0db4d364
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
35 deletions
+46
-35
useGoogleAuth.tsx
src/hooks/useGoogleAuth.tsx
+46
-35
No files found.
src/hooks/useGoogleAuth.tsx
View file @
f638aae1
import
React
from
'react'
import
{
useGoogleLogin
,
useGoogleLogout
}
from
'react-google-login'
import
{
createContext
,
useContext
,
useEffect
,
useState
,
FC
}
from
'react'
import
{
GoogleLoginResponse
,
useGoogleLogin
,
useGoogleLogout
}
from
'react-google-login'
import
{
useHistory
}
from
'react-router-dom'
import
{
Manager
}
from
'Manager'
const
GoogleAuthContext
=
React
.
createContext
<
any
>
({});
import
{
ManagerService
}
from
'services'
import
{
tokenStorage
}
from
'utils'
export
const
useGoogleAuth
=
()
=>
React
.
useContext
(
GoogleAuthContext
);
export
default
useGoogleAuth
;
export
const
GoogleAuthProvider
:
React
.
FC
=
({
children
})
=>
{
const
{
push
:
historyPush
}
=
useHistory
();
const
[
isSignedIn
,
setIsSignedIn
]
=
React
.
useState
(
false
)
const
[
staySignedIn
,
setStaySignedIn
]
=
React
.
useState
(
true
)
const
[
manager
,
setManager
]
=
React
.
useState
<
Manager
|
null
>
()
const
[
authError
,
setAuthError
]
=
React
.
useState
<
String
|
null
>
()
const
GoogleAuthContext
=
createContext
<
any
>
({});
const
{
signIn
,
loaded
,
}
=
useGoogleLogin
({
export
const
useGoogleAuth
=
()
=>
useContext
(
GoogleAuthContext
);
export
default
useGoogleAuth
;
export
const
GoogleAuthProvider
:
FC
=
({
children
})
=>
{
const
{
push
:
historyPush
}
=
useHistory
();
const
[
isSignedIn
,
setIsSignedIn
]
=
useState
(
false
)
const
[
staySignedIn
,
setStaySignedIn
]
=
useState
(
true
)
const
[
manager
,
setManager
]
=
useState
<
Manager
|
null
>
()
const
[
authError
,
setAuthError
]
=
useState
<
string
|
null
>
()
const
[
accessToken
,
setAccessToken
]
=
useState
<
string
|
null
>
()
const
{
signIn
,
loaded
}
=
useGoogleLogin
({
clientId
:
(
process
.
env
.
REACT_APP_GOOGLE_CLIENT_ID
as
string
),
onSuccess
:
(
res
:
any
)
=>
{
const
{
email
,
familyName
:
lastName
,
givenName
:
firstName
,
googleId
,
imageUrl
}
=
res
.
profileObj
;
setManager
(({
profileObj
:
{
email
,
familyName
:
lastName
,
givenName
:
firstName
,
googleId
,
imageUrl
},
tokenId
:
token
}
=
res
;
const
managerData
:
Manager
=
{
email
,
firstName
,
lastName
,
googleId
,
imageUrl
}))
}
setManager
(
managerData
)
setAccessToken
(
token
)
console
.
log
(
token
)
// ManagerService.existByEmail(email) // GET api/managers/[email]
// .then((exists: boolean) => {
// if (exists) {
// ManagerService.isLoggedin(email)
// } else {
// ManagerService.create([managerData])
// }
// }).finally(() => {
setIsSignedIn
(
true
);
historyPush
(
"/orders"
);
// })
ManagerService
.
authenticate
(
token
)
.
then
(()
=>
{
setIsSignedIn
(
true
)
historyPush
(
"/orders"
)
})
},
onFailure
:
(
err
)
=>
{
console
.
error
(
"GOOGLE AUTH SIGN-IN ERROR: "
,
err
)
setAuthError
(
`Ooops. Something went wrong:
${
err
?.
message
}
`)
;
setAuthError
(
`Ooops. Something went wrong:
${
err
?.
message
}
`)
},
isSignedIn: staySignedIn,
cookiePolicy: 'single_host_origin'
...
...
@@ -58,16 +64,21 @@ export const GoogleAuthProvider: React.FC = ({ children }) => {
const { signOut } = useGoogleLogout({
clientId: (process.env.REACT_APP_GOOGLE_CLIENT_ID as string),
onLogoutSuccess: () => {
setIsSignedIn(false)
;
setIsSignedIn(false)
historyPush("/")
},
onFailure: () => {
console.error("A GOOGLE AUTH SIGN-OUT ERROR OCCURED")
setAuthError("Ooops. Something went wrong")
;
setAuthError("Ooops. Something went wrong")
},
})
useEffect(() => {
if (accessToken) tokenStorage.set(accessToken);
}, [accessToken])
const authContextValues = {
accessToken,
authError,
isSignedIn,
loaded,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment