/
dashboard.js
62 lines (55 loc) · 1.77 KB
/
dashboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// this import is only included in the server build since its only used in getServerSideProps
import Passage from '@passageidentity/passage-node';
import styles from '../styles/App.module.css';
import { useEffect } from 'react';
function Dashboard({isAuthorized, appID}){
useEffect(()=>{
require('@passageidentity/passage-elements/passage-profile');
}, []);
const authorizedBody =
<>
You successfully signed in with Passage.
<br/><br/>
<passage-profile app-id={appID}></passage-profile>
</>
const unauthorizedBody =
<>
You have not logged in and cannot view the dashboard.
<br/><br/>
<a href="/" className={styles.link}>Login to continue.</a>
</>
return (
<div className={styles.dashboard}>
<div className={styles.title}>{isAuthorized ? 'Welcome!' : 'Unauthorized'}</div>
<div className={styles.message}>
{ isAuthorized ? authorizedBody : unauthorizedBody }
</div>
</div>
);
};
export async function getServerSideProps(context) {
// getServerSideProps runs server-side only and will never execute on the client browser
// this allows the safe use of a private Passage API Key
const appID = process.env.PASSAGE_APP_ID;
const passage = new Passage({
appID,
apiKey: process.env.PASSAGE_API_KEY,
authStrategy: "HEADER",
});
try {
const authToken = context.req.cookies['psg_auth_token'];
const req = {
headers: {
authorization: `Bearer ${authToken}`,
},
};
const userID = await passage.authenticateRequest(req);
if (userID) {
return { props: {isAuthorized: true, appID: appID} };
}
} catch (error) {
// authentication failed
return { props: {isAuthorized: false, appID: appID} };
}
}
export default Dashboard;