forked from nickredmark/ooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 1.92 KB
/
index.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
const {MongoClient, ObjectId} = require('mongodb')
const express = require('express')
const session = require('express-session')
const {promisify} = require('util')
const Ooth = require('ooth')
const oothLocal = require('ooth-local')
const OothMongo = require('ooth-mongo')
const MONGO_HOST = 'mongodb://localhost:27017'
const MONGO_DB = 'ooth-minimal'
const HOST = 'http://localhost'
const PORT = 3000
const SECRET = 'somesecret'
const SHARED_SECRET = 'somesharedsecret'
const OOTH_PATH = '/auth'
const start = async () => {
try {
const client = await MongoClient.connect(MONGO_HOST)
const db = client.db(MONGO_DB)
const app = express()
app.use(session({
name: 'api-session-id',
secret: SECRET,
resave: false,
saveUninitialized: true,
}))
const ooth = new Ooth({
sharedSecret: SHARED_SECRET,
path: OOTH_PATH,
})
const oothMongo = new OothMongo(db, ObjectId)
await ooth.start(app, oothMongo)
ooth.use('local', oothLocal({
onRegister({email, verificationToken, _id}) {
console.log(`Someone registered.`)
},
onGenerateVerificationToken({email, verificationToken}) {
console.log(`Someone requested a verification email.`)
},
onVerify({email}) {
console.log(`Someone verified their email`)
},
onForgotPassword({email, passwordResetToken}) {
console.log(`Someone forgot their password`)
},
onResetPassword({email}) {
console.log(`Someone reset their password`)
},
}))
app.get('/', (req, res) => res.sendFile(`${__dirname}/index.html`))
await promisify(app.listen)(PORT)
console.info(`Online at ${HOST}:${PORT}`)
} catch (e) {
console.error(e)
}
}
start()