Created
November 26, 2020 20:43
-
-
Save domzgarcia/64eda678d5c5ac5b5c778ef73c249a50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = require('express')(); | |
const https = require('https'); | |
// const server = require('http').Server(app); | |
const fs = require('fs'); | |
var privateKey = fs.readFileSync('./localhost.key', 'utf8'); | |
var certificate = fs.readFileSync('./localhost.crt', 'utf8'); | |
var creds = {key: privateKey, cert: certificate}; | |
const server = https.createServer(creds, app); | |
const io = require('socket.io')(server); | |
const next = require('next'); | |
const cookieParser = require('cookie-parser'); | |
const dev = process.env.NODE_ENV !== 'production'; | |
const nextAppSettings = { | |
dev, | |
quiet: process.env.QUIET_MODE === '1', | |
}; | |
const nextApp = next(nextAppSettings); | |
const nextHandler = nextApp.getRequestHandler(); | |
const axios = require('axios'); | |
const port = 8443; | |
// const port = 3000; | |
app.use(cookieParser('byob0ecom0site0')); | |
io.on('connection', (socket) => { | |
//fire when connected to get the current count on the local storage | |
io.sockets.emit('newCart', true); | |
socket.on('setCart', (foo) => { | |
io.sockets.emit('newCart', true); | |
}); | |
socket.on('userLogin', (details) => { | |
io.sockets.emit('userLoggedIn', true); | |
}); | |
socket.on('userLogout', () => { | |
io.sockets.emit('userLoggedOut', true); | |
}); | |
//TODO: handle logged in users add to cart on different domains ( mobile x web) | |
}); | |
nextApp.prepare().then(() => { | |
app.get('/password', async (req, res) => { | |
const passwordResetToken = req.query.token; | |
try { | |
res.cookie('password-reset-token', passwordResetToken, { | |
maxAge: 100000000, | |
}); | |
res.redirect(302, '/update-password'); | |
} catch (e) { | |
//has errors | |
res.redirect(302, '/'); | |
} | |
}); | |
app.get('/registration/step-2', async (req, res) => { | |
const regToken = req.query.regToken; | |
const url = `${process.env.NEXT_PUBLIC_BYOB_HOST}/api/v1/check-registration-token`; | |
console.log('[>]', regToken, url); | |
try { | |
await axios(url, { | |
method: 'get', | |
responseType: 'application/json', | |
headers: { | |
'registration-token': regToken, | |
}, | |
}); | |
//token is valid | |
// byob reg token = brt | |
res.cookie('brt', regToken, { | |
httpOnly: true, | |
maxAge: 1000000000, | |
signed: true, | |
}); | |
res.cookie('regtoken', regToken, { | |
httpOnly: true, | |
maxAge: 100000, | |
}); | |
res.redirect(302, '/signup/final-step'); | |
} catch (e) { | |
console.log('Catch', e); | |
//has errors | |
res.redirect(302, '/'); | |
} | |
}); | |
app.get('/signup/final-step', (req, res) => { | |
//call check token--> | |
const isValid = !!req.signedCookies.brt; | |
console.log('is valid', isValid); | |
if (!isValid) { | |
res.redirect(302, '/'); | |
return; | |
} | |
res.cookie('brt', '', { | |
httpOnly: true, | |
maxAge: 0, | |
signed: true, | |
}); | |
nextHandler(req, res); | |
}); | |
app.get('*', (req, res) => { | |
nextHandler(req, res); | |
}); | |
server.listen(port, (err) => { | |
if (err) throw err; | |
console.log(`> Ready on port ${port}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment