Skip to content

Commit d0e4cb7

Browse files
authored
example for private/normal data (#29)
* example for private/normal data * fixup! example for private/normal data * remove /api prefix in backend paths
1 parent 28c5240 commit d0e4cb7

6 files changed

Lines changed: 63 additions & 29 deletions

File tree

templates/src/api/kratos.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const adminApi = new AdminApiAxiosParamCreator({basePath : authAdminURL });
2121
* maps and calls flows[Login/Registration/Recovery/Settings] to kratos sdk fn names
2222
*/
2323
const getBrowserFlowParams = async (flow) => publicApi[`initializeSelfService${flow}ViaBrowserFlow`]();
24-
const getRequestFlowData = async (flow, id) => publicApi[`getSelfService${flow}Flow`](id);
24+
const getRequestFlowData = async (flow, id) => adminApi[`getSelfService${flow}Flow`](id);
2525

2626
/**
2727
* generate<Logout/FormRequest/RequestData/Session>URL uses SDK to generate endpoint for http client
@@ -34,7 +34,7 @@ const generateLogoutUrl = async () => {
3434
const generateFormRequestUrl = async (type) => {
3535
let { url } = await getBrowserFlowParams(capitalize(type));
3636
// Workaround for bug in SDK specs: https://github.com/ory/sdk/issues/43
37-
if (type == "settings") {
37+
if (type === "settings") {
3838
url = url.replace(/\/flows$/, '');
3939
}
4040
return authPublicURL + url;

templates/src/components/Card.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
.card {
2-
padding: 4%;
2+
padding: 2%;
33
background: #eee;
44
border: none;
55
border-radius: 0px;
66
min-width: 350px;
7+
max-width: 450px;
8+
margin-top: 50px;
79
}
810

911
.card::before {
1012
content: attr(header);
1113
position: absolute;
12-
margin-top: calc(-4% - 1.5em);
13-
margin-left: calc(-4%);
14+
/* the % offsets the padding on .card */
15+
margin-top: calc(-2% - 1.5em);
16+
margin-left: calc(-2%);
1417
text-indent: 0.2em;
1518
font-weight: 600;
1619
}

templates/src/components/Logo.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.logo {
22
font-weight: 400;
33
font-size: 2.5em;
4-
margin: 1em 0;
4+
margin: 1rem;
55
}

templates/src/pages/Auth.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@
2929
.separator::after {
3030
margin-left: .25em;
3131
}
32+
33+
.auth-container .card{
34+
padding: 4%;
35+
}
36+
.auth-container .card::before {
37+
/* the % offsets the padding on .card */
38+
margin-top: calc(-4% - 1.5em);
39+
margin-left: calc(-4%);
40+
}

templates/src/pages/Home.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
justify-content: center;
66
}
77

8+
.Home .card {
9+
width: 400px;
10+
}
11+
812
.App-logo {
913
width: calc(1 / 8 * 100vw);
1014
}

templates/src/pages/Home.js

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { useEffect, useState } from 'react'
2-
import './Home.css'
32
import config from '../config';
4-
5-
import '../components/Info'
3+
import './Home.css'
64
import InfoPanel from '../components/Info'
5+
<%if eq (index .Params `userAuth`) "yes" %>import Card from '../components/Card'<% end %>
76

87
function Home() {
98
const [data, setData] = useState({
@@ -14,32 +13,51 @@ function Home() {
1413
const [status, setStatus] = useState({
1514
code: 'Checking...',
1615
})
16+
<%if eq (index .Params `userAuth`) "yes" %>
17+
const [privateState, setPrivateData] = useState({
18+
isLoading: true,
19+
data: {},
20+
});
21+
22+
const fetchPrivateData = async(data) => {
23+
const uri = `${config.backendURL}/auth/userInfo`
24+
const resp = await fetch(uri,{credentials : "include"});
25+
return {
26+
data: await resp.json(),
27+
}
28+
}<% end %>
29+
30+
const fetchInfoPanel = async() => {
31+
const resp = await fetch(`${config.backendURL}/status/about`, { credentials: "include" });
32+
const code = resp.status
33+
const info = await resp.json()
34+
return { code, info };
35+
};
36+
1737

1838
useEffect(() => {
19-
fetch(`${config.backendURL}/status/about`, { credentials: "include" })
20-
.then((result) => {
21-
setStatus({
22-
code: result.status,
23-
})
24-
return result.json()
25-
})
26-
.then((data) => {
27-
setData({
28-
info: data,
29-
error: null,
30-
})
31-
})
32-
.catch((error) => {
33-
setData({
34-
info: {},
35-
error: error,
36-
})
37-
})
38-
}, [])
39+
fetchInfoPanel().then(({code, info}) => {
40+
setStatus({ code });
41+
setData({ info, error: null })
42+
})
43+
.catch((error) => {
44+
setData({ info: {}, error: error })
45+
});
46+
<%if eq (index .Params `userAuth`) "yes" %>
47+
fetchPrivateData().then(setPrivateData);<% end %>
48+
}, [])
3949

4050
return (
4151
<main className="Home">
4252
<InfoPanel data={data} status={status} config={config} />
53+
<%if eq (index .Params `userAuth`) "yes" %>
54+
<Card header="Authenticated Data">
55+
{
56+
privateState.isLoading ?
57+
<div>Fetching data...</div> :
58+
<pre>{ JSON.stringify(privateState.data, null , 2) }</pre>
59+
}
60+
</Card><% end %>
4361
</main>
4462
)
4563
}

0 commit comments

Comments
 (0)