DAVIDS/src/public/components/authenticate.js

92 lines
3.3 KiB
JavaScript

/*
* DAVIDS lets you find and view duplicates in your Spotify playlists.
* Copyright (C) 2016 Gabriel Augendre <gabriel@augendre.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
var React = require('react');
var ReactBootstrap = require('react-bootstrap');
var $ = require('jquery');
var Authenticate = React.createClass({
logout: function () {
this.props.refreshAuth(null, null);
window.location.href = "/";
},
refreshToken: function (event) {
event.preventDefault();
var self = this;
$.ajax({
url: '/refresh_token',
data: {
'refresh_token': self.props.auth.refresh_token
},
success: function (data) {
self.props.refreshAuth(data.access_token, self.props.auth.refresh_token);
},
error: function (xhr, status, err) {
console.error(status, err);
}
});
},
render: function () {
var auth;
var Nav = ReactBootstrap.Nav,
NavItem = ReactBootstrap.NavItem,
Navbar = ReactBootstrap.Navbar;
if (this.props.auth.access_token == null) {
auth = (
<Nav pullRight>
<NavItem eventKey={1} href="/login">
<img className="spotify-logo" src="images/spotify-black.png" alt="Spotify Logo"/><strong>Login With Spotify</strong>
</NavItem>
</Nav>
);
}
else {
auth = (
<Nav pullRight>
<NavItem eventKey={2} onClick={this.refreshToken} href="#">Refresh my token</NavItem>
<NavItem eventKey={3} onClick={this.logout} href="#"><strong>Logout</strong></NavItem>
</Nav>
);
}
return (
<Navbar fluid={true}>
<Navbar.Header>
<Navbar.Brand>
<a href="/" className="text-capitalize">DAVIDS</a>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#" active={this.props.activeTab.finder}
onClick={this.props.showFinder}>Finder</NavItem>
<NavItem eventKey={1} href="#" active={this.props.activeTab.about}
onClick={this.props.showAbout}>About</NavItem>
</Nav>
{auth}
</Navbar.Collapse>
</Navbar>
);
}
});
module.exports = Authenticate;