Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Dashboard PV278
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Polaško
Dashboard PV278
Commits
8882966d
There was an error fetching the commit references. Please try again later.
Commit
8882966d
authored
4 years ago
by
Martin Maroši
Browse files
Options
Downloads
Patches
Plain Diff
Add method to update user profile.
parent
d1e65fff
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/react-auth0-spa.js
+22
-0
22 additions, 0 deletions
src/react-auth0-spa.js
src/views/Profile.js
+4
-1
4 additions, 1 deletion
src/views/Profile.js
with
26 additions
and
1 deletion
src/react-auth0-spa.js
+
22
−
0
View file @
8882966d
import
React
,
{
useState
,
useEffect
,
useContext
}
from
"
react
"
;
import
createAuth0Client
from
"
@auth0/auth0-spa-js
"
;
import
PropTypes
from
"
prop-types
"
;
import
Api
from
"
./utils/api
"
;
const
DEFAULT_REDIRECT_CALLBACK
=
()
=>
window
.
history
.
replaceState
({},
document
.
title
,
window
.
location
.
pathname
);
...
...
@@ -48,6 +49,7 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_
setPopupOpen
(
false
);
}
const
user
=
await
auth0Client
.
getUser
();
auth0Client
.
crossOriginVerification
();
setUser
(
user
);
setIsAuthenticated
(
true
);
};
...
...
@@ -60,6 +62,25 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_
setIsAuthenticated
(
true
);
setUser
(
user
);
};
const
updateProfile
=
(
data
)
=>
Api
.
get
(
"
/rest/auth
"
).
then
(([{
_id
,
...
body
}])
=>
fetch
(
"
https://dev-8ihcso-1.auth0.com/oauth/token
"
,
{
method
:
"
POST
"
,
headers
:
{
"
content-type
"
:
"
application/json
"
},
body
:
JSON
.
stringify
(
body
)
})
.
then
((
data
)
=>
data
.
json
())
.
then
(({
access_token
})
=>
fetch
(
`https://dev-8ihcso-1.auth0.com/api/v2/users/
${
user
.
sub
}
`
,
{
method
:
"
PATCH
"
,
headers
:
{
Authorization
:
`Bearer
${
access_token
}
`
,
"
Content-Type
"
:
"
application/json
"
},
body
:
JSON
.
stringify
(
data
)
})
.
then
((
user
)
=>
user
.
json
())
.
then
((
user
)
=>
setUser
(
user
))
)
);
return
(
<
Auth0Context
.
Provider
value
=
{{
...
...
@@ -69,6 +90,7 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_
popupOpen
,
loginWithPopup
,
handleRedirectCallback
,
updateProfile
,
getIdTokenClaims
:
(...
p
)
=>
auth0Client
.
getIdTokenClaims
(...
p
),
loginWithRedirect
:
(...
p
)
=>
auth0Client
.
loginWithRedirect
(...
p
),
getTokenSilently
:
(...
p
)
=>
auth0Client
.
getTokenSilently
(...
p
),
...
...
This diff is collapsed.
Click to expand it.
src/views/Profile.js
+
4
−
1
View file @
8882966d
...
...
@@ -4,7 +4,9 @@ import Loading from "../components/Loading";
import
{
useAuth0
}
from
"
../react-auth0-spa
"
;
const
Profile
=
()
=>
{
const
{
loading
,
user
}
=
useAuth0
();
const
{
loading
,
user
,
updateProfile
}
=
useAuth0
();
const
handleNameUpdate
=
()
=>
updateProfile
({
user_metadata
:
{
name
:
"
Karlik
"
}
});
if
(
loading
||
!
user
)
{
return
<
Loading
/>
;
...
...
@@ -12,6 +14,7 @@ const Profile = () => {
return
(
<
div
>
<
button
onClick
=
{
handleNameUpdate
}
>
Click
<
/button
>
<
img
src
=
{
user
.
picture
}
alt
=
"
Profile
"
/>
<
div
>
<
h2
>
{
user
.
name
}
<
/h2
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment