Skip to content

Commit 154bc0b

Browse files
author
swyx
committed
examples
1 parent 1b3252f commit 154bc0b

File tree

5 files changed

+41
-41
lines changed

5 files changed

+41
-41
lines changed

example/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
22

33
This shows an example of how to use the `netlify-identity-widget` with React.
44

5+
The source code is at https://github.com/netlify/netlify-identity-widget/tree/master/example
6+
7+
and it is deployed at: https://netlify-identity-widget-react-example.netlify.com
8+
59
For more info on the `netlify-identity-widget`, [find the repo here.](https://github.com/netlify/netlify-identity-widget/)

example/public/_redirects

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* /index.html 200

example/src/App.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import Protected from './Protected';
33
import Public from './Public';
44
import netlifyIdentity from 'netlify-identity-widget';
@@ -10,7 +10,7 @@ import {
1010
withRouter
1111
} from 'react-router-dom';
1212

13-
// https://reacttraining.com/react-router/web/example/auth-workflow
13+
// copied straight from https://reacttraining.com/react-router/web/example/auth-workflow
1414
////////////////////////////////////////////////////////////
1515
// 1. Click the public page
1616
// 2. Click the protected page
@@ -38,14 +38,30 @@ function AuthExample() {
3838
);
3939
}
4040

41+
const fakeAuth = {
42+
isAuthenticated: false,
43+
authenticate(cb) {
44+
this.isAuthenticated = true;
45+
netlifyIdentity.open();
46+
// setTimeout(cb, 100); // fake async
47+
netlifyIdentity.on('login', cb);
48+
},
49+
signout(cb) {
50+
this.isAuthenticated = false;
51+
// setTimeout(cb, 100);
52+
netlifyIdentity.logout();
53+
netlifyIdentity.on('logout', cb);
54+
}
55+
};
56+
4157
const AuthButton = withRouter(
4258
({ history }) =>
43-
netlifyIdentity.currentUser() ? (
59+
fakeAuth.isAuthenticated ? (
4460
<p>
4561
Welcome!{' '}
4662
<button
4763
onClick={() => {
48-
netlifyIdentity.logout().then(() => history.push('/'));
64+
fakeAuth.signout(() => history.push('/'));
4965
}}
5066
>
5167
Sign out
@@ -61,7 +77,7 @@ function PrivateRoute({ component: Component, ...rest }) {
6177
<Route
6278
{...rest}
6379
render={props =>
64-
netlifyIdentity.currentUser() ? (
80+
fakeAuth.isAuthenticated ? (
6581
<Component {...props} />
6682
) : (
6783
<Redirect
@@ -78,14 +94,13 @@ function PrivateRoute({ component: Component, ...rest }) {
7894

7995
class Login extends React.Component {
8096
state = { redirectToReferrer: false };
81-
componentDidMount = () => {
82-
netlifyIdentity.on('login', () =>
83-
this.setState({ redirectToReferrer: true })
84-
);
85-
};
97+
8698
login = () => {
87-
netlifyIdentity.open();
99+
fakeAuth.authenticate(() => {
100+
this.setState({ redirectToReferrer: true });
101+
});
88102
};
103+
89104
render() {
90105
let { from } = this.props.location.state || { from: { pathname: '/' } };
91106
let { redirectToReferrer } = this.state;

example/src/Protected.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import React from 'react';
2-
export default function Public() {
3-
return <h3>Public</h3>;
2+
import netlifyIdentity from 'netlify-identity-widget';
3+
export default function Protected() {
4+
const user = netlifyIdentity.currentUser();
5+
console.log({ user });
6+
return (
7+
<div>
8+
<h3>Protected Page</h3>
9+
You are logged in as <b>{user.email}</b>
10+
</div>
11+
);
412
}

example/src/index.js

-28
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,10 @@ import './index.css';
44
import App from './App';
55
// import registerServiceWorker from './registerServiceWorker';
66
import netlifyIdentity from 'netlify-identity-widget';
7-
// import {
8-
// BrowserRouter as Router,
9-
// Route,
10-
// Link,
11-
// Redirect,
12-
// withRouter
13-
// } from 'react-router-dom';
147

158
window.netlifyIdentity = netlifyIdentity;
169
// You must run this once before trying to interact with the widget
1710
netlifyIdentity.init();
1811

19-
// class AppWithAuth extends React.Component {
20-
// state = {
21-
// user: null
22-
// };
23-
// componentDidMount() {
24-
// netlifyIdentity.on('login', user => this.setState({ user }));
25-
// }
26-
// render() {
27-
// // // Get the current user:
28-
// // const user = netlifyIdentity.currentUser();
29-
// // you can also just use the user from the setState above
30-
31-
// return (
32-
// <Router>
33-
// <App />
34-
// </Router>
35-
// );
36-
// }
37-
// }
38-
39-
// ReactDOM.render(<AppWithAuth />, document.getElementById('root'));
4012
ReactDOM.render(<App />, document.getElementById('root'));
4113
// registerServiceWorker();

0 commit comments

Comments
 (0)