Skip to content

Instantly share code, notes, and snippets.

@airgead73
Last active January 17, 2018 07:38
Show Gist options
  • Select an option

  • Save airgead73/22d14bfce7bbf2fe7fc0b4f91bee01d8 to your computer and use it in GitHub Desktop.

Select an option

Save airgead73/22d14bfce7bbf2fe7fc0b4f91bee01d8 to your computer and use it in GitHub Desktop.
Original client files
import React, { Component } from 'react';
import { Navbar, Button } from 'react-bootstrap';
import './App.css';
class App extends Component {
goTo(route) {
this.props.history.replace(`/${route}`)
}
login() {
this.props.auth.login();
}
logout() {
this.props.auth.logout();
}
render() {
const { isAuthenticated } = this.props.auth;
return (
<div>
<Navbar fluid>
<Navbar.Header>
<Navbar.Brand>
<a href="home">Home</a>
</Navbar.Brand>
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.goTo.bind(this, 'home')}
>
Home
</Button>
{
!isAuthenticated() && (
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.login.bind(this)}
>
Log In
</Button>
)
}
{
isAuthenticated() && (
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.goTo.bind(this, 'profile')}
>
Profile
</Button>
)
}
{
isAuthenticated() && (
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.logout.bind(this)}
>
Log Out
</Button>
)
}
</Navbar.Header>
</Navbar>
<div className="container">
{this.props.children}
</div>
</div>
);
}
}
export default App;
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
class Home extends Component {
login() {
this.props.auth.login();
}
render() {
const { isAuthenticated } = this.props.auth;
return (
<div className="container">
{
isAuthenticated() && (
<h4>
You are logged in! You can now view your{' '}
<Link to="profile">profile area</Link>
.
</h4>
)
}
{
!isAuthenticated() && (
<h4>
You are not logged in! Please{' '}
<a
style={{ cursor: 'pointer' }}
onClick={this.login.bind(this)}
>
Log In
</a>
{' '}to continue.
</h4>
)
}
</div>
);
}
}
export default Home;
import ReactDOM from 'react-dom';
import './index.css';
import 'bootstrap/dist/css/bootstrap.css';
import { makeMainRoutes } from './routes';
import registerServiceWorker from './registerServiceWorker';
const routes = makeMainRoutes();
ReactDOM.render(routes, document.getElementById('root'));
registerServiceWorker();
import React, { Component } from 'react';
import { Panel, ControlLabel, Glyphicon } from 'react-bootstrap';
import './Profile.css';
class Profile extends Component {
componentWillMount() {
this.setState({ profile: {} });
const { userProfile, getProfile } = this.props.auth;
if (!userProfile) {
getProfile((err, profile) => {
this.setState({ profile });
});
} else {
this.setState({ profile: userProfile });
}
}
render() {
const { profile } = this.state;
return (
<div className="container">
<div className="profile-area">
<h1>{profile.name}</h1>
<Panel header="Profile">
<img src={profile.picture} alt="profile" />
<div>
<ControlLabel><Glyphicon glyph="user" /> Nickname</ControlLabel>
<h3>{profile.nickname}</h3>
</div>
<pre>{JSON.stringify(profile, null, 2)}</pre>
</Panel>
</div>
</div>
);
}
}
export default Profile;
import React from 'react';
import { Redirect, Route, Router } from 'react-router-dom';
import App from './App';
import Home from './Home/Home';
import Profile from './Profile/Profile';
import Callback from './Callback/Callback';
import Auth from './Auth/Auth';
import history from './history';
const auth = new Auth();
const handleAuthentication = ({location}) => {
if (/access_token|id_token|error/.test(location.hash)) {
auth.handleAuthentication();
}
}
export const makeMainRoutes = () => {
return (
<Router history={history}>
<div>
<Route exact path="/" render={(props) => <App auth={auth} {...props} />} />
<Route path="/home" render={(props) => <Home auth={auth} {...props} />} />
<Route path="/profile" render={(props) => (
!auth.isAuthenticated() ? (
<Redirect to="/home"/>
) : (
<Profile auth={auth} {...props} />
)
)} />
<Route path="/callback" render={(props) => {
handleAuthentication(props);
return <Callback {...props} />
}}/>
</div>
</Router>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment