Skip to content

Commit bc053bf

Browse files
author
Zach Lysobey
committed
(feature) start blockstack integration: Hello World
1 parent 3d36b39 commit bc053bf

File tree

9 files changed

+120
-15
lines changed

9 files changed

+120
-15
lines changed

cors/.firebaserc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

cors/_headers

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/index.html
2+
Access-Control-Allow-Origin: *
3+
Access-Control-Allow-Headers: "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
4+
Access-Control-Allow-Methods: "POST, GET, OPTIONS, DELETE, PUT"
5+
/manifest.json
6+
Access-Control-Allow-Origin: *
7+
Access-Control-Allow-Headers: "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
8+
Access-Control-Allow-Methods: "POST, GET, OPTIONS, DELETE, PUT"

cors/_redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* /index.html 200

cors/firebase.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"hosting": {
3+
"public": "build",
4+
"ignore": [
5+
"firebase.json",
6+
"**/.*",
7+
"**/node_modules/**"
8+
],
9+
"rewrites": [
10+
{
11+
"source": "**",
12+
"destination": "/index.html"
13+
}
14+
],
15+
"headers": [ {
16+
"source" : "manifest.json",
17+
"headers" : [ {
18+
"key" : "Access-Control-Allow-Origin",
19+
"value" : "*"
20+
} ]
21+
} ]
22+
}
23+
}

src/App.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import React from 'react';
2-
import logo from './logo.svg';
2+
import { UserSession } from 'blockstack';
3+
34
import './App.css';
45

5-
const App: React.FC = () => {
6+
import { SignedIn } from './SignedIn';
7+
import { Landing } from './Landing';
8+
9+
interface Props {
10+
userSession: UserSession
11+
}
12+
const App: React.FC<Props> = ({
13+
userSession
14+
}) => {
615
return (
716
<div className="App">
817
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.tsx</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
18+
<p>Learning Blockstack!</p>
19+
{
20+
userSession.isUserSignedIn()
21+
? <SignedIn name={'World'}/>
22+
: <Landing userSession={userSession}/>
23+
}
2124
</header>
2225
</div>
2326
);

src/Landing.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
3+
import { UserSession } from 'blockstack'
4+
5+
interface Props {
6+
userSession: UserSession
7+
}
8+
export const Landing: React.FC<Props> = ({ userSession }) => {
9+
const signIn = () => userSession.redirectToSignIn()
10+
return <>
11+
<p>Landing...</p>
12+
13+
<button
14+
onClick={signIn}
15+
>
16+
Sign in
17+
</button>
18+
</>
19+
}

src/SignedIn.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
3+
interface AppProps {
4+
name: string
5+
}
6+
export const SignedIn: React.FC<AppProps> = ({
7+
name
8+
}) => {
9+
console.log({ name })
10+
return <p>Signed In. Hello {name}!</p>
11+
}

src/index.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,29 @@ import './index.css';
44
import App from './App';
55
import * as serviceWorker from './serviceWorker';
66

7-
ReactDOM.render(<App />, document.getElementById('root'));
7+
import { UserSession, AppConfig } from 'blockstack';
8+
9+
const appConfig = new AppConfig(['store_write', 'publish_data'])
10+
const userSession = new UserSession({ appConfig });
11+
12+
const isUserSignedIn = userSession.isUserSignedIn()
13+
const isSignInPending = userSession.isSignInPending()
14+
15+
if (!isUserSignedIn && isSignInPending) {
16+
userSession.handlePendingSignIn()
17+
.then((userData) => {
18+
if (!userData.username) {
19+
throw new Error('This app requires a username.')
20+
}
21+
window.location.href = `/kingdom/${userData.username}`
22+
})
23+
}
24+
25+
const app = <App userSession={userSession} />;
26+
27+
const $root = document.getElementById('root');
28+
29+
ReactDOM.render(app, $root);
830

931
// If you want your app to work offline and load faster, you can change
1032
// unregister() to register() below. Note this comes with some pitfalls.

src/setupProxy.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 'React-scripts start' sets up a development server that automatically
2+
// registers files with this name. This proxy file sets up a CORS header
3+
// for manifest.json, allowing sign in via Blockstack without using
4+
// the webpack configuration file that create-react-app has configured
5+
// and hidden.
6+
7+
module.exports = function(app) {
8+
app.get('/manifest.json', (req, res, next) => {
9+
res.set({
10+
'Access-Control-Allow-Origin': '*',
11+
'Access-Control-Allow-Headers': '*',
12+
'Access-Control-Allow-Methods': 'GET',
13+
'X-random': 'random'
14+
});
15+
next();
16+
})
17+
};

0 commit comments

Comments
 (0)