-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 2.86 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
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
89
90
91
92
93
94
95
96
const express = require('express');
const app = express();
const passport = require('passport');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
import { gateway } from './gateway.js';
import { strategy } from './setup-passport.js';
app.engine('ejs', require('ejs').renderFile);
app.use(cookieParser());
app.use(session({ secret: process.env.SECRET, resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
function requireLogin(req, res, next) {
if (!req.isAuthenticated()) {
return res.redirect('/login/failed');
}
next();
}
app.get('/login/failed', (req, res) => {
res.end('You must login to view this page')
});
app.get('/created', (req, res) => {
res.cookie('connect.sid','', { maxAge: 1 });
res.render('created.ejs');
});
app.get('/user', requireLogin, (req, res) => {
if(!req.user._json.app_metadata || !req.user._json.app_metadata.braintreeId) {
console.log(req.user._json)
// if they have passed the requireLogin function and a valid users but do
// not have the braintreeId, this is most likely becasue we just create the user accoutn.
// a workaround is to show a "created" message to them, and ask them to login again.
return res.redirect('/created');
}
gateway.clientToken.generate({
customerId: req.user._json.app_metadata.braintreeId
}, (err, response) => {
if(err) {
return res.send(':( something wrong has happened. Try again later<br/><br/>Details:<br/>' + JSON.stringify(err));
}
res.render('user.ejs', {
user: req.user.displayName,
'clientToken': response.clientToken,
'auth0domain': process.env.AUTH0_DOMAIN
});
});
});
app.get('/login/callback',
passport.authenticate('auth0', { failureRedirect: '/login/failed' }),
(req, res) => {
if (!req.user) {
return res.end('Unexpected error, try login in again');
}
res.redirect("/user");
}
);
app.get('/', (req, res) => {
if(req.user) {
return res.redirect("/user");
}
res.render('login.ejs', {
'auth0domain': process.env.AUTH0_DOMAIN,
'auth0clientId': process.env.AUTH0_CLIENT_ID,
});
});
app.post("/checkout", (req, res) => {
// NOTE: In sandbox mode this will be empty, replace it the line below;
// const nonce = fake-valid-nonce
const nonce = req.body.payment_method_nonce;
gateway.transaction.sale({
amount: '1.00',
paymentMethodNonce: nonce
}, (err, result) => {
if(err) {
return res.end('Transaction did not go trough, your card was not charged.' + JSON.stringify(result.errors.deepErrors()));
}
res.end('Transaction sucessed')
});
});
app.listen(process.env.PORT || 3000);
console.log('Applicationen is running on localhost:' + (process.env.PORT || 3000))