This repository was archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (75 loc) · 1.87 KB
/
server.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict'
/**
* Dependencies
* @ignore
*/
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const session = require('express-session')
const Webauthn = require('webauthn')
/**
* Module Dependencies
* @ignore
*/
const LevelAdapter = require('webauthn/src/LevelAdapter')
/**
* Example
* @ignore
*/
const app = express()
// Session
app.use(session({
secret: 'keyboard cat',
saveUninitialized: true,
resave: false,
cookie: {
maxAge: 24 * 60 * 60 * 1000, // 24 hours
},
}))
// Static
app.use(express.static(path.join(__dirname, 'build')))
// Body parsing
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// Create webauthn
const webauthn = new Webauthn({
origin: 'http://localhost:3000',
usernameField: 'username',
userFields: {
username: 'username',
name: 'displayName',
},
store: new LevelAdapter('db'),
// OR
// store: {
// put: async (id, value) => {/* return <void> */},
// get: async (id) => {/* return User */},
// search: async (search) => {/* return { [username]: User } */},
// delete: async (id) => {/* return boolean */},
// },
rpName: 'Stranger Labs, Inc.',
})
// Mount webauthn endpoints
app.use('/webauthn', webauthn.initialize())
// Endpoint without passport
app.get('/secret', webauthn.authenticate(), (req, res) => {
res.status(200).json({ status: 'ok', message: `Super secret message for ${req.user.displayName}` })
})
// Debug
app.get('/db', async (req, res) => {
res.status(200).json(await webauthn.store.search())
})
// Debug
app.get('/session', (req, res) => {
res.status(200).json(req.session)
})
// Serve React App
app.use((req, res) => {
return res.sendFile(path.join(__dirname, 'build', 'index.html'))
})
// Listen
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log('Listening on port', port)
})