Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
vicotor
frontend
Commits
0bcef249
Commit
0bcef249
authored
Dec 24, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no CORS no more
parent
091537b9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
6 deletions
+16
-6
config.ts
configs/app/config.ts
+1
-0
buildUrl.ts
lib/api/buildUrl.ts
+15
-6
No files found.
configs/app/config.ts
View file @
0bcef249
...
...
@@ -90,6 +90,7 @@ const config = Object.freeze({
adButlerOn
:
getEnvValue
(
process
.
env
.
NEXT_PUBLIC_AD_ADBUTLER_ON
)
===
'
true
'
,
},
api
:
{
host
:
apiHost
,
endpoint
:
apiHost
?
`https://
${
apiHost
}
`
:
'
https://blockscout.com
'
,
socket
:
apiHost
?
`wss://
${
apiHost
}
`
:
'
wss://blockscout.com
'
,
basePath
:
stripTrailingSlash
(
getEnvValue
(
process
.
env
.
NEXT_PUBLIC_API_BASE_PATH
)
||
''
),
...
...
lib/api/buildUrl.ts
View file @
0bcef249
...
...
@@ -9,13 +9,22 @@ export default function buildUrl(
pathParams
?:
Record
<
string
,
string
>
,
queryParams
?:
Record
<
string
,
string
|
undefined
>
,
)
{
// FIXME was not able to figure out how to send CORS with credentials from localhost
// so for local development we use nextjs api as proxy server (only!)
const
baseUrl
=
appConfig
.
host
===
'
localhost
'
?
appConfig
.
baseUrl
:
(
resource
.
endpoint
||
appConfig
.
api
.
endpoint
);
// FIXME
// 1. I was not able to figure out how to send CORS with credentials from localhost
// unsuccessfully tried different ways, even custom local dev domain
// so for local development we have to use next.js api as proxy server
// 2. and there is an issue with API and csrf token
// for some reason API will reply with error "Bad request" to any PUT / POST CORS request
// even though valid csrf-token is passed in header
// we also can pass token in request body but in this case API will replay with "Forbidden" error
// @nikitosing said it will take a lot of time to debug this problem on back-end side, maybe he'll change his mind in future :)
// To sum up, we are using next.js proxy for all instances where app host is not the same as API host (incl. localhost)
// will need to change the condition if there are more micro services that need authentication and DB state changes
const
needProxy
=
appConfig
.
host
!==
appConfig
.
api
.
host
;
const
baseUrl
=
needProxy
?
appConfig
.
baseUrl
:
(
resource
.
endpoint
||
appConfig
.
api
.
endpoint
);
const
basePath
=
resource
.
basePath
!==
undefined
?
resource
.
basePath
:
appConfig
.
api
.
basePath
;
const
path
=
appConfig
.
host
===
'
localhost
'
?
'
/proxy
'
+
basePath
+
resource
.
path
:
basePath
+
resource
.
path
;
const
path
=
needProxy
?
'
/proxy
'
+
basePath
+
resource
.
path
:
basePath
+
resource
.
path
;
const
url
=
new
URL
(
compile
(
path
)(
pathParams
),
baseUrl
);
queryParams
&&
Object
.
entries
(
queryParams
).
forEach
(([
key
,
value
])
=>
{
...
...
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