【发布时间】:2019-08-11 18:21:57
【问题描述】:
我的 docker 应用中有这个文件夹结构:
nginx/
dev.conf
client/
src/
Spotify.js
这就是我从client 调用redirect uri 的方式:
Spotify.js
class SpotifyAuth extends Component {
constructor () {
super()
this.handleRedirect = this.handleRedirect.bind(this)
}
getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
e = r.exec(q)
while (e) {
hashParams[e[1]] = decodeURIComponent(e[2]);
e = r.exec(q);
}
return hashParams;
}
handleRedirect = (e) => {
axios.get("http://localhost:8888" )
.then(response => response.json())
.then(data => console.log(data))
.catch((err) => { console.log(err); });
//e.preventDefault();
}
render () {
return (
<div className='button__container'>
<button className='button' onClick={this.handleRedirect}
><strong>CONNECT YOUR SPOTIFY ACCOUNT</strong>
</button>
</div>
)
}
}
export default SpotifyAuth;
docker-compose-dev.yml
nginx:
build:
context: ./services/nginx
dockerfile: Dockerfile-dev
restart: always
ports:
- 80:80
- 8888:8888
depends_on:
- web
- client
client:
build:
context: ./services/client
dockerfile: Dockerfile-dev
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
- REACT_APP_WEB_SERVICE_URL=${REACT_APP_WEB_SERVICE_URL}
depends_on:
- web
nginx/dev.conf
server {
listen 80;
location / {
proxy_pass http://client:3000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
如何正确授权并处理来自Spotify.js的令牌?
编辑:
我已经尝试了以下答案提出的解决方案,首先使用 localhost:8888,然后使用 localhost:3000(两者都在 Spotify 开发中作为重定向 URL 有效)
它只适用于 8888,即便如此:
应用程序将token 转储到localStorage,但挂起并在浏览器上显示以下警报:
然后,如果我点击 OK,并且只有 IF,它会重定向。
怎么了?
【问题讨论】:
-
您是否有理由从
localhost传递到localhost:8888,以便可以进一步将用户重定向到 spotify? -
for
localhost我有 nginx 在位置client:3000进行监听,我将其用于另一个注册和登录流程(用于应用程序 REST API 本身,具有自己的令牌)。不过,我不知道这是否是最佳做法。 -
您可以更改在 Spotify 注册的 oauth 客户端的
redirect_uri吗? -
当然。如果您的解决方案需要这个,我会试一试。
标签: reactjs docker docker-compose spotify