Work on signup. Removed polka, using express since passport was being weird

This commit is contained in:
Robert Hall
2018-09-22 16:31:32 -06:00
parent d97a4693e1
commit 297a951fcb
11 changed files with 680 additions and 71 deletions

View File

@@ -1,16 +1,15 @@
// WARNING: THIS HELPER FILE IS NOT GOOD PRACTICE AND ONLY HERE FOR CONVENIENCE
// use a real database for persisting users instead
// const Users = [{
// username: 'general-zod',
// email: 'general.zod@krypton.com',
// hash: '',
// }, {
// username: 'kal-el',
// email: 'kal-el@krypton.com',
// hash: '',
// }];
const Users = [];
const Users = [{
username: 'general-zod',
email: 'general.zod@krypton.com',
hash: '$2b$10$wP/YQvEX1pC4F1Unnf46ceOR1I6Q.OgOtRNjUT7NxbBDW8vxEEGSK', // the password is `password`
}, {
username: 'kal-el',
email: 'kal-el@krypton.com',
hash: '$2b$10$wP/YQvEX1pC4F1Unnf46ceOR1I6Q.OgOtRNjUT7NxbBDW8vxEEGSK', // the password is `password`
}];
export default {
find(key, value) {

View File

@@ -9,6 +9,7 @@ import { Strategy as LocalStrategy } from 'passport-local';
import db from './db';
const env = process.env.NODE_ENV;
const JWT_SECRET = 'put-your-JWT-secret-here'; // you can set this w/ an environment variable
export function authSetup(app) {
@@ -33,6 +34,7 @@ export function authSetup(app) {
app.post('/auth/signup', async(req, res, next) => {
try {
debugger
const { username, email, password } = req.body;
const userExists = db.find('username', username);
@@ -51,13 +53,13 @@ export function authSetup(app) {
// generate a signed son web token with the contents of user object and return it in the response
const month = 60 * 60 * 24 * 30;
const token = jwt.sign(userToSendToClient, config.JWT_SECRET, { expiresIn: month });
const token = jwt.sign(userToSendToClient, JWT_SECRET, { expiresIn: month });
res.cookie('ds', token, {
// httpOnly: false,
secure: env === 'production' ? true : false,
maxAge: 1000 * month,
});
res.status(200).send({ userToSendToClient });
res.status(200).send({ user: userToSendToClient });
} catch (error) {
res.status(400).send({ error: 'req body should take the form { username, password }' });
}
@@ -77,7 +79,7 @@ export function authSetup(app) {
}
// generate a signed son web token with the contents of user object and return it in the response
const month = 60 * 60 * 24 * 30;
const token = jwt.sign(user, config.JWT_SECRET, { expiresIn: month });
const token = jwt.sign(user, JWT_SECRET, { expiresIn: month });
return res.cookie('ds', token, {
// httpOnly: false,
secure: env === 'production' ? true : false,

View File

@@ -1,7 +1,27 @@
import { init } from 'sapper/runtime.js';
import { manifest } from './manifest/client.js';
import { Store } from 'svelte/store.js'
init({
target: document.querySelector('#sapper'),
manifest
});
manifest,
store: data => {
const user = data.user;
const store = new Store(data);
if (!user) {
// SEE: https://stackoverflow.com/questions/10593013/delete-cookie-by-name
document.cookie = 'ds=;expires=Sun, 09 Jan 1974 00:00:01 GMT;';
}
store.set({
logout: () => {
return fetch('auth/logout', { method: 'POST' }).then(() => {
// SEE: https://stackoverflow.com/questions/10593013/delete-cookie-by-name
document.cookie = 'ds=;expires=Sun, 09 Jan 1974 00:00:01 GMT;';
store.set({ user: null });
window.location = '/'
})
},
})
return store
}
});

View File

@@ -1,35 +1,36 @@
import sirv from 'sirv';
import polka from 'polka';
// import bodyParser from 'body-parser';
// import cookieParser from 'cookie-parser';
import express from 'express';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import { authSetup } from './auth/setup';
import sapper from 'sapper';
import compression from 'compression';
// import { Store } from 'svelte/store.js';
import { Store } from 'svelte/store.js';
// import { validate } from '../routes/_services/auth-check.js';
import { manifest } from './manifest/server.js';
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';
const app = polka() // You can also use Express
polka()
app.use(compression({ threshold: 0 }))
app.use(sirv('assets', { dev }))
// app.use(bodyParser.json())
// app.use(bodyParser.urlencoded({ extended: true }))
// app.use(cookieParser())
const app = express()
authSetup(app)
app.use(compression({ threshold: 0 }))
app.use(sirv('assets', { dev }))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(sapper({
manifest,
store: req => {
// const user = validate(req);
// return new Store({ user: user.unauthorized ? null : user });
},
}))
authSetup(app)
app.listen(PORT, err => {
if (err) console.log('error', err);
})
app.use(sapper({
manifest,
store: req => {
// const user = validate(req);
// return new Store({ user: user.unauthorized ? null : user });
return new Store({ user: null });
},
}))
app.listen(PORT, err => {
if (err) console.log('error', err);
})