From 660cc823d4f2446dbc3113b2de4d3164664303fb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 27 Jun 2018 16:32:53 +0300 Subject: fix social login --- vnext/src/api/index.js | 7 ++++--- vnext/src/components/LoginButton.js | 4 ++-- vnext/src/index.js | 10 ++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) (limited to 'vnext') diff --git a/vnext/src/api/index.js b/vnext/src/api/index.js index 130a5dc2..b190bc48 100644 --- a/vnext/src/api/index.js +++ b/vnext/src/api/index.js @@ -12,12 +12,13 @@ client.interceptors.request.use(config => { return config; }) -export function auth(username, password) { +export function me(username = '', password = '') { return new Promise((resolve, reject) => { + client.get('/me', { - headers: { + headers: username ? { 'Authorization': 'Basic ' + window.btoa(unescape(encodeURIComponent(username + ":" + password))) - } + } : {} }).then(response => { localStorage.visitor = JSON.stringify(response.data); resolve(response.data) diff --git a/vnext/src/components/LoginButton.js b/vnext/src/components/LoginButton.js index fbce6982..e59dabdf 100644 --- a/vnext/src/components/LoginButton.js +++ b/vnext/src/components/LoginButton.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import Icon from './Icon'; import Modal from './Modal'; -import { auth } from '../api'; +import { me } from '../api'; export default class LoginButton extends React.Component { constructor(props) { @@ -32,7 +32,7 @@ export default class LoginButton extends React.Component { } login = (event) => { event.preventDefault(); - auth(this.state.username, this.state.password) + me(this.state.username, this.state.password) .then(response => { this.toggleModal(); this.props.onAuth(response); diff --git a/vnext/src/index.js b/vnext/src/index.js index 9527178e..9dbf21e7 100644 --- a/vnext/src/index.js +++ b/vnext/src/index.js @@ -15,11 +15,14 @@ import Footer from './components/Footer'; import Avatar from './components/Avatar'; import Header from './components/Header'; +import { me } from './api'; + class App extends React.Component { constructor(props) { super(props); let params = qs.parse(window.location.search) if (params.hash) { + localStorage.visitor = JSON.stringify({uid: 0, hash: params.hash}); window.history.replaceState({}, document.title, `${window.location.protocol}//${window.location.host}${window.location.pathname}`) } this.state = { @@ -30,6 +33,13 @@ class App extends React.Component { }; } + componentDidMount() { + const {hash, uid} = this.state.visitor; + if (uid === 0 && hash) { + me().then(visitor => this.auth(visitor)) + } + } + render() { const user = this.state.visitor; return ( -- cgit v1.2.3